<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: K Fuquay</title>
    <description>The latest articles on Forem by K Fuquay (@kfuquay).</description>
    <link>https://forem.com/kfuquay</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F233933%2Fd3edbead-ee51-46be-bb6d-aa7f6ebe594e.jpg</url>
      <title>Forem: K Fuquay</title>
      <link>https://forem.com/kfuquay</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/kfuquay"/>
    <language>en</language>
    <item>
      <title>Request buffering with reverse proxies</title>
      <dc:creator>K Fuquay</dc:creator>
      <pubDate>Fri, 01 Jul 2022 17:14:52 +0000</pubDate>
      <link>https://forem.com/kfuquay/request-buffering-with-reverse-proxies-551c</link>
      <guid>https://forem.com/kfuquay/request-buffering-with-reverse-proxies-551c</guid>
      <description>&lt;h2&gt;
  
  
  What is a reverse proxy?
&lt;/h2&gt;

&lt;p&gt;A reverse proxy is a server that forwards requests from the client to another server.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--lLx8LXGH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/leahl8u922jdon4xqad9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--lLx8LXGH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/leahl8u922jdon4xqad9.png" alt="Flow chart describing relationship between client, reverse proxy, and upstream server.  The client is on the left with an arrow pointing towards the reverse proxy, which is in the center of the flow chart.  The reverse proxy points towards the upstream server, which is on the right." width="880" height="110"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Why use reverse proxies
&lt;/h2&gt;

&lt;p&gt;There are several reasons you might want to use a reverse proxy:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Load balancing - a reverse proxy can be used to distribute requests across a number of servers.&lt;/li&gt;
&lt;li&gt;Caching - caching and serving content from a reverse proxy can increase performance.&lt;/li&gt;
&lt;li&gt;Security - using a reverse proxy will obscure architectural details of your backend infrastructure as well as server identities (the IP of the origin server for instance).&lt;/li&gt;
&lt;li&gt;Maintainability - in the event that your upstream server moves, you would only need to update the value of the upstream server in a single place.&lt;/li&gt;
&lt;li&gt;Request buffering - Minimizes upstream server execution time.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  A closer look at request buffering
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4-Kq1p5G--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1keq8t993tm0ypxyvplg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4-Kq1p5G--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1keq8t993tm0ypxyvplg.png" alt='Flow chart describing relationship between client, reverse proxy, and upstream server.  The client is on the left with an arrow pointing towards the reverse proxy, which is in the center of the flow chart.  This relationship is described as "slow request".  The reverse proxy points towards the upstream server, which is on the right.  This relationship is described as "fast request".' width="880" height="110"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let’s take the example of a client on a slow connection trying to upload a large file (an image for instance) to a server.  When the server receives this request it will be blocked until it receives the entire request &amp;amp; processes it.  Essentially, the servers execution time will be directly affected by the connection speed of the client.&lt;/p&gt;

&lt;p&gt;A reverse proxy will wait to receive all of the request from the client before forwarding it to the upstream server.  This means that the actual execution time on the server is greatly reduced, as this server-to-server request is much faster than the client-to-server request.&lt;/p&gt;

&lt;p&gt;This pattern can be particularly useful when your upstream server has strict execution time limitations.  For example, &lt;a href="https://docs.netlify.com/functions/overview/#default-deployment-options"&gt;Netlify’s synchronous serverless functions&lt;/a&gt; have an execution limit of 10 seconds, which begins as soon as the http connection is opened.  Using a reverse proxy in this case could decrease the risk of hitting that execution limit.&lt;/p&gt;

</description>
      <category>devops</category>
    </item>
    <item>
      <title>Extending Python's JSON Encoder</title>
      <dc:creator>K Fuquay</dc:creator>
      <pubDate>Fri, 11 Mar 2022 19:29:44 +0000</pubDate>
      <link>https://forem.com/kfuquay/extending-pythons-json-encoder-7k0</link>
      <guid>https://forem.com/kfuquay/extending-pythons-json-encoder-7k0</guid>
      <description>&lt;p&gt;I recently ran into a situation where I needed to ensure consistent formatting of datetimes as I serialized a dict to JSON using python’s built-in &lt;a href="https://docs.python.org/3/library/json.html"&gt;json&lt;/a&gt; library.&lt;/p&gt;

&lt;p&gt;The &lt;a href="https://www.json.org/json-en.html"&gt;JSON specification&lt;/a&gt; does not specify how to handle dates - so it follows that the &lt;a href="https://docs.python.org/3/library/json.html#json.JSONEncoder"&gt;JSON encoder&lt;/a&gt; does not, by default, include any date formatting.&lt;/p&gt;

&lt;p&gt;Attempting to convert a dict which includes a datetime to JSON using the default JSON Encoder will raise a TypeError.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;json&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;datetime&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;datetime&lt;/span&gt;

&lt;span class="n"&gt;obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="s"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"test"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s"&gt;"created"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;datetime&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;utcnow&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;obj_default_encoder&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dumps&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;obj_default_encoder&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# TypeError: Object of type datetime is not JSON serializable
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I was able to achieve the behavior I wanted by defining a class which extends &lt;code&gt;json.JSONEncoder&lt;/code&gt;.  In this class I included instructions for handling dates - you could also include other modifications to the default behavior of JSONEncoder.  Finally, I passed the custom encoder class to the &lt;code&gt;cls&lt;/code&gt; kwarg of &lt;code&gt;json.dumps&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;From &lt;a href="https://docs.python.org/3/library/json.html#json.dump"&gt;the python docs&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;To use a custom &lt;a href="https://docs.python.org/3/library/json.html#json.JSONEncoder"&gt;&lt;code&gt;JSONEncoder&lt;/code&gt;&lt;/a&gt; subclass (e.g. one that overrides the &lt;code&gt;default()&lt;/code&gt; method to serialize additional types), specify it with the &lt;em&gt;cls&lt;/em&gt; kwarg; otherwise &lt;a href="https://docs.python.org/3/library/json.html#json.JSONEncoder"&gt;&lt;code&gt;JSONEncoder&lt;/code&gt;&lt;/a&gt; is used.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Usage Example
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;json&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;datetime&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;datetime&lt;/span&gt;

&lt;span class="c1"&gt;#
# Defining a custom JSON encoder class
#
&lt;/span&gt;
&lt;span class="c1"&gt;# Custom class extends json.JSONEncoder
&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;CustomJSONEncoder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;JSONEncoder&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;

    &lt;span class="c1"&gt;# Override default() method
&lt;/span&gt;    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;default&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;

        &lt;span class="c1"&gt;# Datetime to string
&lt;/span&gt;        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nb"&gt;isinstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;datetime&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
            &lt;span class="c1"&gt;# Format datetime - `Fri, 21 Aug 2020 17:59:59 GMT`
&lt;/span&gt;            &lt;span class="n"&gt;obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;strftime&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'%a, %d %b %Y %H:%M:%S GMT'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;obj&lt;/span&gt;

        &lt;span class="c1"&gt;# Default behavior for all other types
&lt;/span&gt;        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;super&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;default&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;#
# Using custom class to serialize a dict to JSON
#
&lt;/span&gt;
&lt;span class="n"&gt;obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="s"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"test"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s"&gt;"created"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;datetime&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;utcnow&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;obj_custom_encoder&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dumps&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cls&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;CustomJSONEncoder&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;obj_custom_encoder&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# {"name": "test", "created": "Fri, 11 Mar 2022 19:11:51 GMT"}
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>python</category>
      <category>todayilearned</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
