<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Jan Fornoff</title>
    <link>https://blog.janfornoff.com/</link>
    <description>Recent content on Jan Fornoff</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en-us</language>
    <copyright>Jan Fornoff</copyright>
    <lastBuildDate>Sun, 14 Jul 2019 10:00:00 +0200</lastBuildDate>
    
    <atom:link href="https://blog.janfornoff.com/index.xml" rel="self" type="application/rss+xml" />
    
    
    <item>
      <title>Consistent Hashing</title>
      <link>https://blog.janfornoff.com/2019/07/14/consistent-hashing/</link>
      <pubDate>Sun, 14 Jul 2019 10:00:00 +0200</pubDate>
      
      <guid>https://blog.janfornoff.com/2019/07/14/consistent-hashing/</guid>
      <description>

&lt;h1 id=&#34;the-problem&#34;&gt;The Problem&lt;/h1&gt;

&lt;p&gt;You need to handle more requests than a single server can deal with.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;First thought: Well, we can have &lt;strong&gt;two servers&lt;/strong&gt;, duh.&lt;/li&gt;
&lt;li&gt;Second thought: How do we decide where each request should go?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There are different methods of distributing requests between servers.
The simplest method that intuitively comes to mind is &lt;strong&gt;Round Robin&lt;/strong&gt;, where you allocate requests
to servers one-by-one, starting over at the beginning when everyone had a turn.
Obviously, the distribution of requests is very even (note that this does not quite imply perfect utilization, since not every request takes equal resources and time to process).&lt;/p&gt;

&lt;p&gt;&lt;center&gt;
  &lt;figure class=&#34;fill&#34;&gt;
    &lt;img src=&#34;https://blog.janfornoff.com/assets/img/consistenthashing/roundrobin.svg&#34;
         alt=&#34;Round Robin&#34;/&gt; &lt;figcaption&gt;
            &lt;p&gt;Round Robin&lt;/p&gt;
        &lt;/figcaption&gt;
&lt;/figure&gt;

&lt;/center&gt;&lt;/p&gt;

&lt;p&gt;This doesn&amp;rsquo;t always work great.
Imagine a case where you have to do slow computations to serve requests. You could cache the results to save redundant work and make response times faster.
In this case, applying Round Robin would lead to redundant work and inefficient memory usage (due to each server computing and caching results).&lt;/p&gt;

&lt;p&gt;Now imagine load increasing to a point where the servers must expire cache entries due to limited memory space.
These expired cache entires will have to get recomputed or fetched, again doing avoidable work.&lt;/p&gt;

&lt;p&gt;Here&amp;rsquo;s where request hashing can be useful.&lt;/p&gt;

&lt;h1 id=&#34;request-hashing&#34;&gt;Request hashing&lt;/h1&gt;

&lt;p&gt;A hash function (in the context of distribution) is not much more than a mathematical formula that projects a large space of values into a smaller one. This method lends itself well to mapping requests to servers (or &amp;ldquo;items&amp;rdquo; to &amp;ldquo;buckets&amp;rdquo;, which is essentially equivalent).
This mapping is also stable, i.e., two identical requests are going to have the same hash value, thus mapping to the same server.&lt;/p&gt;

&lt;p&gt;Load balancing can exploit this property to choose a hash function such that requests requiring the same cache data hit the same servers. When more similar requests hit the same servers, we increase the likelihood of cache hits! 🎉&lt;/p&gt;

&lt;p&gt;For an example, let&amp;rsquo;s pretend that we have a service that could profit from caching, and we have three servers.
&lt;center&gt;
  &lt;figure class=&#34;fill&#34;&gt;
    &lt;img src=&#34;https://blog.janfornoff.com/assets/img/consistenthashing/loadbalancingexample.svg&#34;/&gt; 
&lt;/figure&gt;

&lt;/center&gt;&lt;/p&gt;

&lt;p&gt;We could then use a hash to distribute requests naively as follows:&lt;/p&gt;

&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-go&#34; data-lang=&#34;go&#34;&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;ServerToSelect&lt;/span&gt;(&lt;span style=&#34;color:#a6e22e&#34;&gt;Request&lt;/span&gt;, &lt;span style=&#34;color:#a6e22e&#34;&gt;NumServers&lt;/span&gt;) = &lt;span style=&#34;color:#a6e22e&#34;&gt;Hash&lt;/span&gt;(&lt;span style=&#34;color:#a6e22e&#34;&gt;Request&lt;/span&gt;) &lt;span style=&#34;color:#a6e22e&#34;&gt;mod&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;NumServers&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Let&amp;rsquo;s also say we have three requests hashing as follows:
&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-go&#34; data-lang=&#34;go&#34;&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;Hash&lt;/span&gt;(&lt;span style=&#34;color:#a6e22e&#34;&gt;R1&lt;/span&gt;) = &lt;span style=&#34;color:#ae81ff&#34;&gt;3&lt;/span&gt;
&lt;span style=&#34;color:#a6e22e&#34;&gt;Hash&lt;/span&gt;(&lt;span style=&#34;color:#a6e22e&#34;&gt;R2&lt;/span&gt;) = &lt;span style=&#34;color:#ae81ff&#34;&gt;8&lt;/span&gt;
&lt;span style=&#34;color:#a6e22e&#34;&gt;Hash&lt;/span&gt;(&lt;span style=&#34;color:#a6e22e&#34;&gt;R3&lt;/span&gt;) = &lt;span style=&#34;color:#ae81ff&#34;&gt;19&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;

&lt;p&gt;This would give us:
&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-go&#34; data-lang=&#34;go&#34;&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;ServerToSelect&lt;/span&gt;(&lt;span style=&#34;color:#a6e22e&#34;&gt;R1&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;3&lt;/span&gt;) = &lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt; &lt;span style=&#34;color:#75715e&#34;&gt;// first server
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;&lt;/span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;ServerToSelect&lt;/span&gt;(&lt;span style=&#34;color:#a6e22e&#34;&gt;R2&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;3&lt;/span&gt;) = &lt;span style=&#34;color:#ae81ff&#34;&gt;2&lt;/span&gt; &lt;span style=&#34;color:#75715e&#34;&gt;// third server
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;&lt;/span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;ServerToSelect&lt;/span&gt;(&lt;span style=&#34;color:#a6e22e&#34;&gt;R3&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;3&lt;/span&gt;) = &lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;//&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;second&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;server&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;

&lt;p&gt;Now let&amp;rsquo;s say we look at our monitoring and notice that an extra server should be added.&lt;/p&gt;

&lt;h3 id=&#34;ohhh-no-elasticity&#34;&gt;Ohhh no, elasticity!&lt;/h3&gt;

&lt;p&gt;Let&amp;rsquo;s see how the additional server affects the request allocation.&lt;/p&gt;

&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-go&#34; data-lang=&#34;go&#34;&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;ServerToSelect&lt;/span&gt;(&lt;span style=&#34;color:#a6e22e&#34;&gt;R1&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;4&lt;/span&gt;) = &lt;span style=&#34;color:#ae81ff&#34;&gt;3&lt;/span&gt; &lt;span style=&#34;color:#75715e&#34;&gt;// fourth server
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;&lt;/span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;ServerToSelect&lt;/span&gt;(&lt;span style=&#34;color:#a6e22e&#34;&gt;R2&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;4&lt;/span&gt;) = &lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt; &lt;span style=&#34;color:#75715e&#34;&gt;// first server
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;&lt;/span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;ServerToSelect&lt;/span&gt;(&lt;span style=&#34;color:#a6e22e&#34;&gt;R3&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;4&lt;/span&gt;) = &lt;span style=&#34;color:#ae81ff&#34;&gt;3&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;//&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;fourth&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;server&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;As we can see, after adding a node, the request allocation totally changed.
This is problematic since all the cached data on the servers is now either useless
or at least less relevant to the requests hitting it.&lt;/p&gt;

&lt;p&gt;Can we do better than this? Well of course we can!&lt;/p&gt;

&lt;h1 id=&#34;consistent-hashing&#34;&gt;Consistent hashing&lt;/h1&gt;

&lt;p&gt;Consistent hashing provides us a more stable way of dividing requests in the face of changing
server pools (original paper for those interested: &lt;a href=&#34;https://www.akamai.com/us/en/multimedia/documents/technical-publication/consistent-hashing-and-random-trees-distributed-caching-protocols-for-relieving-hot-spots-on-the-world-wide-web-technical-publication.pdf&#34;&gt;1&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;Here is how it generally works:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;We hash an identifier of each server in our server pool (e.g., their IP).
&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-go&#34; data-lang=&#34;go&#34;&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;Hash&lt;/span&gt;(&lt;span style=&#34;color:#a6e22e&#34;&gt;S1&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;IP&lt;/span&gt;) = &lt;span style=&#34;color:#a6e22e&#34;&gt;X&lt;/span&gt;
&lt;span style=&#34;color:#a6e22e&#34;&gt;Hash&lt;/span&gt;(&lt;span style=&#34;color:#a6e22e&#34;&gt;S2&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;IP&lt;/span&gt;) = &lt;span style=&#34;color:#a6e22e&#34;&gt;Y&lt;/span&gt;
&lt;span style=&#34;color:#a6e22e&#34;&gt;Hash&lt;/span&gt;(&lt;span style=&#34;color:#a6e22e&#34;&gt;S3&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;IP&lt;/span&gt;) = &lt;span style=&#34;color:#a6e22e&#34;&gt;Z&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;We divide the Hash space between all servers (we&amp;rsquo;ll see how that works in the next section).&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;We hash requests, the server who &amp;ldquo;owns&amp;rdquo; that part of the hash space fulfills it.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&#34;the-visualization&#34;&gt;The Visualization&lt;/h3&gt;

&lt;p&gt;&lt;center&gt;
  &lt;figure class=&#34;fill&#34;&gt;
    &lt;img src=&#34;https://blog.janfornoff.com/assets/img/consistenthashing/hashring.svg&#34;/&gt; 
&lt;/figure&gt;

&lt;/center&gt;&lt;/p&gt;

&lt;p&gt;Don&amp;rsquo;t freak out yet. This is a &amp;ldquo;Hash Ring&amp;rdquo; and acts as a visual way of reasoning about how requests are going to map to servers.
We are going to assume that our Hash function returns a 64-bit integer.
The ring forms by &amp;ldquo;wrapping around&amp;rdquo; from the maximum value (&lt;code&gt;2^64 - 1&lt;/code&gt;) to the minimum value (&lt;code&gt;0&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;To determine the responsible server for a request, we look at the hash value for said request.
Then we determine the &amp;ldquo;closest&amp;rdquo; server on the hash ring.&lt;/p&gt;

&lt;p&gt;Implementation may work in different ways:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Start from the Request hash value, go clockwise until encountering a Server hash value&lt;/li&gt;
&lt;li&gt;Compare absolute distances to the two closest Server hash values (in either direction)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example:
When looking at &lt;span style=&#34;color: #03cc01&#34;&gt;&lt;strong&gt;R3&lt;/strong&gt;&lt;/span&gt;, absolute distance would map it to &lt;strong&gt;Server 3&lt;/strong&gt;, clockwise rotation would map it to &lt;strong&gt;Server 2&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The important thing to realize is that every server &amp;ldquo;owns&amp;rdquo; a part of the hash ring,
i.e., a range of request hash values that will map to it.&lt;/p&gt;

&lt;p&gt;Upon &lt;strong&gt;adding&lt;/strong&gt; a server, it takes over a part of the hash ring from neighboring servers.
Upon &lt;strong&gt;removing&lt;/strong&gt; a server, neighboring servers are taking over the part of the removed server.
The most important part: most of the hash ring (and all request mapping therein) remains unaffected.&lt;/p&gt;

&lt;p&gt;This unlocks the benefit that servers can come and go without large impacts on request distribution.
Furthermore, this distribution technique handles limited views of the server pool well.
If a server is unknown to some peers, e.g., when it has just joined the server pool, some requests will still hit the server that &amp;ldquo;owned&amp;rdquo; that section of the hash ring before. However, there is a high probability that it will still have warm caches for these types of requests!&lt;/p&gt;

&lt;h1 id=&#34;conclusion&#34;&gt;Conclusion&lt;/h1&gt;

&lt;p&gt;Consistent hashing allows for more robust reassignment semantics when spreading requests
across a dynamic set of servers. While slightly more complicated to implement, it&amp;rsquo;s still fairly simple.&lt;/p&gt;

&lt;p&gt;Closing off, I want to note two more things.
Firstly, dividing the hash ring space &lt;strong&gt;evenly&lt;/strong&gt; between all servers is not inherently solved by consistent hashing. One approach that alleviates the problem is replicating servers to multiple points on the hash ring, this comes with the drawback of multiplying memory usage.&lt;/p&gt;

&lt;p&gt;Lastly, the problem considered above is &lt;strong&gt;web caching&lt;/strong&gt;. The distribution provided by consistent hashing also proves useful
for distributed storage systems (i.e., storage systems where you cannot fit the data set onto a single machine).
The division of a dataset between computers is then called &lt;em&gt;sharding&lt;/em&gt;. Adding and removing shards benefits from consistent hashing as well, since it minimizes the data that needs to transferred between shards to keep the data base consistent. (Interesting paper: &lt;a href=&#34;https://arxiv.org/abs/1406.2294&#34;&gt;2&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;But this is a topic for another post. Bye!&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>On Circuit Breakers</title>
      <link>https://blog.janfornoff.com/2019/01/31/on-circuit-breakers/</link>
      <pubDate>Thu, 31 Jan 2019 06:48:45 +0100</pubDate>
      
      <guid>https://blog.janfornoff.com/2019/01/31/on-circuit-breakers/</guid>
      <description>

&lt;h2 id=&#34;hit-me-baby-one-more-time&#34;&gt;Hit me baby, one more time&lt;/h2&gt;

&lt;p&gt;Circuit breakers are a software development pattern that aims to increase the reliability of a service in the face of unreliable external dependencies.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If your system does not function when it has no access an external dependency, its reliability is necessarily the minimum reliability out of all its dependencies, or likely worse.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Circuit breakers rely on a property of failing requests: If there is a bunch of failing requests, subsequent requests are likely to fail as well. There are many reasons why that might be:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The opposite service is down, crashed or overloaded&lt;/li&gt;
&lt;li&gt;Network connectivity is impaired&lt;/li&gt;
&lt;li&gt;Requests are rejected by the opposite site due to bugs or misconfigurations&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All these conditions have in common that they probably won&amp;rsquo;t be gone after a few milliseconds.
This impression is reinforced when we have tried a few times already and there is no recovery to be seen.&lt;/p&gt;

&lt;h2 id=&#34;why-the-name&#34;&gt;Why the name?&lt;/h2&gt;

&lt;p&gt;As you likely know, a Circuit Breaker is an appliance used in electrical circuits to cut the connectivity in case of harmful conditions of very high current (as it often implies a short circuit).
It is providing &lt;strong&gt;fault tolerance for electrical systems&lt;/strong&gt;. The Circuit Breaker pattern provides &lt;strong&gt;fault tolerance for software systems&lt;/strong&gt;. So how does this concept transfer?&lt;/p&gt;

&lt;p&gt;As we discussed above, if there is a bunch of errors when trying to interact with an external service, there is a point (e.g., after requests to the service failed consistently for some amount of time) at which we can assume that it is not terribly useful to even make the call anymore.
The Circuit Breaker is a software component that detects when this is the case and &amp;ldquo;pulls the plug&amp;rdquo;. After that, it does not call the external service anymore. It just returns the error right away. Or even better, &lt;em&gt;there is a fallback strategy in place&lt;/em&gt;.&lt;/p&gt;

&lt;h2 id=&#34;meditating-on-failure-remediation&#34;&gt;Meditating on failure remediation&lt;/h2&gt;

&lt;p&gt;The interesting part about circuit breaking is in my opinion, that you have to think about your failure recovery. What can you do if this or that external service fails to respond to requests?
Let&amp;rsquo;s think about this on a little case study.&lt;/p&gt;

&lt;p&gt;&lt;img src=&#34;https://blog.janfornoff.com/assets/img/circuitbreakers/system.png&#34; alt=&#34;System architecture&#34; /&gt;
On our shop homepage, we want to present some recommendations from products that the user might be interested in. The front-facing application queries a recommendations system. This system uses previous orders made to figure out suitable recommendations.&lt;/p&gt;

&lt;p&gt;&lt;img src=&#34;https://blog.janfornoff.com/assets/img/circuitbreakers/purchasefailure.png&#34; alt=&#34;Purchase system failure&#34; /&gt;
Someone tripped over the power cord of the purchase system, we can&amp;rsquo;t retrieve the previous orders anymore D:&lt;/p&gt;

&lt;p&gt;How do we deal with this failure mode? We could throw our hands in the air in panic and stop serving requests altogether. Doesn&amp;rsquo;t sound great to you? Yeah no, it&amp;rsquo;s probably not.
Right now is the time where we put on our thinking hats and deliberate on &amp;ldquo;What do we do if we can&amp;rsquo;t compute recommendations based on orders?&amp;rdquo;&lt;/p&gt;

&lt;p&gt;We could:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Find a recommendation that we can always make (most popular products, anyone?)&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;Return an error to the frontend servers and tell them deal to with it. That&amp;rsquo;s a perfectly valid strategy, maybe it&amp;rsquo;s not even a big deal to not present recommendations on the home page.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Both strategies can be applied using a Circuit Breaker. The pattern is agnostic of how you deal with the detected failure. It allows you to not overload the external system further. Furthermore, you can &lt;strong&gt;respond quicker&lt;/strong&gt; instead of running into timeouts over and over, when it is highly probable that it will happen again (this is known as the &lt;em&gt;Fail Fast&lt;/em&gt; principle). This way, propagation of failure and slowness through the dependency chain is mitigated. Your frontend servers will go back to responding quickly after the recommendation system&amp;rsquo;s Circuit Breaker kicks in after a few query timeouts.&lt;/p&gt;

&lt;h2 id=&#34;conclusion&#34;&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;Circuit Breakers are a very useful pattern to deal with bursts of failure, preserving quick response times and facilitating a consciously chosen fallback strategy (whatever it might look like).
There are a lot of examples from the practice where the same principle is applied (e.g., NGINX upstreams and Kubernetes services), but that is for another time. Until next time!&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Introduction to the Transmission Control Protocol</title>
      <link>https://blog.janfornoff.com/2018/09/10/introduction-to-the-transmission-control-protocol/</link>
      <pubDate>Mon, 10 Sep 2018 19:53:51 +0000</pubDate>
      
      <guid>https://blog.janfornoff.com/2018/09/10/introduction-to-the-transmission-control-protocol/</guid>
      <description>

&lt;p&gt;This post continues the series on the fundamental protocols of the Internet, with a focus on the abstractions they provide and how they build on each other.&lt;/p&gt;

&lt;p&gt;TCP (the Transmission Control Protocol) builds on top of IP (find the &lt;a href=&#34;https://blog.janfornoff.com/2018/05/27/introduction-to-the-internet-protocol/&#34;&gt;accompanying post here&lt;/a&gt;. Based on the best-effort delivery guarantees of IP, it adds &lt;strong&gt;reliable byte stream guarantees&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In plain english this means:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Messages sent are eventually received (&lt;strong&gt;Reliability&lt;/strong&gt;)&lt;/li&gt;
&lt;li&gt;Incidental reordering of arriving data fragments is resolved&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;TCP acknowledges the concept of inter-process communication (IP doesn&amp;rsquo;t have that, it just cares about how to get messages from one &lt;strong&gt;host&lt;/strong&gt; to another). It is fundamentally based on connecting a &amp;ldquo;pair of processes&amp;rdquo; (if you are interested, &lt;a href=&#34;https://tools.ietf.org/html/rfc793&#34;&gt;RFC 793&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;Staying with the theme of providing services to upper layers, TCP offers:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Ensuring that the receiving process can keep up with the senders pace (&lt;strong&gt;Flow control&lt;/strong&gt;)&lt;/li&gt;
&lt;li&gt;Utilizing the underlying bandwidth without overloading the network (&lt;strong&gt;Congestion control&lt;/strong&gt;)&lt;/li&gt;
&lt;li&gt;Ordering (using ascending sequence numbers attached to each data segment)&lt;/li&gt;
&lt;li&gt;Reliability by data retransmission&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;These properties are what make TCP so popular, few people really want to think deeply about what happens when data segments are lost or reordered on the path over the network.&lt;/p&gt;

&lt;h4 id=&#34;bird-s-eye-view&#34;&gt;Bird&amp;rsquo;s eye view&lt;/h4&gt;

&lt;p&gt;For the rest of this post, I&amp;rsquo;d like to give you an impression how TCP is actually used by applications or upper-level protocols.&lt;/p&gt;

&lt;p&gt;To start communicating with another process, it is necessary to establish a TCP connection.
The end product of this is a connected pair of &lt;strong&gt;Sockets&lt;/strong&gt;, essentially an abstraction allowing to read and write data through the connection (you can check out my post on &lt;a href=&#34;https://blog.janfornoff.com/2018/01/20/introduction-to-linux-sockets/&#34;&gt;Linux Sockets&lt;/a&gt; if you want to know the details).
Symmetrically to the setup, there is a teardown sequence that is performed when you &lt;a href=&#34;https://linux.die.net/man/3/close&#34;&gt;&lt;code&gt;close&lt;/code&gt;&lt;/a&gt; a Socket on either side.&lt;/p&gt;

&lt;h4 id=&#34;buffering&#34;&gt;Buffering&lt;/h4&gt;

&lt;p&gt;The TCP implementations on both sides of the connections are keeping buffers for both transmission directions &amp;ndash; &lt;em&gt;receiving&lt;/em&gt; and &lt;em&gt;sending&lt;/em&gt;.
These are limiting the amount of data that can be:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;sent without the other side consuming it&lt;/li&gt;
&lt;li&gt;received without being consumed&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Applications send and receive data using &lt;strong&gt;system calls&lt;/strong&gt; (i.e., your application walks to the Linux kernel and asks it to &lt;a href=&#34;https://linux.die.net/man/2/send&#34;&gt;&lt;code&gt;send&lt;/code&gt;&lt;/a&gt; or &lt;a href=&#34;https://linux.die.net/man/2/recv&#34;&gt;&lt;code&gt;recv&lt;/code&gt;&lt;/a&gt; from the network). In between &lt;a href=&#34;https://linux.die.net/man/2/recv&#34;&gt;&lt;code&gt;recv&lt;/code&gt;&lt;/a&gt; calls, the Kernel buffers a finite amount of data in the Socket &lt;strong&gt;receive buffer&lt;/strong&gt;. If that buffer runs full, TCP&amp;rsquo;s &lt;strong&gt;flow control&lt;/strong&gt; mechanism allows indicating to the sending application to stop sending for now. The technical term for this sort of behavior is &lt;strong&gt;back pressure&lt;/strong&gt;. The interesting part about this is that the sending application doesn&amp;rsquo;t need to know about this behavior, the &lt;a href=&#34;https://linux.die.net/man/2/send&#34;&gt;&lt;code&gt;send&lt;/code&gt;&lt;/a&gt; system call just starts blocking until the data can be sent. &lt;a href=&#34;https://linux.die.net/man/2/recv&#34;&gt;&lt;code&gt;recv&lt;/code&gt;&lt;/a&gt; behaves similarly on an empty receive buffer &amp;ndash; it just blocks until it has some data to deliver.&lt;/p&gt;

&lt;h4 id=&#34;conclusion&#34;&gt;Conclusion&lt;/h4&gt;

&lt;p&gt;Summing up, TCP abstracts away the complications of dealing with an unreliable underlying network.
Using these provided services, applications and upper layers can just focus on sending and receiving data over a reliable channel.&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Introduction to the Internet Protocol</title>
      <link>https://blog.janfornoff.com/2018/05/27/introduction-to-the-internet-protocol/</link>
      <pubDate>Sun, 27 May 2018 19:05:48 +0000</pubDate>
      
      <guid>https://blog.janfornoff.com/2018/05/27/introduction-to-the-internet-protocol/</guid>
      <description>

&lt;p&gt;The Internet Protocol (IP) is a crucial underpinning of the Internet as we take it for granted nowadays.
To be completely honest with you, there is two IP protocols (there is the &amp;ldquo;old version&amp;rdquo;, IPv4 and the &amp;ldquo;new version&amp;rdquo;, IPv6). Both are standardised in the form of RFCs (&lt;a href=&#34;https://tools.ietf.org/html/rfc791&#34;&gt;IPv4&lt;/a&gt; and &lt;a href=&#34;https://tools.ietf.org/html/rfc8200&#34;&gt;IPv6&lt;/a&gt;). There are many more RFCs specifying details of the technologies, augmenting the base RFCs.&lt;/p&gt;

&lt;p&gt;However, this post is &lt;em&gt;not&lt;/em&gt; about technical details.&lt;/p&gt;

&lt;h4 id=&#34;what-this-post-is-not-about&#34;&gt;What this post is not about&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Differences between IPv4 and IPv6 (&lt;em&gt;Spoiler:&lt;/em&gt; v6 is pretty damn awesome.)&lt;/li&gt;
&lt;li&gt;How protocol headers look like &lt;br /&gt;
(It&amp;rsquo;s interesting but you can do it yourself, it&amp;rsquo;s just information in a predefined format.)&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&#34;what-this-post-is-about&#34;&gt;What this post IS about&lt;/h4&gt;

&lt;p&gt;Instead, this post goes over the Internet Protocol on a very high and abstract level: &lt;br /&gt;
How IP fits into the Internet protocol stack, the guarantees that it provides and how the OSI layers separate concerns.&lt;/p&gt;

&lt;h2 id=&#34;service-giving-and-taking&#34;&gt;Service: Giving and taking&lt;/h2&gt;

&lt;p&gt;IP resides at the &lt;strong&gt;Network Layer (Layer 3)&lt;/strong&gt; of the OSI stack.
The core idea of these layers is &lt;strong&gt;providing services&lt;/strong&gt; to an upper layer, and &lt;strong&gt;using services&lt;/strong&gt; provided by a lower layer.&lt;/p&gt;

&lt;p&gt;Lower layers know &lt;strong&gt;nothing&lt;/strong&gt; about how their services are used by upper layers. &lt;br /&gt;
Upper layers know &lt;strong&gt;nothing&lt;/strong&gt; about how exactly lower layers operate. &lt;br /&gt;
It does not concern them either &amp;ndash; it&amp;rsquo;s an agreed-upon contract of service delivery.&lt;/p&gt;

&lt;p&gt;The service that IP provides is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Addressing nodes on a network&lt;/li&gt;
&lt;li&gt;Forwarding and routing packets between nodes on the network&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In summary, IP provides upper layers with the service to send data to another node on the network without worrying about how to get the packet to the right place in the network topology.
The upper layer (usually the &lt;strong&gt;Transport Layer&lt;/strong&gt;, but there are exceptions) may be for instance TCP or UDP.&lt;/p&gt;

&lt;p&gt;IP in turn uses services of the &lt;strong&gt;Data Link Layer (Layer 2)&lt;/strong&gt;, which provides delivery of data frames within e.g., a LAN (local-area network) or WAN (wide-area network).
For the former, services on this level may be Ethernet or Wireless LAN.&lt;/p&gt;

&lt;p&gt;As such, each layer has a well defined responsibility, i.e.:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Transport Layer&lt;/strong&gt; knows about strategies of connecting pairs of processes (e.g., your web browser and a server) &amp;ndash; it may provide e.g., persistent connections, reliability and flow control.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Network Layer&lt;/strong&gt; knows how to navigate data from A to B within a network (the Transport Layer is completely oblivious of how to do that, it just assumes it can address someone and the packet will &amp;ndash; most likely &amp;ndash; make it there).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Data Link Layer&lt;/strong&gt; knows how to transport the data between nodes in a network (e.g., moving data between your Laptop and Home Router using WiFi or Ethernet).&lt;/p&gt;

&lt;h2 id=&#34;guarantees&#34;&gt;Guarantees&lt;/h2&gt;

&lt;p&gt;Now IP is a so-called &lt;strong&gt;best-effort protocol&lt;/strong&gt;. Plainly stated, this means: &lt;em&gt;&amp;ldquo;I can&amp;rsquo;t promise your packet will make it, but I&amp;rsquo;ll try, ok?&amp;rdquo;&lt;/em&gt;. Sending an IP datagram therefore does not guarantee that it arrives at its destination.&lt;/p&gt;

&lt;p&gt;Sending may fail for a multitude of reasons:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Very busy Routers on the network path being forced to discard packages (also called &amp;ldquo;Congestion&amp;rdquo;)&lt;/li&gt;
&lt;li&gt;The underlying physical medium causing the package to be delivered incompletely&lt;/li&gt;
&lt;li&gt;Packages being intentionally discarded by e.g., Firewalls or Routers&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;IP does not attempt any correction in case this happens. This is decided by the upper layers (e.g., TCP can retransmit lost segments of its data stream when the delivery seems to have failed).&lt;/p&gt;

&lt;h2 id=&#34;conclusion&#34;&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;This post attempts a new style of writing that focuses more on the broad architectural strokes of what the Internet is made up of. There is a lot of material on technical details. However, I personally feel like intuition for the underlying thought framework is way harder to find.&lt;/p&gt;

&lt;p&gt;Hope you found this useful, until next time.&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Book Notes: &#34;A Guide to the Good Life&#34;</title>
      <link>https://blog.janfornoff.com/2018/02/19/book-notes-a-guide-to-the-good-life/</link>
      <pubDate>Mon, 19 Feb 2018 06:59:49 +0000</pubDate>
      
      <guid>https://blog.janfornoff.com/2018/02/19/book-notes-a-guide-to-the-good-life/</guid>
      <description>

&lt;p&gt;This post will outline the first three of the core lessons I took from &amp;ldquo;A Guide to the Good Life&amp;rdquo; by William Irvine.&lt;/p&gt;

&lt;h1 id=&#34;tranquility-virtue&#34;&gt;Tranquility &amp;amp; Virtue&lt;/h1&gt;

&lt;p&gt;At the core of stoic philosophy stand the concepts of &lt;em&gt;Tranquility&lt;/em&gt; and &lt;em&gt;Virtue&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Tranquility&lt;/strong&gt; is described as the &amp;ldquo;psychological state marked by the &lt;em&gt;absence of negative emotions&lt;/em&gt;&amp;rdquo;, such as grief, anger or anxiety.
However, this is not a zombie-esque state of &amp;ldquo;not feeling anything&amp;rdquo;, but instead the presence of positive emotions, untainted by negative ones.&lt;/p&gt;

&lt;p&gt;Interestingly, Tranquility and Virtue are symbiotic. If you are not tranquil, your mind will be distracted by the negative emotions and
your judgement will be less reasonable. This in turn prevents you from moving towards Virtue. Your emotions triumph over your intellect.&lt;/p&gt;

&lt;h1 id=&#34;adversities&#34;&gt;Adversities&lt;/h1&gt;

&lt;p&gt;Challenges and adversities in life are reframed into a sparring-like context.
If fate hits you hard, you should regard it as a type of training situation.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Adversities we encounter in life are &lt;strong&gt;mere training&lt;/strong&gt;. These things that we shudder and tremble at are &lt;strong&gt;for the good&lt;/strong&gt; of the persons to whom they come.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We can observe how application of this thought changes emotional responses.
Instead of being frustrated and resenting an unfortunate event (note how these are all negative emotions), you shall view the broken Nutella jar and lost Love as an opportunity to practice your Virtue.&lt;/p&gt;

&lt;h1 id=&#34;negative-visualization&#34;&gt;Negative visualization&lt;/h1&gt;

&lt;p&gt;Contemplating bad things happening to you is powerful. Why?&lt;/p&gt;

&lt;p&gt;Firstly, and less importantly, it helps you be mentally prepared if fate strikes.
However, the more interesting effect of imagining yourself getting fucked over in every imaginable fashion, is that you start appreciating not being fucked in these ways.&lt;/p&gt;

&lt;p&gt;Let me elaborate.&lt;/p&gt;

&lt;p&gt;&lt;img src=&#34;https://blog.janfornoff.com/assets/img/stoicism/want.png&#34; alt=&#34;Want&#34; /&gt;&lt;/p&gt;

&lt;p&gt;Usually, things go a little like this: You have quite a lot of stuff. If you&amp;rsquo;re reading this, I&amp;rsquo;m assuming that you at least have access to the internet. But you&amp;rsquo;re pretty much used to it at this point. And chances are, you want something right now, would just love to have it! Nothing would make you happier.
Be it that slick new piece of tech gadget, that girl or boy next door looking so damn fine, a new pair of shoes.
And I&amp;rsquo;ll be bold and state that after you get it, &lt;strong&gt;you&amp;rsquo;ll eventually lose your appreciation for it and it&amp;rsquo;ll stop giving you joy.&lt;/strong&gt; This is natural, we adapt, even to what we have.&lt;/p&gt;

&lt;p&gt;We are also dopamine junkies (the stuff in your brain that gives you that rush when you get something new).
When our appreciation wears off and we grow accustomed to the thing we just wanted so much, we eventually want something new to give us a new rush. &lt;em&gt;We start chasing the next thing.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Stoics think that we can rejuvenate our passion for the things we already have by imagining how it would be to lose them.
This does not mean getting pessimistic or even fatalistic. This means realizing that you&amp;rsquo;re not entitled to have all these nice things.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;All we have is on loan from Fortune, which can reclaim it at any time, and without advance notice.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This lets us re-learn to appreciate what we have. By that, we can cut the cycle of wanting and chasing the next thing in pursuit of satisfaction short. It puts our first world problems into perspective.&lt;/p&gt;

&lt;p&gt;Again, observe how this lends itself to attaining &lt;em&gt;Tranquility&lt;/em&gt; by taking away the negative emotion of being unfulfilled because you don&amp;rsquo;t have the new iPhone X, and replacing it with the gratitude for having a phone that allows you to talk to your loved ones from anywhere.&lt;/p&gt;

&lt;h1 id=&#34;choosing-your-battles&#34;&gt;Choosing your battles&lt;/h1&gt;

&lt;p&gt;The core of the next lesson is categorizing things you do and face into three categories.&lt;/p&gt;

&lt;p&gt;&lt;img src=&#34;https://blog.janfornoff.com/assets/img/stoicism/control.png&#34; alt=&#34;Control&#34; /&gt;&lt;/p&gt;

&lt;p&gt;If you have &lt;strong&gt;complete control&lt;/strong&gt; over something, engage with it fully. No one can keep you from succeding but yourself.&lt;/p&gt;

&lt;p&gt;In contrast, if you have &lt;strong&gt;no control&lt;/strong&gt;, your actions have no impact on the outcome at all. All effort invested is wasted. Therefore, you should not be concerned with it.&lt;/p&gt;

&lt;p&gt;The category right between those, to me, is the most interesting.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Internal &amp;amp; external goals&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Suppose you&amp;rsquo;re really ambitious in playing a sport. You play a match, try really hard and still, &lt;strong&gt;you lose&lt;/strong&gt;. Pretty upsetting, right?
Notice what your frame of mind is when you focus on the outcome of winning. This outcome is not fully within your control. Your training efforts and how focused you are improve your chances of winning, but you cannot be sure. Call this an &lt;strong&gt;external goal&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;If you are really attached to being the winner, failing to win will be crushing. It will disrupt your &lt;em&gt;Tranquility&lt;/em&gt;.
Furthermore, if you&amp;rsquo;re behind on that match, you will probably get frustrated and play worse.&lt;/p&gt;

&lt;p&gt;Now contrast this mindset with someone who plays with the goal of playing to the best of his / her ability and trying to get better. This goal is way farther within your control. Call this an &lt;strong&gt;internal goal&lt;/strong&gt;. Even if you lose the game, you can attain your personal goal and retain your &lt;em&gt;Tranquility&lt;/em&gt;. Additionally, playing your best will support your chances of eventually getting to win!&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Internalizing&lt;/em&gt; your goals is advisable for matters that are not fully controllable by you.
This isolates you from the negative emotions of uncontrollable outcomes. Meanwhile, you move forward.&lt;/p&gt;

&lt;p&gt;Let&amp;rsquo;s wrap up for now, hope this was useful to you. I&amp;rsquo;ll see you on the other side.&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Introduction to Linux sockets</title>
      <link>https://blog.janfornoff.com/2018/01/20/introduction-to-linux-sockets/</link>
      <pubDate>Sat, 20 Jan 2018 11:14:58 +0000</pubDate>
      
      <guid>https://blog.janfornoff.com/2018/01/20/introduction-to-linux-sockets/</guid>
      <description>

&lt;p&gt;This post will cover a little bit about the abstraction of a Socket in Linuxworld, what they exactly are and how they are generally used.&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&#34;what-are-sockets-and-how-are-they-created&#34;&gt;What are Sockets and how are they created?&lt;/h2&gt;

&lt;p&gt;Sockets in the most general sense are an abstraction of interprocess communication. They represent the endpoint of a communication channel (abstracting the details of the underlying transport).&lt;/p&gt;

&lt;p&gt;To interact with Sockets, you use system calls. Those are commands that you send the Linux kernel who manages the communication and networking stack.&lt;/p&gt;

&lt;p&gt;Sockets are created by using the &lt;code&gt;socket()&lt;/code&gt; system call.
Let&amp;rsquo;s deconstruct this call by looking at it&amp;rsquo;s arguments.&lt;/p&gt;

&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-c&#34; data-lang=&#34;c&#34;&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;int&lt;/span&gt; socket(&lt;span style=&#34;color:#66d9ef&#34;&gt;int&lt;/span&gt; domain, &lt;span style=&#34;color:#66d9ef&#34;&gt;int&lt;/span&gt; type, &lt;span style=&#34;color:#66d9ef&#34;&gt;int&lt;/span&gt; protocol)&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h1 id=&#34;communication-domain&#34;&gt;Communication domain&lt;/h1&gt;

&lt;p&gt;The communication domain specifies a family of protocols that this Socket is going to live in.
Most prominently, Sockets can be &amp;ldquo;scoped&amp;rdquo; to the local machine (Unix socket) or the internet.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;UNIX domain (identified by the &lt;code&gt;AF_UNIX&lt;/code&gt; constant)&lt;/li&gt;
&lt;li&gt;IPv4 internet (&lt;code&gt;AF_INET&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;IPv6 internet (&lt;code&gt;AF_INET6&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;a bunch more, which can be found in &lt;a href=&#34;http://man7.org/linux/man-pages/man2/socket.2.html&#34;&gt;the man page&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&#34;socket-type&#34;&gt;Socket type&lt;/h1&gt;

&lt;p&gt;The Socket type specifies the communication semantics which are going to be in effect for this socket.
Let&amp;rsquo;s go through the most common examples:&lt;/p&gt;

&lt;h4 id=&#34;stream-sockets-sock-stream&#34;&gt;Stream sockets (&lt;code&gt;SOCK_STREAM&lt;/code&gt;)&lt;/h4&gt;

&lt;p&gt;Stream sockets provide what is called a &lt;em&gt;sequenced, reliable, two-way, connection-based byte stream&lt;/em&gt;.
Deconstructing this in terms of guarantees provided:
- &lt;strong&gt;Sequenced&lt;/strong&gt;: Messages are received in the same order in which they were sent
- &lt;strong&gt;Reliable&lt;/strong&gt;:
  - No duplication: The same message is received at most once
  - No creation: Messages are not received unless sent (there are no random messages appearing out of thin air)
  - Validity: When a process sends a message to another one (given that they&amp;rsquo;re correct and not buggy),
    it will eventually be received&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;two-way&lt;/strong&gt;: Both sides are able to send and receive&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;connection-based&lt;/strong&gt;: A session is established prior to data transfer&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The protocol in the Internet communication domain for this socket type is TCP.&lt;/p&gt;

&lt;h4 id=&#34;datagram-sockets-sock-dgram&#34;&gt;Datagram sockets (&lt;code&gt;SOCK_DGRAM&lt;/code&gt;)&lt;/h4&gt;

&lt;p&gt;A datagram is:
&amp;gt; A self-contained, independent entity of data carrying sufficient information to be routed from the source to the destination computer without reliance on earlier exchanges between this source and destination computer and the transporting network.
&amp;gt; &lt;a href=&#34;https://tools.ietf.org/html/rfc1594&#34;&gt;RFC1594&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Datagram sockets provide sending of &lt;em&gt;connectionless, unreliable messages of a fixed maximum length&lt;/em&gt;.
The protocol in the Internet communication domain for this socket type is UDP.&lt;/p&gt;

&lt;p&gt;There are more types of sockets, which can be found in &lt;a href=&#34;http://man7.org/linux/man-pages/man2/socket.2.html&#34;&gt;the man page&lt;/a&gt;&lt;/p&gt;

&lt;h1 id=&#34;socket-protocol&#34;&gt;Socket protocol&lt;/h1&gt;

&lt;p&gt;In most cases, the &lt;code&gt;int protocol&lt;/code&gt; is redundant, it is used when the communication domain has more than one protocol available for use (for example on &lt;code&gt;SOCK_RAW&lt;/code&gt; type sockets). Otherwise it can be nulled and ignored.&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&#34;connecting-sockets&#34;&gt;Connecting Sockets&lt;/h2&gt;

&lt;p&gt;Obviously, Sockets are only useful when there is another program on the other end of the communication channel.
So how do Sockets connect to each other?&lt;/p&gt;

&lt;p&gt;Most commonly, this happens by a Socket connecting to another Socket that is listening.
Let&amp;rsquo;s step through this by a TCP client-server example.&lt;/p&gt;

&lt;h4 id=&#34;server-side&#34;&gt;Server side&lt;/h4&gt;

&lt;p&gt;When a Server is starting up, it:
1. Instantiates a Socket through the &lt;a href=&#34;https://www.freebsd.org/cgi/man.cgi?socket(2)&#34;&gt;&lt;code&gt;socket()&lt;/code&gt;&lt;/a&gt; system call that we just saw.
2. Binds it to a name (in this case IP address and port) through &lt;a href=&#34;https://www.freebsd.org/cgi/man.cgi?bind(2)&#34;&gt;&lt;code&gt;bind()&lt;/code&gt;&lt;/a&gt;
3. Allows incoming connections on the Socket by calling &lt;a href=&#34;https://www.freebsd.org/cgi/man.cgi?listen(2)&#34;&gt;&lt;code&gt;listen()&lt;/code&gt;&lt;/a&gt;
4. The socket is now able to be connected to, essentially putting incoming connections into a queue
5. Calling &lt;a href=&#34;https://www.freebsd.org/cgi/man.cgi?accept(2)&#34;&gt;&lt;code&gt;accept()&lt;/code&gt;&lt;/a&gt; pulls connections from the queue and allocates a new Socket for them which is returned&lt;/p&gt;

&lt;h4 id=&#34;client-side&#34;&gt;Client side&lt;/h4&gt;

&lt;p&gt;When trying to establish a connection to a Server, the steps are:
1. Instantiating a Socket through &lt;code&gt;socket()&lt;/code&gt;, just like in the Server
2. Calling &lt;a href=&#34;https://www.freebsd.org/cgi/man.cgi?connect&#34;&gt;&lt;code&gt;connect()&lt;/code&gt;&lt;/a&gt; with the name / address that the Server socket was bound to (in &lt;code&gt;SOCK_STREAM&lt;/code&gt; sockets this attempts to initialize the session with the Server)
3. Sending some data (e.g., the HTTP request) to the server by using &lt;a href=&#34;https://www.freebsd.org/cgi/man.cgi?query=send&#34;&gt;&lt;code&gt;send()&lt;/code&gt;&lt;/a&gt; or similarly &lt;a href=&#34;https://www.freebsd.org/cgi/man.cgi?query=write&#34;&gt;&lt;code&gt;write()&lt;/code&gt;&lt;/a&gt; (which behaves the same, but you cannot set flags)
4. Reading a response using &lt;a href=&#34;https://www.freebsd.org/cgi/man.cgi?query=recv&#34;&gt;&lt;code&gt;recv()&lt;/code&gt;&lt;/a&gt; or &lt;a href=&#34;https://www.freebsd.org/cgi/man.cgi?query=read&amp;amp;sektion=2&#34;&gt;&lt;code&gt;read()&lt;/code&gt;&lt;/a&gt; - this call blocks (does not return) waiting unless the socket is set to use non-blocking I/O (by using &lt;a href=&#34;https://www.freebsd.org/cgi/man.cgi?query=fcntl&#34;&gt;&lt;code&gt;fcntl()&lt;/code&gt;&lt;/a&gt;)
5. When done interacting with the Server, &lt;a href=&#34;https://www.freebsd.org/cgi/man.cgi?query=close&#34;&gt;&lt;code&gt;close()&lt;/code&gt;&lt;/a&gt; is called on the Socket, which deletes it and closes the established connection.&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&#34;summing-up&#34;&gt;Summing up&lt;/h2&gt;

&lt;p&gt;Hope this has taught you a thing or two about Sockets and given you a bit of insight on how Sockets actually work close to the metal.&lt;/p&gt;

&lt;p&gt;Until next time!&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Documentation Driven Development - or why names matter</title>
      <link>https://blog.janfornoff.com/2017/12/09/documentation-driven-development-or-why-names-matter/</link>
      <pubDate>Sat, 09 Dec 2017 10:42:34 +0000</pubDate>
      
      <guid>https://blog.janfornoff.com/2017/12/09/documentation-driven-development-or-why-names-matter/</guid>
      <description>

&lt;p&gt;Today&amp;rsquo;s post will be a pitch for why you should be sensitive to how you refer to
concepts in your code and documentation.&lt;/p&gt;

&lt;p&gt;First statement: &lt;a href=&#34;http://adventofcode.com/2017&#34;&gt;Advent of Code&lt;/a&gt; is fun as heck and you should do it.&lt;/p&gt;

&lt;p&gt;While solving the Day 7 problem (&lt;a href=&#34;https://github.com/jfornoff/advent-of-code-2017/blob/master/dayseven/lib/dayseven.ex&#34;&gt;My solution here - Spoiler alert&lt;/a&gt;) I got stuck.
I knew what generally needed to happen, but couldn&amp;rsquo;t quite get it right.&lt;/p&gt;

&lt;p&gt;Everyone gets stuck, and people deal differently with this, I usually start scribbling on paper.
Today I wanted to try something different.&lt;/p&gt;

&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-elixir&#34; data-lang=&#34;elixir&#34;&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;@doc&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;&amp;#34;&amp;#34;
&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;lt;Your mental picture of the problem&amp;gt;
&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;&amp;#34;&amp;#34;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h3 id=&#34;problem-solving-is-verbal&#34;&gt;Problem solving is verbal&lt;/h3&gt;

&lt;p&gt;Especially when writing code, you need to name the concepts and abstractions you&amp;rsquo;re
working with. And if you&amp;rsquo;ve been writing source code for an extended period of time,
you will probably agree with me when I say that there are parts of code that just &amp;ldquo;click&amp;rdquo;
when you read them.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Yeah that&amp;rsquo;s obviously doing X.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Is it obvious though? Is it that the solved problem was just really simple that caused
you to sweep over the code without any mental friction?&lt;/p&gt;

&lt;p&gt;I&amp;rsquo;ll argue that it&amp;rsquo;s not. Instead, I&amp;rsquo;ll claim that the author of the code spent time
figuring out what the fragments of the problem should be called to give you the right
impression about what they&amp;rsquo;re there for and where they conceptually belong.&lt;/p&gt;

&lt;p&gt;Moving from this point, it&amp;rsquo;s conclusive that you&amp;rsquo;ll probably have a better time understanding
your own code &lt;em&gt;as you write it&lt;/em&gt; when you pay attention to what you call things.
Every time you have to step back from writing code and wonder &lt;strong&gt;Wait, is this right? What was I trying to do again?&lt;/strong&gt;, this will yield dividends. You can re-read where you came from and pick back up with clarity.&lt;/p&gt;

&lt;p&gt;Now the fitting name usually won&amp;rsquo;t pop into your head right away.
Here&amp;rsquo;s where writing documentation (even before the code exists) can help you map out what&amp;rsquo;s actually going on with the problem.&lt;/p&gt;

&lt;h3 id=&#34;document-to-establish-vocabulary&#34;&gt;Document to establish vocabulary&lt;/h3&gt;

&lt;p&gt;When you read through a Jira ticket or someone tells you about a problem that&amp;rsquo;s been bugging them,
you subconsciously start mentally modelling the pieces of data you&amp;rsquo;re dealing with
and already start sketching out what the rough solution steps would be.&lt;/p&gt;

&lt;p&gt;If the problem has any appreciable degree of complexity, you will most likely not get it right by just eyeballing the solution approach.&lt;/p&gt;

&lt;p&gt;This is where it helps to just write down what you conceptually want to do.&lt;/p&gt;

&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-elixir&#34; data-lang=&#34;elixir&#34;&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;@doc&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;&amp;#34;&amp;#34;
&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;This parses tree nodes with their child node names from the input string and
&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;rearranges them into a traversable Tree data structure,
&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;represented by the root node containing its child subtrees.
&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;&amp;#34;&amp;#34;&lt;/span&gt;

def build_tree(input_string) do
end&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;From here, it is pretty straightforward to &amp;ldquo;Write the code you wish you had&amp;rdquo; (which is a saying often used in the TDD and BDD community, stating that you can &lt;strong&gt;dream up&lt;/strong&gt; how ideal high-level code should look and not care about the underlying implementation yet)&lt;/p&gt;

&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-elixir&#34; data-lang=&#34;elixir&#34;&gt;def build_tree(input_string) do
  input_string
  &lt;span style=&#34;color:#f92672&#34;&gt;|&amp;gt;&lt;/span&gt; parse_nodes()
  &lt;span style=&#34;color:#f92672&#34;&gt;|&amp;gt;&lt;/span&gt; arrange_nodes_into_tree()
end&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;From here, you can drill down and start wondering &amp;ldquo;What do I need to do to parse this string into a list of nodes?&amp;rdquo;&lt;/p&gt;

&lt;p&gt;Also note how the documentation-writing also eased us into establishing the core concepts of
our solution model. This is the vocabulary of your solution. &lt;strong&gt;Do not deviate from it, decide!&lt;/strong&gt;&lt;/p&gt;

&lt;h3 id=&#34;conclusion&#34;&gt;Conclusion&lt;/h3&gt;

&lt;p&gt;Taking your hands off the keyboard for 10 minutes and figuring out in which terms you want to
reason about the problem at hand is not unproductive.&lt;/p&gt;

&lt;p&gt;If you feel like the text on your screen is gibberish, try to descriptively state
what this code is trying to do, find out what&amp;rsquo;s clouding the clarity and rename something for the better.&lt;/p&gt;

&lt;p&gt;Farewell!&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Continuous deployment for your CV</title>
      <link>https://blog.janfornoff.com/2017/11/03/continuous-deployment-for-your-cv/</link>
      <pubDate>Fri, 03 Nov 2017 18:18:37 +0000</pubDate>
      
      <guid>https://blog.janfornoff.com/2017/11/03/continuous-deployment-for-your-cv/</guid>
      <description>

&lt;p&gt;The goal of today&amp;rsquo;s post is to get a little whiff of Continuous Deployment,
especially build pipelines.&lt;/p&gt;

&lt;p&gt;First off, I need you to realize that sending CVs by Email is not acceptable, because this is 2017. Are we on the same page? OK.&lt;/p&gt;

&lt;p&gt;We&amp;rsquo;re going to put our CV into version control and build a PDF out of it as soon as we push a new commit.&lt;/p&gt;

&lt;h1 id=&#34;frequently-asked-questions&#34;&gt;Frequently Asked Questions&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;Is this necessary? &lt;strong&gt;No.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Are we doing it anyway? &lt;strong&gt;Yes.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Why? &lt;strong&gt;Firstly, because we can. Secondly, because it teaches us about automation.&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&#34;the-plan&#34;&gt;The Plan&lt;/h1&gt;

&lt;p&gt;What are we specifically trying to achieve?
- Being able to change our published CV by pushing a new commit to a Git repository
- Having a stable URL at hand that always refers to the latest and greatest version of our CV&lt;/p&gt;

&lt;p&gt;Secondary priorities:
- Not having to host anything ourselves (not because we can&amp;rsquo;t, but because it&amp;rsquo;s not necessary and less effort)&lt;/p&gt;

&lt;h1 id=&#34;automatic-building-of-pdf&#34;&gt;Automatic building of PDF&lt;/h1&gt;

&lt;p&gt;Our goal is to push a Git commit and somehow get a PDF out of it.&lt;/p&gt;

&lt;p&gt;To keep it super simple, we are looking for a Git repository provider offering build pipelines so we don&amp;rsquo;t have to wire up Hooks to trigger build jobs. I considered &lt;a href=&#34;https://bitbucket.org/&#34;&gt;Bitbucket&lt;/a&gt; and &lt;a href=&#34;https://gitlab.com&#34;&gt;Gitlab&lt;/a&gt;. Eventually chose Gitlab since Bitbucket severely limits the amount of available build time and makes public downloads harder.&lt;/p&gt;

&lt;p&gt;Build pipelines are declarations of what needs to happen to build something. Usually they run automatically when a new push a Git repository happened. Our Gitlab Pipeline will do exactly that for us, build us a PDF whenever we push.&lt;/p&gt;

&lt;p&gt;Here is a Gitlab pipeline definition.
&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-yaml&#34; data-lang=&#34;yaml&#34;&gt;stages:
  - build

compile-cv:
  stage: build
  image: blang/latex
  script:
    - bash build-cv.sh
  artifacts:
    paths:
      - &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;build/CV.pdf&amp;#34;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Quick walkthrough:&lt;/strong&gt;
1. &lt;code&gt;stages&lt;/code&gt; just allows grouping your pipeline into stages - what a shocker.
   That&amp;rsquo;s pretty cool for running things that don&amp;rsquo;t depend on each other in parallel.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;code&gt;compile-cv&lt;/code&gt; is a &amp;ldquo;step&amp;rdquo; in the build that is run in a Docker container

&lt;ul&gt;
&lt;li&gt;based on the Docker image &lt;code&gt;blang/latex&lt;/code&gt;, which provides a LaTeX build environment&lt;/li&gt;
&lt;li&gt;running a shell script that basically just executes &lt;code&gt;pdflatex&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;defining the &lt;code&gt;pdflatex&lt;/code&gt; result - &lt;code&gt;build/CV.pdf&lt;/code&gt; - as an &lt;code&gt;artifact&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href=&#34;https://docs.gitlab.com/ce/user/project/pipelines/job_artifacts.html&#34;&gt;Job artifacts&lt;/a&gt; in Gitlab pipelines are super neat, since they are downloadable, and allow us to link to the most recent artifact under a stable URL!&lt;/p&gt;

&lt;h1 id=&#34;making-the-pdf-publically-available&#34;&gt;Making the PDF publically available&lt;/h1&gt;

&lt;p&gt;This pretty much just requires you to set the Gitlab repository visibility to &lt;code&gt;public&lt;/code&gt;. I&amp;rsquo;m sure you&amp;rsquo;ll figure out how to do that if you really want to.&lt;/p&gt;

&lt;p&gt;Eventually, you will be able to call an URL that looks a bit like this, and get a PDF download (&lt;strong&gt;Rockstar developer tip:&lt;/strong&gt; Log out of Gitlab to see if it is actually publically available).
&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-html&#34; data-lang=&#34;html&#34;&gt;https://gitlab.com/jfornoff/cv/-/jobs/artifacts/master/raw/build/CV.pdf?job=compile-cv&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;

&lt;p&gt;Obviously your URL will vary, the general scheme is:
&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-html&#34; data-lang=&#34;html&#34;&gt;https://gitlab.com/&amp;lt;&lt;span style=&#34;color:#f92672&#34;&gt;namespace&lt;/span&gt;&amp;gt;/&amp;lt;&lt;span style=&#34;color:#f92672&#34;&gt;project&lt;/span&gt;&amp;gt;/-/jobs/artifacts/&amp;lt;&lt;span style=&#34;color:#f92672&#34;&gt;ref&lt;/span&gt;&amp;gt;/raw/&amp;lt;&lt;span style=&#34;color:#f92672&#34;&gt;path_to_file&lt;/span&gt;&amp;gt;?job=&amp;lt;&lt;span style=&#34;color:#f92672&#34;&gt;job_name&lt;/span&gt;&amp;gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;

&lt;h1 id=&#34;getting-a-pretty-url&#34;&gt;Getting a pretty URL&lt;/h1&gt;

&lt;p&gt;This URL is totally not memorable and you&amp;rsquo;ll need to look it up, we can do better.&lt;/p&gt;

&lt;p&gt;I&amp;rsquo;ll give you two options that might work for you:&lt;/p&gt;

&lt;h4 id=&#34;1-url-shortener&#34;&gt;1. URL shortener&lt;/h4&gt;

&lt;p&gt;If you just want the quick-and-not-quite-nice version of the publically available, Continuous Deployment, DevOps, Microservices, Cloud, Disruption CV, then just go for something like &lt;a href=&#34;https://tinyurl.com/&#34;&gt;TinyUrl&lt;/a&gt;, yielding something like:
&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-html&#34; data-lang=&#34;html&#34;&gt;https://tinyurl.com/jfornoff-cv&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;

&lt;h4 id=&#34;2-using-your-own-domain&#34;&gt;2. Using your own domain&lt;/h4&gt;

&lt;p&gt;Serving files directly from the Gitlab artifact under a nice URL usually requires you to run a HTTP server somewhere, redirecting or URL rewriting to your artifact URL.&lt;/p&gt;

&lt;p&gt;You say that&amp;rsquo;s too much effort? I agree.
After some digging around, I stumbled upon &lt;a href=&#34;http://redirect.name/&#34;&gt;redirect.name&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;It allows you to configure a DNS domain to use &lt;code&gt;TXT&lt;/code&gt; entries for routing redirections, they host everything for you.&lt;/p&gt;

&lt;p&gt;Assume you own &lt;code&gt;greatdomain.com&lt;/code&gt;, and would like to have &lt;code&gt;cv.greatdomain.com&lt;/code&gt; be the link to your hosted CV.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In this case, you would set a so-called &lt;code&gt;CNAME&lt;/code&gt; record to make &lt;code&gt;cv.greatdomain.com&lt;/code&gt; resolve to a &lt;code&gt;redirect.name&lt;/code&gt; IP address (since we want them to take care of the redirection).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Two-sentence intro to DNS and &lt;code&gt;CNAME&lt;/code&gt; records:&lt;/strong&gt;
- DNS associates (&amp;ldquo;resolves&amp;rdquo;) &lt;code&gt;google.com&lt;/code&gt; (hostname) to &lt;code&gt;172.217.22.78&lt;/code&gt; (IP address)
- &lt;code&gt;CNAME&lt;/code&gt; tells a DNS lookup &amp;ldquo;but our princess is in another castle - here is another hostname to resolve to get an actual IP address&amp;rdquo; (Sorry.)&lt;/p&gt;

&lt;p&gt;Here&amp;rsquo;s how it would look:
&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-dns&#34; data-lang=&#34;dns&#34;&gt;cv.greatdomain.com.      &amp;lt;TTL&amp;gt;   IN      CNAME   alias.redirect.name.&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;redirect.name&lt;/code&gt; does not know how to redirect yet. We tell them in a &lt;code&gt;TXT&lt;/code&gt; DNS record.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;One-sentence intro to &lt;code&gt;TXT&lt;/code&gt; records:&lt;/strong&gt; There&amp;rsquo;s nothing to know, they just allow you to store some arbitrary text under a certain hostname. &lt;code&gt;redirect.name&lt;/code&gt; uses it to read your routing definitions from there.&lt;/p&gt;

&lt;p&gt;That would give you something along the lines of this:
&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-dns&#34; data-lang=&#34;dns&#34;&gt;_redirect.cv.greatdomain.com. &amp;lt;TTL&amp;gt; IN   TXT     &amp;#34;Redirects to https://gitlab.com/gitlabuser/my-cv/-/jobs/artifacts/master/raw/build/CV.pdf?job=compile-cv&amp;#34;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;

&lt;p&gt;Now you can visit &lt;code&gt;cv.greatdomain.com&lt;/code&gt;, and you get redirected to your downloadable PDF hosted in Gitlab!&lt;/p&gt;

&lt;h2 id=&#34;conclusion&#34;&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;I&amp;rsquo;ll be honest, this didn&amp;rsquo;t save any time, probably not even in the long run. However, it shows how to be deliberately intolerant to repetitive manual steps, which I personally consider a productive habit.&lt;/p&gt;

&lt;p&gt;Hope you liked it and until next time!&lt;/p&gt;

&lt;h2 id=&#34;appendix&#34;&gt;Appendix&lt;/h2&gt;

&lt;p&gt;If I handwaved over something, you can find all the pipeline code for building a CV from LaTeX &lt;a href=&#34;https://gitlab.com/jfornoff/cv&#34;&gt;in my public CV repository&lt;/a&gt;.&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Mix xref to the rescue!</title>
      <link>https://blog.janfornoff.com/2017/10/28/mix-xref-to-the-rescue/</link>
      <pubDate>Sat, 28 Oct 2017 10:15:04 +0000</pubDate>
      
      <guid>https://blog.janfornoff.com/2017/10/28/mix-xref-to-the-rescue/</guid>
      <description>

&lt;p&gt;&lt;a href=&#34;{% post_url 2017-10-21-getting-started-with-mix-format %}&#34;&gt;Last week&lt;/a&gt;, i told you about the soon-included code formatter in the standard Elixir build tool &lt;a href=&#34;https://hexdocs.pm/mix/Mix.html&#34;&gt;&lt;strong&gt;Mix&lt;/strong&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Today, we&amp;rsquo;re continuing our journey into the amazing things that Mix can do for you.
There will be three features that I&amp;rsquo;m going to spotlight, just skip over whatever section
if you already know it. This is a &lt;strong&gt;What the hell, this is a thing? How do I use that?&lt;/strong&gt; type of post.&lt;/p&gt;

&lt;h2 id=&#34;short-intro-to-mix-xref&#34;&gt;Short intro to mix xref&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;mix xref&lt;/code&gt; is a task that is able to analyze relationships between your Elixir modules.
Sounds really boring, huh? It does. Let me show you why it&amp;rsquo;s actually amazing.&lt;/p&gt;

&lt;h2 id=&#34;finding-dead-end-function-calls&#34;&gt;Finding dead end function calls&lt;/h2&gt;

&lt;p&gt;Did you ever fat finger a function name, run the code and get greeted with:
&lt;strong&gt;HEY I&amp;rsquo;M YOUR RUNTIME ENVIRONMENT AND I TOTALLY CAN&amp;rsquo;T FIND THE THING THAT WE&amp;rsquo;RE TRYING TO CALL HERE&lt;/strong&gt;?&lt;/p&gt;

&lt;p&gt;This also assumes that you&amp;rsquo;re actually running the code locally (hope you have a good test suite that exercises your code base, otherwise&amp;hellip; I feel sorry for you.) before shipping it. Anyways, besides the point. Imagine a world, where your compiler could tell you exactly that this is not going to work, quickly and for your entire code base!&lt;/p&gt;

&lt;p&gt;Meet &lt;code&gt;mix xref unreachable&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Assume the following project.&lt;/p&gt;

&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-elixir&#34; data-lang=&#34;elixir&#34;&gt;defmodule &lt;span style=&#34;color:#a6e22e&#34;&gt;Greeter&lt;/span&gt; do
  def hello(name) do
    &lt;span style=&#34;color:#a6e22e&#34;&gt;IO&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;put &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Hello &lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;#{&lt;/span&gt;name&lt;span style=&#34;color:#e6db74&#34;&gt;}&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;&lt;/span&gt;
  end
end&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Eagle eye, can you spot the error? Did you make one like these before? I have.&lt;/p&gt;

&lt;p&gt;Now let&amp;rsquo;s see if we can get some feedback from our nice buildtool.
&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-shell&#34; data-lang=&#34;shell&#34;&gt;$ mix xref unreachable
lib/foo.ex:3: IO.put/1&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;

&lt;p&gt;That&amp;rsquo;s the short, machine-readable version (imagine if your editor would run this automatically and show you an indicator in the code whenever something like this happens! Hmm&amp;hellip;)&lt;/p&gt;

&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-shell&#34; data-lang=&#34;shell&#34;&gt;$ mix xref warnings
warning: &lt;span style=&#34;color:#66d9ef&#34;&gt;function&lt;/span&gt; IO.put/1 is undefined or private. Did you mean one of:

      * puts/1
      * puts/2

  lib/foo.ex:3&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Ever wondered how these warnings are generated when you compile all your code? Yup. From this &lt;code&gt;mix&lt;/code&gt; task.&lt;/p&gt;

&lt;h2 id=&#34;running-only-the-tests-you-care-about&#34;&gt;Running only the tests you care about&lt;/h2&gt;

&lt;p&gt;Programming efficiently is a lot about feedback loops. The quicker you find out that
the code you are currently writing does not quite do what you think it does, the quicker
you can move on with your life.&lt;/p&gt;

&lt;p&gt;Here comes &lt;code&gt;mix test --stale&lt;/code&gt;.
It works by keeping track of changes in your code since the last test run and determining the tests that could have been possibly impacted. Then it just runs those.&lt;/p&gt;

&lt;p&gt;This become especially important when your test suite grows, maybe even spanning multiple sub-applications in an Umbrella app. It&amp;rsquo;s already well supported in TDD workflow tools such as &lt;a href=&#34;https://github.com/lpil/mix-test.watch&#34;&gt;mix test.watch&lt;/a&gt;, which supports a &lt;code&gt;--stale&lt;/code&gt; flag.&lt;/p&gt;

&lt;h2 id=&#34;where-s-that-function-used&#34;&gt;Where&amp;rsquo;s that function used?&lt;/h2&gt;

&lt;p&gt;Ever stumbled upon a function that you really wondered what it does? Chances are that you&amp;rsquo;d be interested in where it is used, to figure out what parts of the code base are using it - to get some context.&lt;/p&gt;

&lt;p&gt;It&amp;rsquo;s a lot like &lt;strong&gt;Jump to Definition&lt;/strong&gt;, just the other way around!&lt;/p&gt;

&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-shell&#34; data-lang=&#34;shell&#34;&gt;$ mix xref callers Kernel.&lt;span style=&#34;color:#f92672&#34;&gt;==&lt;/span&gt;/2
lib/example/compare.ex:84: Kernel.&lt;span style=&#34;color:#f92672&#34;&gt;==&lt;/span&gt;/2&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Neat! If you think this example is contrived, consider that you&amp;rsquo;re upgrading to a new major version of your favorite library and they changed some naming around. Run &lt;code&gt;callers&lt;/code&gt; for the renamed function or module, walk through the call sites and make the necessary change. As soon &lt;code&gt;callers&lt;/code&gt; says you&amp;rsquo;re clear, no need to wonder - &lt;strong&gt;can I commit this or do i need to double check again?&lt;/strong&gt;&lt;/p&gt;

&lt;h2 id=&#34;conclusion&#34;&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;I&amp;rsquo;ll let you go now. Hope this post taught you something about how Mix can make your life easier or showed you a cool use of &lt;code&gt;xref&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Until next time.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;P.S.&lt;/strong&gt; Be aware that &lt;code&gt;mix xref&lt;/code&gt; is also able to output &lt;strong&gt;all&lt;/strong&gt; the relationships between your application modules (e.g. in graphical format) - it&amp;rsquo;ll come back to you in case you ever need it.&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Getting started with mix format</title>
      <link>https://blog.janfornoff.com/2017/10/21/getting-started-with-mix-format/</link>
      <pubDate>Sat, 21 Oct 2017 10:13:25 +0000</pubDate>
      
      <guid>https://blog.janfornoff.com/2017/10/21/getting-started-with-mix-format/</guid>
      <description>

&lt;h2 id=&#34;what-s-code-formatting-and-why-do-we-care&#34;&gt;What&amp;rsquo;s code formatting and why do we care?&lt;/h2&gt;

&lt;p&gt;On my past job, I had the pleasure of writing quite a bit of &lt;a href=&#34;http://elm-lang.org/&#34;&gt;Elm&lt;/a&gt; (which has the most outstanding developer experience that I&amp;rsquo;ve ever seen and is going to take over the frontend eventually, I hope).&lt;/p&gt;

&lt;p&gt;One feature about the Elm workflow that really stood out to me, is that you:
- Write some code that you think should work
- Hit &lt;strong&gt;Save&lt;/strong&gt;
- Suddenly your reedit-problem-solving-jumbled code looks perfectly fine!&lt;/p&gt;

&lt;p&gt;This does not sound like a big deal at first, but I invite you to try it for a day and see if you ever want to go back. And in my understanding, it&amp;rsquo;s not even about saved keystrokes, but about the mental context switches you&amp;rsquo;re not making.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;OK, that works, time to make it looks nice, and then I can move on with the next step&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Just imagine deleting this step from the equation. If you are a developer, I don&amp;rsquo;t think I have to argue to you that suspending your current train of thought does not help with solving the problem at hand. Therefore, I believe it won&amp;rsquo;t be hard for you to agree that reindenting lines is not what makes you happy or valuable to the world.&lt;/p&gt;

&lt;h2 id=&#34;code-formatting-in-elixir&#34;&gt;Code formatting in Elixir&lt;/h2&gt;

&lt;p&gt;Fortunately, José Valim and the amazing Elixir core team seem to agree with me and made code formatting a first-class citizen in the upcoming release 1.6 of Elixir.&lt;/p&gt;

&lt;p&gt;At the time of writing this post, version 1.6 is not out yet.
If you want to try it today, I recommend just running the &lt;code&gt;master&lt;/code&gt; branch of Elixir.&lt;/p&gt;

&lt;h4 id=&#34;setup&#34;&gt;Setup&lt;/h4&gt;

&lt;p&gt;For all Linux and MacOS users, I recommend using &lt;a href=&#34;https://github.com/asdf-vm/asdf&#34;&gt;the ASDF version manager&lt;/a&gt; and it&amp;rsquo;s excellent &lt;a href=&#34;https://github.com/asdf-vm/asdf-elixir&#34;&gt;Elixir plugin&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;From there, you can run:
&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# Install the current master version of Elixir, to update just reinstall it&lt;/span&gt;
asdf install elixir master-otp-20

&lt;span style=&#34;color:#75715e&#34;&gt;# Use edge Elixir as the global default&lt;/span&gt;
asdf global elixir master-otp-20

&lt;span style=&#34;color:#75715e&#34;&gt;# Or just for the project that you&amp;#39;re currently in&lt;/span&gt;
asdf local elixir master-otp-20&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;

&lt;p&gt;And have access to &lt;code&gt;mix format&lt;/code&gt;, which runs the code formatter.&lt;/p&gt;

&lt;h3 id=&#34;format-on-save&#34;&gt;Format-on-save&lt;/h3&gt;

&lt;p&gt;In my book, this is the most important use case and the setup on your editor of choice is going to vary, but any self-respecting editor has some way of running commands on save or on a keyboard shortcut.&lt;/p&gt;

&lt;p&gt;There&amp;rsquo;s even some plugins that take that configuration from you:
- &lt;a href=&#34;https://github.com/rgreenjr/atom-elixir-formatter&#34;&gt;Atom&lt;/a&gt;
- &lt;a href=&#34;https://github.com/sbdchd/neoformat&#34;&gt;Neoformat for VIM&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Side note&lt;/strong&gt; If you happen to set this up by yourself, most formatting frameworks support piping the file contents to the formatter via &lt;code&gt;STDIN&lt;/code&gt; and reading the formatted output from &lt;code&gt;STDOUT&lt;/code&gt;, which makes for snappier formatting. This is supported by &lt;code&gt;mix format -&lt;/code&gt;.&lt;/p&gt;

&lt;h3 id=&#34;whole-project-formatting&#34;&gt;Whole-project formatting&lt;/h3&gt;

&lt;p&gt;To do this, you can either pass a pattern like:
&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;mix format &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;lib/**/*.{ex,exs}&amp;#34;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;

&lt;p&gt;However, I&amp;rsquo;d recommend adding a &lt;code&gt;.formatter.exs&lt;/code&gt; file to your project root (that&amp;rsquo;s where &lt;code&gt;mix format&lt;/code&gt; is looking for it by default, but you can put it wherever you want. That&amp;rsquo;s what she said.):
&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-elixir&#34; data-lang=&#34;elixir&#34;&gt;[
  &lt;span style=&#34;color:#e6db74&#34;&gt;inputs&lt;/span&gt;: [
    &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;mix.exs&amp;#34;&lt;/span&gt;,
    &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;{config,lib,test}/**/*.{ex,exs}&amp;#34;&lt;/span&gt;
  ]
]&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;

&lt;p&gt;This enables you to just run &lt;code&gt;mix format&lt;/code&gt; with no arguments to reformat all the code in the specified paths.&lt;/p&gt;

&lt;p&gt;That&amp;rsquo;s all I got for now, hopefully my little essay persuaded you to give automated code formatting a try, if not in Elixir, then maybe in your language of choice. Until next time.&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Elixir Macros Part 2 - How to find out what&#39;s going on</title>
      <link>https://blog.janfornoff.com/elixir-macros-2/</link>
      <pubDate>Sun, 15 Oct 2017 10:07:32 +0000</pubDate>
      
      <guid>https://blog.janfornoff.com/elixir-macros-2/</guid>
      <description>

&lt;p&gt;In &lt;a href=&#34;{% post_url 2017-10-03-elixir-macros-part1 %}&#34;&gt;part 1&lt;/a&gt;, we looked into how Elixir macros work and how they allow us to &lt;strong&gt;write code that writes code&lt;/strong&gt;, as many say.&lt;/p&gt;

&lt;p&gt;One drawback of using macros is that they are hiding complexity, making it less obvious what exactly a given piece of code is doing. Today, we&amp;rsquo;ll figure out how to dive into libraries and track down what macros are doing under the hood.&lt;/p&gt;

&lt;p&gt;To make this as realistic as possible, we&amp;rsquo;ll use an example from real library (&lt;a href=&#34;https://hexdocs.pm/plug/readme.html&#34;&gt;&lt;code&gt;Plug&lt;/code&gt;&lt;/a&gt;). In a nutshell, &lt;code&gt;Plug&lt;/code&gt; provides abstractions to deal with HTTP requests and responding to them.&lt;/p&gt;

&lt;h2 id=&#34;whirlwind-introduction-to-plug&#34;&gt;Whirlwind introduction to Plug&lt;/h2&gt;

&lt;p&gt;To understand what &lt;code&gt;Plug&lt;/code&gt; does, you just need to understand two concepts.
Firstly, the concept of a &lt;code&gt;Plug.Conn&lt;/code&gt; to represent an incoming request being processed, and secondly the &lt;code&gt;Plug&lt;/code&gt; itself, which represents a processing step on a &lt;code&gt;Plug.Conn&lt;/code&gt;.&lt;/p&gt;

&lt;h4 id=&#34;the-conn-data-structure&#34;&gt;The Conn data structure&lt;/h4&gt;

&lt;p&gt;When you consider a HTTP request entering your application, there is a bunch of data that you care about. Starting from the HTTP verb (e.g., &lt;code&gt;GET&lt;/code&gt; / &lt;code&gt;POST&lt;/code&gt;), over HTTP headers (e.g. &lt;code&gt;Authorization&lt;/code&gt; to check which user made the request), to the request body.&lt;/p&gt;

&lt;p&gt;&lt;a href=&#34;https://hexdocs.pm/plug/Plug.Conn.html&#34;&gt;&lt;code&gt;%Plug.Conn{}&lt;/code&gt;&lt;/a&gt; is an Elixir struct that encapsulates all this data.
Processing of requests is then done by routing this data structure through a sequence of processing steps (those steps are called &lt;code&gt;Plugs&lt;/code&gt;), fabricating the response.&lt;/p&gt;

&lt;h4 id=&#34;plugs-as-a-way-of-transforming-the-conn&#34;&gt;Plugs as a way of transforming the Conn&lt;/h4&gt;

&lt;p&gt;As we just touched on, the &lt;code&gt;%Plug.Conn{}&lt;/code&gt; also contains data about how the HTTP response should look like, e.g., the HTTP response code and response body.&lt;/p&gt;

&lt;p&gt;A &lt;code&gt;Plug&lt;/code&gt; is literally just a definition of a transformation step, either in module form (&lt;code&gt;ModulePlug.call(conn, opts)&lt;/code&gt;) or function form (&lt;code&gt;functionPlug(conn, opts))&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;Now let&amp;rsquo;s imagine a MVC scenario where an incoming request hits a router, gets dispatched to a controller action and eventually is answered by the server, maybe with a rendered HTML view or JSON response.&lt;/p&gt;

&lt;p&gt;Conceptually, the running web server implementation will receive the HTTP request, wrap it into its &lt;code&gt;%Plug.Conn{}&lt;/code&gt; representation, and pass it to the router implementation given in it&amp;rsquo;s configuration.&lt;/p&gt;

&lt;p&gt;Now for the really interesting part: This router is a &lt;code&gt;Plug&lt;/code&gt;. It receives a &lt;code&gt;%Plug.Conn{}&lt;/code&gt;, transforms it and returns a &lt;code&gt;%Plug.Conn{}&lt;/code&gt;. That&amp;rsquo;s all that &lt;code&gt;Plug&lt;/code&gt;s ever do.
Calling our router might look as simple as:&lt;/p&gt;

&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-elixir&#34; data-lang=&#34;elixir&#34;&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;AppRouter&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;call(conn, additional_router_options)&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The router may then, as a part of its processing, match on the request&amp;rsquo;s HTTP verb and path and delegate to a controller. Guess what that controller is. Yuuuuuup, surprise, it&amp;rsquo;s a &lt;code&gt;Plug&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Congratulations, you now know everything there is to know about &lt;code&gt;Plug&lt;/code&gt;.&lt;/p&gt;

&lt;h2 id=&#34;deconstructing-the-plug-router&#34;&gt;Deconstructing the Plug router&lt;/h2&gt;

&lt;p&gt;Here&amp;rsquo;s a very simple router definition (borrowed straight from &lt;a href=&#34;https://hexdocs.pm/plug/Plug.Router.html&#34;&gt;the excellent &lt;code&gt;Plug.Router&lt;/code&gt; documentation&lt;/a&gt;).&lt;/p&gt;

&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-elixir&#34; data-lang=&#34;elixir&#34;&gt;defmodule &lt;span style=&#34;color:#a6e22e&#34;&gt;AppRouter&lt;/span&gt; do
  use &lt;span style=&#34;color:#a6e22e&#34;&gt;Plug.Router&lt;/span&gt;

  plug &lt;span style=&#34;color:#e6db74&#34;&gt;:match&lt;/span&gt;
  plug &lt;span style=&#34;color:#e6db74&#34;&gt;:dispatch&lt;/span&gt;

  get &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;/hello&amp;#34;&lt;/span&gt; do
    send_resp(conn, &lt;span style=&#34;color:#ae81ff&#34;&gt;200&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;world&amp;#34;&lt;/span&gt;)
  end

  match _ do
    send_resp(conn, &lt;span style=&#34;color:#ae81ff&#34;&gt;404&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;oops&amp;#34;&lt;/span&gt;)
  end
end&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;I won&amp;rsquo;t insult your intelligence by reiterating what this router does.
Rather, I&amp;rsquo;ll tell you how to figure out how this code functions.&lt;/p&gt;

&lt;h4 id=&#34;what-s-that-plug-thingie-and-how-does-it-exactly-work&#34;&gt;What&amp;rsquo;s that &lt;code&gt;plug&lt;/code&gt; thingie and how does it exactly work?&lt;/h4&gt;

&lt;p&gt;At the time of typing these words, &lt;em&gt;I don&amp;rsquo;t know either&lt;/em&gt;. Let&amp;rsquo;s find out.&lt;/p&gt;

&lt;p&gt;To start, we might wonder if the &lt;code&gt;plug&lt;/code&gt; keyword is part of Elixir itself (&lt;strong&gt;Spoiler&lt;/strong&gt;: It&amp;rsquo;s not.). Excellent. Apparently this is a macro. The next question we might ask is: &amp;ldquo;Where is this coming from?&amp;rdquo;&lt;/p&gt;

&lt;p&gt;Generally, macros cannot just appear out of thin air, Elixir doesn&amp;rsquo;t allow for this, because it&amp;rsquo;s intransparent.&lt;/p&gt;

&lt;p&gt;The only two ways that macros may enter a module is as follows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;require&lt;/strong&gt;: &lt;br /&gt;
This allows us to use macros defined in the &lt;code&gt;require&lt;/code&gt;d module in the form of:
&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-elixir&#34; data-lang=&#34;elixir&#34;&gt;require &lt;span style=&#34;color:#a6e22e&#34;&gt;Logger&lt;/span&gt;

&lt;span style=&#34;color:#a6e22e&#34;&gt;Logger&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;info &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Hello world!&amp;#34;&lt;/span&gt; &lt;span style=&#34;color:#75715e&#34;&gt;# `info` is a macro defined in the `Logger` module&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;strong&gt;import&lt;/strong&gt;: &lt;br /&gt;
This merges the &lt;code&gt;import&lt;/code&gt;ed module&amp;rsquo;s functions and macros into the scope of the current module.
We can then call macros like:
&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-elixir&#34; data-lang=&#34;elixir&#34;&gt;import &lt;span style=&#34;color:#a6e22e&#34;&gt;Logger&lt;/span&gt;

info &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Hello world!&amp;#34;&lt;/span&gt; &lt;span style=&#34;color:#75715e&#34;&gt;# Note how we don&amp;#39;t need the `Logger` prefix!&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;strong&gt;use&lt;/strong&gt;:
This is our special candidate. It does not directly make macros available to our module, but it can include &lt;code&gt;require&lt;/code&gt; or &lt;code&gt;import&lt;/code&gt; statements!&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Super tl;dr version of what this statement does:&lt;/strong&gt;
  It invokes the &lt;code&gt;__using__&lt;/code&gt; macro in the &lt;code&gt;use&lt;/code&gt;d module, so its just the syntactic sugar equivalent to &lt;code&gt;TheModule.__using__&lt;/code&gt;. Mostly used to dump some functionality into your module (Notice how we&amp;rsquo;re using &lt;code&gt;Plug.Router&lt;/code&gt; above!)&lt;/p&gt;

&lt;p&gt;Having established what to look for when hunting macros, let&amp;rsquo;s go back to our router example. We were trying to figure out where the &lt;code&gt;plug&lt;/code&gt; macro is coming from, remember?&lt;/p&gt;

&lt;p&gt;The only line that may inject macros is obviously:&lt;/p&gt;

&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-elixir&#34; data-lang=&#34;elixir&#34;&gt;use &lt;span style=&#34;color:#a6e22e&#34;&gt;Plug.Router&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Diving into the &lt;a href=&#34;https://github.com/elixir-plug/plug/blob/v1.4.3/lib/plug/router.ex#L202&#34;&gt;&lt;code&gt;Plug.Router&lt;/code&gt; source code&lt;/a&gt;, we find:&lt;/p&gt;

&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-elixir&#34; data-lang=&#34;elixir&#34;&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# Some details omitted for brevity&lt;/span&gt;
defmacro __using__(opts) do
  quote &lt;span style=&#34;color:#e6db74&#34;&gt;location&lt;/span&gt;: &lt;span style=&#34;color:#e6db74&#34;&gt;:keep&lt;/span&gt; do
    import &lt;span style=&#34;color:#a6e22e&#34;&gt;Plug.Router&lt;/span&gt;

    use &lt;span style=&#34;color:#a6e22e&#34;&gt;Plug.Builder&lt;/span&gt;, unquote(opts)

    def match(conn, _opts) do
      do_match(conn, conn&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;method, &lt;span style=&#34;color:#a6e22e&#34;&gt;Enum&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;map(conn&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;path_info, &lt;span style=&#34;color:#f92672&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;URI&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;decode&lt;span style=&#34;color:#f92672&#34;&gt;/&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;), conn&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;host)
    end

    def dispatch(%&lt;span style=&#34;color:#a6e22e&#34;&gt;Plug.Conn&lt;/span&gt;{&lt;span style=&#34;color:#e6db74&#34;&gt;assigns&lt;/span&gt;: assigns} &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; conn, _opts) do
      &lt;span style=&#34;color:#a6e22e&#34;&gt;Map&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;get(conn&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;private, &lt;span style=&#34;color:#e6db74&#34;&gt;:plug_route&lt;/span&gt;)&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;(conn)
    end

    defoverridable [&lt;span style=&#34;color:#e6db74&#34;&gt;match&lt;/span&gt;: &lt;span style=&#34;color:#ae81ff&#34;&gt;2&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;dispatch&lt;/span&gt;: &lt;span style=&#34;color:#ae81ff&#34;&gt;2&lt;/span&gt;]
  end
end&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Hmm, no &lt;code&gt;defmacro plug&lt;/code&gt;&amp;hellip; OK. Repeat the process. We got two new candidates!
1. &lt;code&gt;import Plug.Router&lt;/code&gt;
2. &lt;code&gt;use Plug.Builder&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Glancing over the &lt;a href=&#34;https://hexdocs.pm/plug/Plug.Router.html#functions&#34;&gt;functions in &lt;code&gt;Plug.Router&lt;/code&gt;&lt;/a&gt;, no hits. Moving on.&lt;/p&gt;

&lt;p&gt;Checking on &lt;a href=&#34;https://github.com/elixir-plug/plug/blob/v1.4.3/lib/plug/builder.ex#L102&#34;&gt;&lt;code&gt;Plug.Builder.__using__&lt;/code&gt;&lt;/a&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a id=&#34;builder_using&#34;&gt;&lt;/a&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-elixir&#34; data-lang=&#34;elixir&#34;&gt;defmacro __using__(opts) do
  quote do
    &lt;span style=&#34;color:#75715e&#34;&gt;# Again, shortened for clarity&lt;/span&gt;

    def init(opts) do
      opts
    end

    def call(conn, opts) do
      plug_builder_call(conn, opts)
    end

    import &lt;span style=&#34;color:#a6e22e&#34;&gt;Plug.Conn&lt;/span&gt;
    import &lt;span style=&#34;color:#a6e22e&#34;&gt;Plug.Builder&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;only&lt;/span&gt;: [&lt;span style=&#34;color:#e6db74&#34;&gt;plug&lt;/span&gt;: &lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;plug&lt;/span&gt;: &lt;span style=&#34;color:#ae81ff&#34;&gt;2&lt;/span&gt;] &lt;span style=&#34;color:#75715e&#34;&gt;# &amp;lt;- OHHHHHHHH BABY&lt;/span&gt;

    &lt;span style=&#34;color:#a6e22e&#34;&gt;Module&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;register_attribute(__MODULE__, &lt;span style=&#34;color:#e6db74&#34;&gt;:plugs&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;accumulate&lt;/span&gt;: true)
    &lt;span style=&#34;color:#a6e22e&#34;&gt;@before_compile&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;Plug.Builder&lt;/span&gt;
  end
end&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Jackpot!&lt;/strong&gt; So &lt;code&gt;plug&lt;/code&gt; is defined in &lt;a href=&#34;https://hexdocs.pm/plug/Plug.Builder.html&#34;&gt;&lt;code&gt;Plug.Builder&lt;/code&gt;&lt;/a&gt;!&lt;/p&gt;

&lt;p&gt;Go deeper you say? Hell yeah! On to the &lt;a href=&#34;https://github.com/elixir-plug/plug/blob/v1.4.3/lib/plug/builder.ex#L150&#34;&gt;&lt;code&gt;plug&lt;/code&gt; macro definition&lt;/a&gt;.
&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-elixir&#34; data-lang=&#34;elixir&#34;&gt;defmacro plug(plug, opts \\ []) do
  quote do
    &lt;span style=&#34;color:#a6e22e&#34;&gt;@plugs&lt;/span&gt; {unquote(plug), unquote(opts), true}
  end
end&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;

&lt;p&gt;Apparently, calling &lt;code&gt;plug&lt;/code&gt; just sets the &lt;code&gt;@plugs&lt;/code&gt; module attribute to a 3-item tuple. So what&amp;rsquo;s that about?&lt;/p&gt;

&lt;p&gt;We could technically wrap up here, we found the macro definition!&lt;/p&gt;

&lt;p&gt;&lt;div style=&#34;width:100%;height:0;padding-bottom:56%;position:relative;&#34;&gt;&lt;iframe src=&#34;https://giphy.com/embed/a0h7sAqON67nO&#34; width=&#34;100%&#34; height=&#34;100%&#34; style=&#34;position:absolute&#34; frameBorder=&#34;0&#34; class=&#34;giphy-embed&#34; allowFullScreen&gt;&lt;/iframe&gt;&lt;/div&gt;&lt;p&gt;&lt;a href=&#34;https://giphy.com/gifs/borat-great-success-a0h7sAqON67nO&#34;&gt;via GIPHY&lt;/a&gt;&lt;/p&gt;&lt;/p&gt;

&lt;p&gt;But where&amp;rsquo;s the fun in that?&lt;/p&gt;

&lt;h2 id=&#34;how-plugs-can-be-composed-out-of-other-plugs&#34;&gt;How Plugs can be composed out of other Plugs&lt;/h2&gt;

&lt;p&gt;I&amp;rsquo;d like you to go back to &lt;a href=&#34;#builder_using&#34;&gt;the &lt;code&gt;Plug.Builder.__using__&lt;/code&gt; macro&lt;/a&gt; for a second.&lt;/p&gt;

&lt;p&gt;Notice the two last lines:
&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-elixir&#34; data-lang=&#34;elixir&#34;&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;Module&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;register_attribute(__MODULE__, &lt;span style=&#34;color:#e6db74&#34;&gt;:plugs&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;accumulate&lt;/span&gt;: true)
&lt;span style=&#34;color:#a6e22e&#34;&gt;@before_compile&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;Plug.Builder&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;

&lt;p&gt;To shorten this a little bit, the call to &lt;a href=&#34;https://hexdocs.pm/elixir/Module.html#register_attribute/3&#34;&gt;&lt;code&gt;Module.register_attribute&lt;/code&gt;&lt;/a&gt; states that we want our module to not reassign &lt;code&gt;@plugs&lt;/code&gt; when we repeatedly assign it but instead add the new values to an accumulating list.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;@before_compile&lt;/code&gt; module attribute is reserved by the language and hints the Elixir compiler to invoke the &lt;code&gt;__before_compile__/1&lt;/code&gt; macro in &lt;code&gt;Plug.Builder&lt;/code&gt; before compiling the module and put its output at the end of the router module definition (&lt;a href=&#34;https://hexdocs.pm/elixir/Module.html#module-compile-callbacks&#34;&gt;for curious people&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;Here we go, what&amp;rsquo;s in the &lt;a href=&#34;https://github.com/elixir-plug/plug/blob/v1.4.3/lib/plug/builder.ex#L126&#34;&gt;&lt;code&gt;__before_compile__&lt;/code&gt; box&lt;/a&gt;?&lt;/p&gt;

&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-elixir&#34; data-lang=&#34;elixir&#34;&gt;defmacro __before_compile__(env) do
  &lt;span style=&#34;color:#75715e&#34;&gt;# Apparently we&amp;#39;re grabbing all the defined plugs from the @plugs list&lt;/span&gt;
  plugs        &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;Module&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;get_attribute(env&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;module, &lt;span style=&#34;color:#e6db74&#34;&gt;:plugs&lt;/span&gt;)
  builder_opts &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;Module&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;get_attribute(env&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;module, &lt;span style=&#34;color:#e6db74&#34;&gt;:plug_builder_opts&lt;/span&gt;)

  &lt;span style=&#34;color:#75715e&#34;&gt;# Oh look, it&amp;#39;s compiling all our plugs into something new!&lt;/span&gt;
  {conn, body} &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;Plug.Builder&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;compile(env, plugs, builder_opts)

  quote do
    &lt;span style=&#34;color:#75715e&#34;&gt;# Defines a private function on our router module, not too bad&lt;/span&gt;
    defp plug_builder_call(unquote(conn), _), &lt;span style=&#34;color:#e6db74&#34;&gt;do&lt;/span&gt;: unquote(body)
  end
end&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;OK, everything is straightforward. Just &lt;code&gt;Plug.Builder.compile/3&lt;/code&gt; seems crazy, fasten your seatbelt, we&amp;rsquo;re &lt;a href=&#34;https://github.com/elixir-plug/plug/blob/v1.4.3/lib/plug/builder.ex#L178&#34;&gt;going in&lt;/a&gt;!&lt;/p&gt;

&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-elixir&#34; data-lang=&#34;elixir&#34;&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;@spec&lt;/span&gt; compile(&lt;span style=&#34;color:#a6e22e&#34;&gt;Macro.Env&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;t, [{plug, &lt;span style=&#34;color:#a6e22e&#34;&gt;Plug&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;opts, &lt;span style=&#34;color:#a6e22e&#34;&gt;Macro&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;t}], &lt;span style=&#34;color:#a6e22e&#34;&gt;Keyword&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;t) &lt;span style=&#34;color:#f92672&#34;&gt;::&lt;/span&gt; {&lt;span style=&#34;color:#a6e22e&#34;&gt;Macro&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;t, &lt;span style=&#34;color:#a6e22e&#34;&gt;Macro&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;t}
def compile(env, pipeline, builder_opts) do
  conn &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; quote &lt;span style=&#34;color:#e6db74&#34;&gt;do&lt;/span&gt;: conn
  {conn, &lt;span style=&#34;color:#a6e22e&#34;&gt;Enum&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;reduce(pipeline, conn, &lt;span style=&#34;color:#f92672&#34;&gt;&amp;amp;&lt;/span&gt;quote_plug(init_plug(&amp;amp;1), &amp;amp;2, env, builder_opts))}
end&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;To keep this post somewhat manageable, we are going to handwave just the tiniest bit and just give you the big picture.&lt;/p&gt;

&lt;p&gt;Our router module calls the &lt;a href=&#34;#builder_using&#34;&gt;&lt;code&gt;Plug.Builder.__using__&lt;/code&gt; macro&lt;/a&gt;, which defines &lt;code&gt;call/2&lt;/code&gt; on it (routers are &lt;code&gt;Plug&lt;/code&gt;s, remember?), which just delegates to &lt;code&gt;plug_builder_call&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;To define &lt;code&gt;plug_builder_call&lt;/code&gt;, we are running over all the &lt;code&gt;Plug&lt;/code&gt;s filled into the &lt;code&gt;@plugs&lt;/code&gt; module attribute (e.g, using the &lt;code&gt;plug&lt;/code&gt; macro that we examined) - those are in the &lt;code&gt;pipeline&lt;/code&gt; parameter of &lt;code&gt;Plug.Builder.compile&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;From there, we are just nesting the &lt;code&gt;Plug&lt;/code&gt;s in one another, effectively ending up with something along the lines of this:&lt;/p&gt;

&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-elixir&#34; data-lang=&#34;elixir&#34;&gt;defmodule &lt;span style=&#34;color:#a6e22e&#34;&gt;AppRouter&lt;/span&gt; do
  def call(conn, opts) do
    plug_builder_call(conn, opts)
  end

  defp plug_builder_call(conn, opts)
    conn
    &lt;span style=&#34;color:#f92672&#34;&gt;|&amp;gt;&lt;/span&gt; match(opts)
    &lt;span style=&#34;color:#f92672&#34;&gt;|&amp;gt;&lt;/span&gt; dispatch(opts)
  end
end&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The &lt;code&gt;call/2&lt;/code&gt; function now just composes the other &lt;code&gt;Plug&lt;/code&gt;s defined within it, in the order of appearance. Neat, huh? It&amp;rsquo;s like a chain of transfomation steps that our request is handed through!&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;match&lt;/code&gt; and &lt;code&gt;dispatch&lt;/code&gt; function plugs are defined in &lt;code&gt;Plug.Router.__using__&lt;/code&gt;, but this post is already 1400 words long, so we&amp;rsquo;ll wrap it up here :-)&lt;/p&gt;

&lt;p&gt;Until next time!&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Elixir Macros Part 1 - How they work</title>
      <link>https://blog.janfornoff.com/elixir-macros-1/</link>
      <pubDate>Tue, 03 Oct 2017 11:59:53 +0000</pubDate>
      
      <guid>https://blog.janfornoff.com/elixir-macros-1/</guid>
      <description>

&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt; Elixir is nice. Macros enable you to create a concise language for the application problem domain without applying magic.&lt;/p&gt;

&lt;p&gt;Today’s post will be about Elixir macros, mostly just documenting my own learning process by retelling the story.&lt;/p&gt;

&lt;h1 id=&#34;why-do-we-care&#34;&gt;Why do we care?&lt;/h1&gt;

&lt;p&gt;Programming languages are used to solve all types of problems. Web servers, games, banking applications, you name it. We appreciate low-boilerplate code that caters to the problem domain. Conciseness and readability are what we’re looking for. However, it is impossible to support expressiveness for all those use cases in the core language. Therefore, we need a mechanism to create readable code.
Creating such domain-bound abstractions involves hiding frequently unimportant details. We need to put these details away while making sure that it’s very clear where to find them. This makes code maintainable and simple to reason about.&lt;/p&gt;

&lt;h1 id=&#34;code-as-transformable-data&#34;&gt;Code as (transformable) data&lt;/h1&gt;

&lt;p&gt;Let’s take a step back. The first important idea to understand what Macros are used for is where they sit in the compilation process. When you compile Elixir code, the text of the source file is parsed into an Abstract Syntax Tree (AST), which represents the code in syntactic form.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Simple example:&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-elixir&#34; data-lang=&#34;elixir&#34;&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;is represented in the AST as:&lt;/p&gt;

&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-elixir&#34; data-lang=&#34;elixir&#34;&gt;{&lt;span style=&#34;color:#e6db74&#34;&gt;:+&lt;/span&gt;, [&lt;span style=&#34;color:#e6db74&#34;&gt;context&lt;/span&gt;: &lt;span style=&#34;color:#a6e22e&#34;&gt;Elixir&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;import&lt;/span&gt;: &lt;span style=&#34;color:#a6e22e&#34;&gt;Kernel&lt;/span&gt;], [&lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;]}&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This is a tuple with 3 elements, the first being the name of the function being called, the second metadata, and the last the list of function arguments.&lt;/p&gt;

&lt;h1 id=&#34;what-role-do-macros-play&#34;&gt;What role do Macros play?&lt;/h1&gt;

&lt;p&gt;After generating the Elixir AST from the source code files, Macros are &lt;em&gt;expanded&lt;/em&gt; by the compiler.
That means: When the compiler finds a call to a Macro, the compiler executes it and substitutes the result of the Macro invocation into the AST.&lt;/p&gt;

&lt;p&gt;Therefore, Macros need to return an AST representation, such as the one we saw in the example.
Our core takeaway from this is that Macros just return &lt;strong&gt;data&lt;/strong&gt; (in this case a data-representation of source code).&lt;/p&gt;

&lt;p&gt;Let&amp;rsquo;s take a look at a fun little Macro.&lt;/p&gt;

&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-elixir&#34; data-lang=&#34;elixir&#34;&gt;&lt;span style=&#34;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f&#34;&gt; 1&lt;/span&gt;defmodule &lt;span style=&#34;color:#a6e22e&#34;&gt;MyLogger&lt;/span&gt; do
&lt;span style=&#34;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f&#34;&gt; 2&lt;/span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;@enabled&lt;/span&gt; true
&lt;span style=&#34;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f&#34;&gt; 3&lt;/span&gt;
&lt;span style=&#34;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f&#34;&gt; 4&lt;/span&gt;  defmacro log(message) do
&lt;span style=&#34;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f&#34;&gt; 5&lt;/span&gt;    if &lt;span style=&#34;color:#a6e22e&#34;&gt;@enabled&lt;/span&gt; do
&lt;span style=&#34;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f&#34;&gt; 6&lt;/span&gt;      quote do
&lt;span style=&#34;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f&#34;&gt; 7&lt;/span&gt;        &lt;span style=&#34;color:#a6e22e&#34;&gt;IO&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;puts unquote(message)
&lt;span style=&#34;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f&#34;&gt; 8&lt;/span&gt;      end
&lt;span style=&#34;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f&#34;&gt; 9&lt;/span&gt;    end
&lt;span style=&#34;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f&#34;&gt;10&lt;/span&gt;  end
&lt;span style=&#34;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f&#34;&gt;11&lt;/span&gt;end&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This is a logger module whose &lt;code&gt;log/1&lt;/code&gt; Macro you can use, and can turn off all log output using &lt;code&gt;@enabled&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;How would that work in practice?&lt;/p&gt;

&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-elixir&#34; data-lang=&#34;elixir&#34;&gt;require &lt;span style=&#34;color:#a6e22e&#34;&gt;MyLogger&lt;/span&gt;

&lt;span style=&#34;color:#75715e&#34;&gt;# when @enabled is true&lt;/span&gt;
&lt;span style=&#34;color:#a6e22e&#34;&gt;MyLogger&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;log(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Hello!&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#75715e&#34;&gt;# =&amp;gt; :ok&lt;/span&gt;
&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Hello!&amp;#34;&lt;/span&gt;

&lt;span style=&#34;color:#75715e&#34;&gt;# when @enabled is false&lt;/span&gt;
&lt;span style=&#34;color:#a6e22e&#34;&gt;MyLogger&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;log(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Hello!&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#75715e&#34;&gt;# =&amp;gt; nil&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Note the calls to &lt;a href=&#34;https://hexdocs.pm/elixir/Kernel.SpecialForms.html#quote/2&#34;&gt;&lt;code&gt;quote&lt;/code&gt;&lt;/a&gt; and &lt;a href=&#34;https://hexdocs.pm/elixir/Kernel.SpecialForms.html#unquote/1&#34;&gt;&lt;code&gt;unquote&lt;/code&gt;&lt;/a&gt;, those are the bread and butter of writing Macros. For the following explanations, I&amp;rsquo;d invite you to think of writing Macros as being analogous to the concept of string interpolation. The block passed &lt;code&gt;quote&lt;/code&gt; outlining the string (or AST-fragment if you will), &lt;code&gt;unquote&lt;/code&gt; used to insert predefined values.&lt;/p&gt;

&lt;p&gt;Line 5 (&lt;code&gt;if @enabled&lt;/code&gt;) is executed at compile time, whenever the Macro is expanded.
Therefore, if &lt;code&gt;@enabled&lt;/code&gt; is false, the &lt;code&gt;quote&lt;/code&gt;-block will not be returned, thus defaulting to nil and inserting nothing into the Macro-site AST.&lt;/p&gt;

&lt;p&gt;If &lt;code&gt;@enabled&lt;/code&gt; is true, &lt;code&gt;quote&lt;/code&gt; states that all code inside of it will be returned as the AST data representation. The effect of this is literally as if you copy-pasted the code inside the &lt;code&gt;quote&lt;/code&gt; block into the module where you called the Macro.&lt;/p&gt;

&lt;p&gt;Now note line 7, specifically &lt;code&gt;unquote(message)&lt;/code&gt;. If we did omitted &lt;code&gt;unquote()&lt;/code&gt;, the message value from the function argument would&amp;rsquo;ve not been inserted into the AST, but instead it stays as a &amp;ldquo;placeholder&amp;rdquo; that is evaluated as soon as runtime execution passes that line. For example, it might output the result of the function &lt;code&gt;message()&lt;/code&gt; of the module that we compiled the Macro into.&lt;/p&gt;

&lt;h1 id=&#34;conclusion&#34;&gt;Conclusion&lt;/h1&gt;

&lt;p&gt;We can conclude that expanded Macros yield some code representation that is then injected into the AST representing the Macro-calling module. When we want to inject values into that code representation (e.g, Macro function arguments), we have to use &lt;code&gt;unquote&lt;/code&gt;. Otherwise the code is just executed in exactly the way we write it in &lt;code&gt;quote&lt;/code&gt;.&lt;/p&gt;

&lt;h1 id=&#34;references&#34;&gt;References&lt;/h1&gt;

&lt;p&gt;If you&amp;rsquo;d like a much longer and more in-depth resource on Elixir Macro mechanics, I highly recommend &lt;a href=&#34;http://www.theerlangelist.com/article/macros_1&#34;&gt;Saša Jurić&amp;rsquo;s series of blog posts on Macros&lt;/a&gt;!&lt;/p&gt;

&lt;h1 id=&#34;up-next&#34;&gt;Up next&lt;/h1&gt;

&lt;p&gt;Macros are constructs that make code and the origin of functionality slightly less obvious because they inject code into your modules AST. This is intended and necessary to create more concise and succinct code, but makes it more challenging to track down how things actually work.&lt;/p&gt;

&lt;p&gt;Therefore Part 2 will be about Macro Archaeology and how to dig up functionality if you want to dive in the nitty-gritty hidden details.&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Cold showers every day</title>
      <link>https://blog.janfornoff.com/cold-showers/</link>
      <pubDate>Thu, 24 Aug 2017 08:00:00 +0000</pubDate>
      
      <guid>https://blog.janfornoff.com/cold-showers/</guid>
      <description>

&lt;p&gt;A while ago, I read &lt;a href=&#34;http://www.kyleschen.com/2017/04/11/what-do-you-want-to-want/&#34;&gt;this article&lt;/a&gt; by Kyle Eschenroeder. It talks about some heuristics in striving for a fulfilled life and some pitfalls you might want to avoid in today’s society, where you can essentially pursue whatever you want.&lt;/p&gt;

&lt;p&gt;Kyle introduces the concept of eudaimonia, which proposes acts that might cost some extra effort in the moment, but leave you feeling good about yourself later on. These acts are also very much about bettering yourself as a human. Getting up earlier to work out might not feel great while you’re struggling to crawl out of bed, but as you leave the gym, it will leave you with a sense of satisfaction — I did well today.&lt;/p&gt;

&lt;p&gt;This post is about such an eudaimonic deed. Taking cold showers. First thing in the morning, every day. &lt;em&gt;How the hell is freezing my ass off going to help me be a better human being?&lt;/em&gt;, you might ask. Great question. Here’s why I think it is a good idea.&lt;/p&gt;

&lt;h3 id=&#34;wakes-you-up-thoroughly&#34;&gt;Wakes you up. Thoroughly.&lt;/h3&gt;

&lt;p&gt;No 2-hour wake up stagger until you hit your stride. This synergizes nicely with trying to build a habit of getting up earlier! You might as well stay in bed if your brain-fog prevents you from doing anything useful.&lt;/p&gt;

&lt;h3 id=&#34;requires-you-to-step-outside-your-comfort-zone&#34;&gt;Requires you to step outside your comfort zone.&lt;/h3&gt;

&lt;p&gt;This in itself is worthwhile, but you also want to practice getting out of your comfort zone over and over.
Getting it done first thing in the morning allows you to:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Form a habit very easily!&lt;/li&gt;
&lt;li&gt;Not be as tempted to skip it, because it sucks (it does). &lt;br /&gt;
Most people tend to not think a lot right after getting up, this helps.&lt;/li&gt;
&lt;li&gt;Health benefits: There probably are some, but I don’t even care.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;That’s it for my first post.&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>About</title>
      <link>https://blog.janfornoff.com/page/about/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      
      <guid>https://blog.janfornoff.com/page/about/</guid>
      <description>&lt;p&gt;Hey! 👋🏻&lt;/p&gt;

&lt;p&gt;My name is Jan. Once upon a time, I finished a &lt;strong&gt;Masters Degree in Business Information Systems&lt;/strong&gt; at TU Darmstadt.
I&amp;rsquo;ve been writing software for quite some time, did 6 years of Ruby working while studying and 2 years of Elixir.
Functional programming is something I grew to enjoy a lot, since it fits my approach to problem solving and seems to keep complexity at bay, mostly.
Here are some of my &lt;a href=&#34;https://blog.janfornoff.com/projects&#34;&gt;side projects&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The operational side of software and making things work really well when the rubber hits the road is very enjoyable to me. I&amp;rsquo;m also sort of a &lt;strong&gt;performance nerd&lt;/strong&gt; in that way, I love benchmarking and optimizing.&lt;/p&gt;

&lt;p&gt;During my university career I touched a lot of networking technology and I&amp;rsquo;m really interested in &lt;strong&gt;Distributed Systems&lt;/strong&gt;, how they operate and thinking through the cases where things can go wrong.&lt;/p&gt;

&lt;p&gt;Previously, I&amp;rsquo;ve been working and learning alongside the great people of &lt;a href=&#34;https://www.grandcentrix.net/&#34;&gt;&lt;strong&gt;Grandcentrix&lt;/strong&gt;&lt;/a&gt; part-time while freelancing on the side. In September 2019, I joined Google Zurich as a Site Reliability Engineer.&lt;/p&gt;

&lt;p&gt;Drop me a mail if you think we should have a chat!&lt;/p&gt;

&lt;p&gt;Also my CV is &lt;a href=&#34;http://cv.janfornoff.com&#34;&gt;here&lt;/a&gt; (or you can find it in the navigation bar).&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Imprint</title>
      <link>https://blog.janfornoff.com/page/imprint/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      
      <guid>https://blog.janfornoff.com/page/imprint/</guid>
      <description>&lt;p&gt;According to §5 TMG:&lt;/p&gt;

&lt;p&gt;Jan Fornoff &lt;br /&gt;
Amselweg 66 &lt;br /&gt;
64295 Darmstadt&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Email:&lt;/strong&gt; &lt;a href=&#34;mailto:janfornoff@gmail.com&#34;&gt;janfornoff@gmail.com&lt;/a&gt;&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Projects</title>
      <link>https://blog.janfornoff.com/page/projects/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      
      <guid>https://blog.janfornoff.com/page/projects/</guid>
      <description>

&lt;h1 id=&#34;gitctx-https-github-com-jfornoff-gitctx&#34;&gt;&lt;a href=&#34;https://github.com/jfornoff/gitctx&#34;&gt;Gitctx&lt;/a&gt;&lt;/h1&gt;

&lt;p&gt;CLI tool (written in Go) that allows to have multiple profiles of Git committer information (i.e., name &amp;amp; email address).
You can then configure on a per-project basis which of these profiles to use.&lt;/p&gt;

&lt;h1 id=&#34;standupbot&#34;&gt;&lt;a href=&#34;#&#34;&gt;Standupbot&lt;/a&gt;&lt;/h1&gt;

&lt;p&gt;Bot that allows you to have asynchronous standup meeting via Slack DM.
Standups are formatted and posted to a status channel for team members and managers to see.
Every user can configure his standup time independently.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;https://docs.google.com/presentation/d/1EwYcrZ7avlwdU9C6yntaoqN5ANCbapg_Gp7QJqb5ACo/edit?usp=sharing&#34;&gt;Meetup presentation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Written in Elixir using OTP&lt;/li&gt;
&lt;li&gt;Deployed to multiple Slack workspaces and in active use every day&lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&#34;multiplayer-tictactoe-https-tictactoe-elm-herokuapp-com&#34;&gt;&lt;a href=&#34;https://tictactoe-elm.herokuapp.com/&#34;&gt;Multiplayer TicTacToe&lt;/a&gt;&lt;/h1&gt;

&lt;p&gt;Written in Elm and using Phoenix Channels ❤️&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Written for a workshop at the &lt;a href=&#34;https://www.meetup.com/Elixir-Meetup-Rhein-Main&#34;&gt;Elixir RheinMain Meetup&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Got me started with Phoenix Channels and handling Websockets in Elm&lt;/li&gt;
&lt;li&gt;Source code available: &lt;a href=&#34;https://github.com/jfornoff/tictactoe&#34;&gt;Elixir backend&lt;/a&gt; &amp;amp; &lt;a href=&#34;https://github.com/jfornoff/tictactoe-elm&#34;&gt;Elm frontend&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&#34;game-of-life-https-elm-game-of-life-herokuapp-com&#34;&gt;&lt;a href=&#34;https://elm-game-of-life.herokuapp.com/&#34;&gt;Game of Life&lt;/a&gt;&lt;/h1&gt;

&lt;p&gt;Written in Elm ❤️&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Implements &lt;a href=&#34;https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life&#34;&gt;Conway&amp;rsquo;s Game of Life&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Taught me quite a bit about encapsulation in Elm modules and how to represent the current Game state as Elm types&lt;/li&gt;
&lt;li&gt;Source code available on &lt;a href=&#34;https://github.com/jfornoff/elm-game-of-life&#34;&gt;Github&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&#34;chefkoch-shopping-https-chefkoch-shopping-herokuapp-com&#34;&gt;&lt;a href=&#34;https://chefkoch-shopping.herokuapp.com&#34;&gt;Chefkoch-Shopping&lt;/a&gt;&lt;/h1&gt;

&lt;p&gt;Experiment while checking out Node.js&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Integrates with the &lt;a href=&#34;https://www.wunderlist.com&#34;&gt;Wunderlist&lt;/a&gt; API&lt;/li&gt;
&lt;li&gt;Fetches Recipes from &lt;a href=&#34;https://www.chefkoch.de&#34;&gt;Chefkoch&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Parses ingredients (Chefkoch does not offer an open API, unfortunately)&lt;/li&gt;
&lt;li&gt;Allows selecting and editing ingredients to insert into Wunderlist&lt;/li&gt;
&lt;/ul&gt;
</description>
    </item>
    
  </channel>
</rss>
