<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Abdulrahman Solanke on Medium]]></title>
        <description><![CDATA[Stories by Abdulrahman Solanke on Medium]]></description>
        <link>https://medium.com/@solankerahman?source=rss-7964fd09d426------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*6uK4pSaEzbrwu0sdf0YOvg@2x.jpeg</url>
            <title>Stories by Abdulrahman Solanke on Medium</title>
            <link>https://medium.com/@solankerahman?source=rss-7964fd09d426------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 21:36:47 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@solankerahman/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[A Backend Engineer’s guide to TCP Connections (Part 1)]]></title>
            <link>https://medium.com/@solankerahman/a-backend-engineers-guide-to-tcp-connections-part-1-10ccccfb8c6e?source=rss-7964fd09d426------2</link>
            <guid isPermaLink="false">https://medium.com/p/10ccccfb8c6e</guid>
            <category><![CDATA[linux]]></category>
            <category><![CDATA[tcp]]></category>
            <category><![CDATA[socket-programming]]></category>
            <dc:creator><![CDATA[Abdulrahman Solanke]]></dc:creator>
            <pubDate>Thu, 23 Mar 2023 11:13:16 GMT</pubDate>
            <atom:updated>2023-03-23T11:13:16.507Z</atom:updated>
            <content:encoded><![CDATA[<p>As a backend engineer, understanding how TCP connections work is essential for building robust and efficient systems that can handle high volumes of traffic.</p><p>In this article, we will explore the key concepts behind TCP connections, including why we need them, how they are established and maintained.</p><h3>Why do we need TCP?</h3><p>TCP (Transmission Control Protocol) is one of the fundamental protocols in computer networking. It is responsible for establishing reliable connections between devices over the Internet. Without TCP, it would be challenging to ensure that data sent from one device to another would arrive intact and in the correct order. TCP guarantees that every packet of data sent is received by the destination device and that packets are retransmitted if they are lost or damaged in transit. This ensures that data is transmitted reliably, making TCP an essential protocol for many applications, including web browsing, email, file transfer, and real-time communication.</p><h3>TCP Connection Flow</h3><p>We are going to look at how a TCP connection goes through different states in its lifecycle. Since it is a guide to general TCP workings, we won’t go in-depth into the Linux kernel implementation of this state machine.</p><p>Recall that TCP is a connection-based protocol. This means that both ends of the connection need a sort of handshake to indicate that a connection has been made successfully.</p><p>To establish a TCP connection, the client and server devices must perform a three-way handshake (3WHS). The handshake is a sequence of messages exchanged between the client and server that establishes and synchronizes the connection. The three steps are:</p><ol><li>The client sends a <strong>SYN</strong> (Synchronize) message to the server, indicating that it wants to establish a connection.</li><li>The server responds with a <strong>SYN-ACK</strong> (Synchronize-Acknowledge) message, indicating that it is willing to establish a connection.</li><li>The client responds with an <strong>ACK</strong> (Acknowledge) message, indicating that it has received the <strong>SYN-ACK</strong> message and that the connection is established.</li></ol><p>Once the connection is established, data flow can begin via the connection.</p><p>Here are some connection states to know:</p><ul><li><strong>LISTEN</strong> Indicates a state of readiness to accept connection requests.</li><li><strong>SYN_SENT</strong> Indicates that the client has initiated the active open process with the receiver.</li><li><strong>SYN_RECEIVED</strong> Indicates that the server has received a SYN segment from the sender.</li><li><strong>ESTABLISHED</strong> Indicates that the receiver has received a SYN segment from the sender, the sequence numbers are synchronized via an ACK packet, and a connection is established.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/761/1*eZkFUd9cACdcFg1fD4cqbg.png" /></figure><h4>SYN Queue</h4><p>When a client sends a SYN packet to a server, the server places the connection request in a queue called the SYN queue. The SYN queue is used to store incomplete connection requests until they can be fully processed. “SYN queue” is not a real queue, but is implemented as a counter plus shared hash table.</p><p>The reason for this design is that SYN queues are dangerous. By sending a single packet (SYN), the sender can get the receiver to commit resources (memory for the SYN queue entry). if you send enough such packets fast enough, possibly with a forged origination address, you will either cause the receiver to exhaust its memory resources or start refusing to accept legitimate connections.</p><p>For this reason, modern operating systems do not have a SYN queue. Instead, they will various techniques (the most common is called SYN cookies) that will allow them to only have a queue for connections that have already answered the initial SYN-ACK packet, and thus prove they have dedicated resources themselves for this connection.</p><h4>Accept Queue</h4><p>Also named complete queue, stores established connections that waiting to be dequeued by the application with accept() syscall. This is implemented as a FIFO queue.</p><p>The accept queue length can be configured by setting the backlog in the listen(fd, backlog) syscall or using the host default net.core.somaxconn value.</p><p>We can monitor the status of the accept queue using ss utility on linux.</p><pre>ubuntu@node-1:~$ ss -ntl | head<br>State  Recv-Q Send-Q Local Address:Port Peer Address:PortProcess<br>LISTEN 0      4096   127.0.0.53%lo:53        0.0.0.0:*          <br>LISTEN 0      128          0.0.0.0:22        0.0.0.0:*          <br>LISTEN 0      4096               *:5000            *:*          <br>LISTEN 0      4096               *:5001            *:*          <br>LISTEN 0      4096               *:2377            *:*          <br>LISTEN 0      4096               *:7946            *:*          <br>LISTEN 0      4096               *:8080            *:*          <br>LISTEN 0      128             [::]:22           [::]:*          </pre><p>For sockets in LISTEN state, the Recv-Q column shows the current size of the accept queue and the Send-Q shows the max size of the accept queue.</p><p>We can also check the default net.core.somaxconn value on the host</p><pre>ubuntu@node-1:~$ sysctl net.core.somaxconn<br>net.core.somaxconn = 4096<br>ubuntu@node-1:~$ uname -r<br>5.15.0-67-generic</pre><p>This shows that the default accept queue backlog is set to 4096 on linux kernel 5.15.</p><p>However, the socket bound to port :22 (SSH) on my host has a Send-Q value of 128. This shows that for SSH connections, the max number of concurrent TCP connections that can be in the accept queue is 128.</p><h3>Theoretical and practical limitations of TCP connections on a Linux host</h3><p>While TCP is a reliable protocol, there are limitations to its performance. One of the primary limitations is the theoretical maximum throughput, which is determined by the round-trip time (RTT) between the client and server devices and the size of the TCP window. The <strong>TCP window</strong> is the amount of data that can be sent before receiving an acknowledgement from the other device. Increasing the TCP window can improve performance, but it also increases the likelihood of packet loss.</p><p>In addition to theoretical limitations, practical limitations can also impact TCP performance. For example, on a Linux host, the number of available TCP sockets can be limited by the operating system’s maximum number of open files. This can result in connection failures and poor performance if too many connections are opened simultaneously.</p><p>Established TCP connections also use memory on the host. This means connections not used should be properly closed to avoid overloading the RAM on the host. The network stack allocates memory to hold TCP connections. This can be improved by doing some TCP network parameter tuning on the host.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=10ccccfb8c6e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[CUSTOM DECORATORS IN PYTHON]]></title>
            <link>https://medium.com/@solankerahman/custom-decorators-in-python-d4ed0747e533?source=rss-7964fd09d426------2</link>
            <guid isPermaLink="false">https://medium.com/p/d4ed0747e533</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[django]]></category>
            <category><![CDATA[decorators]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Abdulrahman Solanke]]></dc:creator>
            <pubDate>Sun, 09 Jun 2019 22:40:00 GMT</pubDate>
            <atom:updated>2019-06-09T22:40:00.178Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NlqpTTAM8DbGl4paBmjE_g.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@cgower?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Christopher Gower</a> on <a href="https://unsplash.com/search/photos/tech?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><h3>WHAT IS A PYTHON DECORATOR?</h3><p>Python is a feature-rich programming language with a very expressive syntax. This seemingly paradoxical attributes set it up for quite an interesting relationship with the tool-sets it offers. One of the python features I will cover in this article is DECORATORS.</p><p>A decorator basically works as a wrapper, which augments the functionality of a function or method without altering the structure of the function itself.</p><p>Python decorators are not an implementation of the the decorator design pattern which allows behaviour to be added to an object dynamically. One major difference to note is that python decorators add functionalities to functions and methods at definition time while decorator pattern adds functionalities at run time. This makes python decorators a higher level construct than decorator pattern.</p><h3>FUNDAMENTALS OF DECORATORS</h3><p>One of the reasons why python decorators are quite powerful is that they leverage on the fact that functions in python are first class citizens (objects) and hence can be manipulated in powerful ways.</p><ol><li>Assign function to a variable</li></ol><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e2c2f31b34aaf21d6e2d0e51feb6b540/href">https://medium.com/media/e2c2f31b34aaf21d6e2d0e51feb6b540/href</a></iframe><p>2. Nest functions within a function and closure</p><p>Python functions can be nested within an outer function. Also, these inner functions have a read-only access to the enclosing scope and not assignment (a condition known as a closure).</p><p>In the code snippet below, the function sum_integers which takes no argument was defined within the list_sum function and returns the sum of only integers from the demo_list argument which was passed into the enclosing list_sum function.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6330756fde67ac4974772db56e53f711/href">https://medium.com/media/6330756fde67ac4974772db56e53f711/href</a></iframe><p>3. Return a function from a function</p><p>A function can be returned from a function which can then be manipulated. The returned function can be assigned to a variable or passed as an argument into another function as we will later see.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/68f319a4fd5af45dd7b846d83e86abf9/href">https://medium.com/media/68f319a4fd5af45dd7b846d83e86abf9/href</a></iframe><p>4. Pass a function as a parameter to another function</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2709aa79afa326bf52e7f66a1cddc9a4/href">https://medium.com/media/2709aa79afa326bf52e7f66a1cddc9a4/href</a></iframe><p><strong>Composition of a Decorator</strong></p><p>Now that we have highlighted some important features of a python function, let us see how that information can help us in generating a decorator.</p><blockquote>Basically, a decorator takes a function as an argument, makes some in-place modifications on the function and returns a new modified function based off the function it received as an argument</blockquote><p>Let us see an example. We will generate a decorator that will simply capitalize the string output of a function</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7c0e8f2f83ac02da51b29d916b04a829/href">https://medium.com/media/7c0e8f2f83ac02da51b29d916b04a829/href</a></iframe><p>The capitalize_names function acts as a decorator that takes a function as an argument. The inner func_wrapper function has access to the function argument via closures and modifies the function. The capitalize_name decorator then returns the result of the func_wrapper function.</p><p>The most important part of the code snippet however is the assignment expression</p><pre>get_fullname = capitalize_names(get_fullname)</pre><p>This ensures that the original function get_fullname becomes replaced by the modified (decorated) function returned by the capitalize_names decorator.</p><p><strong>Python’s decorator syntactic sugar</strong></p><p>Python provides a syntactic sugar to make the creation and usage of a decorator cleaner and nicer. The general assignment expression syntax of a decorator;</p><pre>get_fullname = capitalize_names(get_fullname)</pre><p>can be replaced with a shortcut. The name of decoration function prepended with an @ symbol is mentioned before the definition of the function to be decorated</p><pre>@capitalize_names<br>def <strong>get_fullname</strong>(<em>firstname</em>, <em>lastname</em>):<br>    <em>return</em> &quot;{}, {}&quot;.format(lastname, firstname)</pre><h3>ADVANCED DECORATORS</h3><p><strong>Handling arbitrary number of function arguments</strong></p><p>Upon close introspection of the decorator function provided above, we see that the inner func_wrapper function does not take an arbitrary number of arguments as function parameters. This is a flaw in the sense that we can not predetermine the number of function arguments any function that will use the decorator will possess.</p><p>For instance, the above decorator function won’t work for class methods (which are also basically functions) because all methods expect their first argument to be a reference to its class instance i.e self.</p><p>Therefore, a more generic approach will be to utilize the arguments packing feature of python. This can be done by putting *args and **kwargs as parameters for the func_wrapper function. Then, it can accept any number of arbitrary arguments and keyword arguments</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c421602545edbe0b3f952102d4a0245e/href">https://medium.com/media/c421602545edbe0b3f952102d4a0245e/href</a></iframe><p>Now, the capitalize_names function can be used to decorate any function regardless of the number of arguments. It will also work in decorating class methods since “self” is also an argument.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2dd2c34b978df80a4c0f63ab80db7220/href">https://medium.com/media/2dd2c34b978df80a4c0f63ab80db7220/href</a></iframe><p><strong>Multiple decorators on a function</strong></p><p>Let us consider we want to decorate our get_fullname function by another function that will lump the full name together and prepend it with ‘@’ symbol (something like a username generator).</p><p>Python decorator syntax makes this expressively achievable. Note that the order of setting the decorators matter. For the example below, the capitalize_names decorator acts on the get_fullname function before the lump_names decorator does.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d1e11c3032f18f8ce085448096f9d6db/href">https://medium.com/media/d1e11c3032f18f8ce085448096f9d6db/href</a></iframe><p><strong>Passing arguments to a decorator</strong></p><p>Sometimes it is useful for decorators to take in arguments besides the function they are decorating. This kind of technique is used often for things like function registration. A famous example of this is view configuration within the Django web application framework. For example:</p><pre>from django.contrib.auth.decorators import login_required<br><br>@login_required(login_url=&#39;/accounts/login/&#39;)<br>def my_view(request):<br>    ...</pre><p>The login_required decorator modifies the my_view function and also provides a login_url argument which is available to the my_view function via closures and may be required during the execution of the function.</p><p>For example, let us extend the capitalize_names decorator to also repeat the fullname for an arbitrary number of times</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9d3e404302544404ebadd27f7be4de89/href">https://medium.com/media/9d3e404302544404ebadd27f7be4de89/href</a></iframe><p>Decorators expect to receive a function as an argument, that is why we will have to build a function that takes those extra arguments and generate our decorator on the fly. In the example above, repeat_capitalized_name is our decorator generator.</p><p><strong>Preserving function Metadata after decoration</strong></p><p>Simply put, decorators are just wrapping our functions. Hence, a decorated function carries the metadata of the wrapping function of the decorator.</p><p>Based on the last example, if we do</p><pre>print(get_fullname.__name__)<br>#Output: func_wrapper</pre><p>The output was meant to be get_fullname, yet the __name__ metadata of the get_fullname function got overridden by that of the wrapping func_wrapper function of the decorator function.</p><p>Fortunately Python includes the <a href="http://docs.python.org/2/library/functools.html">functools</a> module which contains <a href="http://docs.python.org/2/library/functools.html#functools.wraps">functools.wraps</a>. Wraps is a decorator for updating the attributes of the wrapping function(func_wrapper) to those of the original function(get_fullname). This is as simple as decorating func_wrapper by @wraps(func). Here is the updated example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0a3abd76672c4a7498efb9c7417280da/href">https://medium.com/media/0a3abd76672c4a7498efb9c7417280da/href</a></iframe><h3>PRACTICAL APPLICATION OF DECORATOR</h3><blockquote>Problem: Implement a retry decorator that takes can be used to initiate retries for a database connection if an exception occurs. It should take as parameters the number of retries and delay between each retry.</blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6c163fa95d86728b19e5e03014d553d5/href">https://medium.com/media/6c163fa95d86728b19e5e03014d553d5/href</a></iframe><p>Hope this article helps to demystify the concept of decorators in python.</p><p>Cheers.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d4ed0747e533" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>