<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: André Diego Piske</title>
    <description>The latest articles on Forem by André Diego Piske (@andrepiske).</description>
    <link>https://forem.com/andrepiske</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F96600%2F6706f6da-f01e-452c-af3d-6f1b4334bb45.jpeg</url>
      <title>Forem: André Diego Piske</title>
      <link>https://forem.com/andrepiske</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/andrepiske"/>
    <language>en</language>
    <item>
      <title>Ruby cryptographic gems</title>
      <dc:creator>André Diego Piske</dc:creator>
      <pubDate>Sat, 04 Jun 2022 08:40:27 +0000</pubDate>
      <link>https://forem.com/andrepiske/ruby-cryptographic-gems-1p5o</link>
      <guid>https://forem.com/andrepiske/ruby-cryptographic-gems-1p5o</guid>
      <description>&lt;p&gt;I'm not an expert in cryptography — I'm just a developer, and most developers are in this same boat of not being experts in cryptography.&lt;br&gt;
This doesn't mean we're negligent by not studying this field, this just means it isn't our area of expertise.&lt;br&gt;
What is negligent, however, is for someone like us to roll out their own cryptographic algorithms or libraries without knowing what they're doing.&lt;/p&gt;

&lt;p&gt;Thus, &lt;a href="https://security.stackexchange.com/a/18198"&gt;never roll your own cryptography&lt;/a&gt; unless you absolutely know what you're doing. Because of that, I want to explore some cryptography gems that awesome people brought to the Ruby ecosystem.&lt;/p&gt;
&lt;h2&gt;
  
  
  bcrypt
&lt;/h2&gt;

&lt;p&gt;The first one is &lt;a href="https://rubygems.org/gems/bcrypt"&gt;bcrypt&lt;/a&gt;. It dals with a very common use case: storing user passwords in databases and checking them when signing a user in.&lt;/p&gt;

&lt;p&gt;Here is some code demonstrating its usage. Explanation comes below.&lt;/p&gt;


&lt;div class="ltag__replit"&gt;
  &lt;iframe height="550px" src="https://repl.it/@andrepiske/CryptStuff?lite=true"&gt;&lt;/iframe&gt;
&lt;/div&gt;


&lt;p&gt;The &lt;code&gt;user_input&lt;/code&gt; variable is some password the user has input. Perhaps through a sign in form or through a account creation form. Let's assume the latter scenario for now.&lt;/p&gt;

&lt;p&gt;So, the user is creating an account and they submitted the password they wish to use later to sign in. How to store such password in the database?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;pwd&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;BCrypt&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Password&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user_input&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It is this simple. Now, just store &lt;code&gt;pwd.to_s&lt;/code&gt; in a text field for the user password.&lt;br&gt;
If you check the contents of &lt;code&gt;pwd.to_s&lt;/code&gt;, you'll notice it looks something like&lt;br&gt;
&lt;code&gt;$2a$12$ccaJDXyKniehBeYgZM4wDOl91.zctTI03qPhOlDGVk5KZ1qcC9Hge&lt;/code&gt;. The value for you will likely be different, even though the password is the same, because there is &lt;a href="https://en.wikipedia.org/wiki/Salt_(cryptography)"&gt;a salt&lt;/a&gt; in the mix. Just to be clear: that funny string is the hashed version of the password.&lt;/p&gt;

&lt;p&gt;Now, what to do with that funny string? You use it when the user is signing in, to check whether they typed the correct password. Remember that what will be stored in the "password" field in the database will be that funny value. So let's say a user is trying to sign in. They provide you their email address and the password. With the email address, you can fetch the entity in the database that has their hashed password (the funny string).&lt;/p&gt;

&lt;p&gt;It's then just a matter of comparing the hashed password to what the user provided in the sign in form:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="c1"&gt;# User provided this in the sign in form&lt;/span&gt;
&lt;span class="n"&gt;user_input&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'9fn837nf'&lt;/span&gt;

&lt;span class="c1"&gt;# The hashed password as stored in your database&lt;/span&gt;
&lt;span class="n"&gt;pwd_in_database&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'$2a$12$ccaJDXyKniehBeYgZM4wDOl91.zctTI03qPhOlDGVk5KZ1qcC9Hge'&lt;/span&gt;

&lt;span class="n"&gt;pwd&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;BCrypt&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Password&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pwd_in_database&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;pwd&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;user_input&lt;/span&gt;
  &lt;span class="nb"&gt;puts&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'user sign in successfully'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;
  &lt;span class="nb"&gt;puts&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'wrong password'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  rbnacl
&lt;/h2&gt;

&lt;p&gt;The other gem I want to explore is &lt;a href="https://rubygems.org/gems/rbnacl"&gt;rbnacl&lt;/a&gt;.&lt;br&gt;
This gem provides general purpose cryptography for many different scenarios and algorithms. They do so in a simplified way so that mortals like us don't have to become cryptography experts. Check out &lt;a href="https://github.com/RubyCrypto/rbnacl/wiki#easier-to-use-cryptography%5D"&gt;these docs&lt;/a&gt; to see what I'm talking about!&lt;/p&gt;
&lt;h3&gt;
  
  
  Symmetric encryption
&lt;/h3&gt;

&lt;p&gt;Say you want to have a key that allows to encrypt some data and later to decrypt that data. It's easy as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Generates a random key with the correct length&lt;/span&gt;
&lt;span class="n"&gt;key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;RbNaCl&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Random&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;random_bytes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;RbNaCl&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;SecretBox&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;key_bytes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;sbox&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;RbNaCl&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;SimpleBox&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;from_secret_key&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;encrypted_data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sbox&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;encrypt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'hello world!'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You'll have to store the &lt;code&gt;key&lt;/code&gt; somewhere safe. Note that it is a binary string, so you might want to base64-encode it to send it around. I generated the key that when base64-encoded is: &lt;code&gt;nveFTikebVaqd4SMzCZ5P7BAKv6BeNwAqowTFkJbHjY=&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;encrypted_data&lt;/code&gt; is also binary data. Encoding it in base64, it turns into:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;LbmIxONTUZXQGzGQwK1gB29H0OxoS8bn0GE/QAzJXt/8K9WNFnaz6XX3F0ecFwZRb9mm9Q==
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, to decrypt the data, one only needs that key and the encrypted payload. Let's take a look:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s1"&gt;'rbnacl'&lt;/span&gt;
&lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s1"&gt;'base64'&lt;/span&gt;

&lt;span class="n"&gt;encrypted_payload&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Base64&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;decode64&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="s1"&gt;'LbmIxONTUZXQGzGQwK1gB29H0OxoS8bn0GE/QAzJXt/8K9WNFnaz6XX3F0ecFwZRb9mm9Q=='&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Base64&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;decode64&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'nveFTikebVaqd4SMzCZ5P7BAKv6BeNwAqowTFkJbHjY='&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;sbox&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;RbNaCl&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;SimpleBox&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;from_secret_key&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sbox&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;decrypt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;encrypted_payload&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nb"&gt;puts&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This script will print &lt;code&gt;hello world!&lt;/code&gt;, which was the original message.&lt;/p&gt;

&lt;p&gt;I didn't use replit here because &lt;code&gt;rbnacl&lt;/code&gt; requires libsodium to be installed in the OS and I didn't manage to get that installed in replit. The folks at rbnacl provide &lt;a href="https://github.com/RubyCrypto/rbnacl/wiki/Installing-libsodium"&gt;some help on how to install that&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;That's a very simple way of encrypting data without having much knowledge about cryptography. The gem also provides ways to have more control on the encryption, like choosing the encryption algorithm. But then you'll have to dig by yourself in the documentation — which by the way are just great.&lt;/p&gt;

&lt;p&gt;If you know other gems on cryptography for Ruby, drop a comment!&lt;/p&gt;




&lt;p&gt;Cover image by &lt;a href="https://unsplash.com/es/@markusspiske?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Markus Spiske&lt;/a&gt; on &lt;a href="https://unsplash.com/s/photos/matrix?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Unsplash&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>security</category>
    </item>
    <item>
      <title>I'm creating Lets Code videos! [feedback wanted]</title>
      <dc:creator>André Diego Piske</dc:creator>
      <pubDate>Sat, 20 Feb 2021 08:52:46 +0000</pubDate>
      <link>https://forem.com/andrepiske/i-m-creating-lets-code-videos-feedback-wanted-322e</link>
      <guid>https://forem.com/andrepiske/i-m-creating-lets-code-videos-feedback-wanted-322e</guid>
      <description>&lt;p&gt;I came to this: I will create content for programmers (that's you!)&lt;/p&gt;

&lt;p&gt;I want to do relevant content for those who either want to learn software development from scratch or improve their skills. I've been in the programming field for 10+ years and it's already time to share some knowledge.&lt;/p&gt;

&lt;p&gt;Here is something I've been working on:&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/1B6iq3blBMI"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Next video on that series goes online later today 💪&lt;/p&gt;

&lt;p&gt;👉 Would &lt;strong&gt;very much&lt;/strong&gt; like to hear your feedback!&lt;/p&gt;

&lt;p&gt;My content focus on technologies I know very well and have been working with in the last few years: Ruby and Javascript for programming languages, a backend-ish full-stack career and devops.&lt;/p&gt;

&lt;p&gt;Let me know what you think!&lt;/p&gt;

</description>
    </item>
    <item>
      <title>How profiling my slow Ruby code led me to publish my first gem!</title>
      <dc:creator>André Diego Piske</dc:creator>
      <pubDate>Tue, 24 Mar 2020 18:15:16 +0000</pubDate>
      <link>https://forem.com/andrepiske/how-profiling-my-slow-ruby-code-led-me-to-publish-my-first-gem-2a56</link>
      <guid>https://forem.com/andrepiske/how-profiling-my-slow-ruby-code-led-me-to-publish-my-first-gem-2a56</guid>
      <description>&lt;p&gt;My current side project is a &lt;a href="https://en.wikipedia.org/wiki/HTTP/2"&gt;HTTP/2&lt;/a&gt; server, fully written in Ruby. This protocol is a major improvement in comparison to its antecessor, HTTP/1.1, but it also brings along a much higher complexity.&lt;/p&gt;

&lt;p&gt;The major change is in the way data is transported over the wire. For instance, when downloading a file through http/1.1, after the headers were exchanged, a server only has to read the file contents from disk and forward them to the client, without any processing involved.&lt;/p&gt;

&lt;p&gt;This is not true in http/2. Instead, a server has to send the file contents in chunks. Every one of those chunks has to be wrapped in what the protocol specification calls a &lt;em&gt;frame&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;A frame is composed of a header and a payload. Here is a depiction of a frame in HTTP/2, borrowed straight from &lt;a href="https://http2.github.io/http2-spec/#rfc.section.4"&gt;the spec&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; +-----------------------------------------------+
 |                 Length (24)                   |
 +---------------+---------------+---------------+
 |   Type (8)    |   Flags (8)   |
 +-+-------------+---------------+-------------------------------+
 |R|                 Stream Identifier (31)                      |
 +=+=============================================================+
 |                   Frame Payload (0...)                      ...
 +---------------------------------------------------------------+
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;That is all binary data. That &lt;code&gt;Length (24)&lt;/code&gt; there is a 24 bits long field, and it stores length of the payload inside the frame.&lt;/p&gt;
&lt;h1&gt;
  
  
  Where it starts
&lt;/h1&gt;

&lt;p&gt;That binary thing, dealing with bits and bytes, that is where the problem starts.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--KkCAnchs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/28snsjpdrfhoiops86yt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KkCAnchs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/28snsjpdrfhoiops86yt.png" alt="4.4 MB/s at full speed" width="880" height="238"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That picture depicts how slow my server was before I could optimize it. The URL points to localhost, the picture shows a file being downloaded at 4.4 MB/s. Such speed is painfully slow, it should be instantaneous! Downloading a file from a localhost server that is serving only one request should be really fast.&lt;/p&gt;

&lt;p&gt;During the file download, the server was showing 100% CPU usage. This gave the insight that the reason of the bad performance could be the fact that ruby was really busy doing stuff. But why so busy? &lt;em&gt;Doing what?&lt;/em&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  To the profiler!
&lt;/h1&gt;

&lt;p&gt;The best way to know what keeps a program busy is to get to the data, and getting the data means profiling the code. Fortunately, and thanks to some very nice people out there putting effort writing up that gem, ruby has &lt;a href="https://rubygems.org/gems/stackprof"&gt;stackprof&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Setting up the gem is very straightforward. I added a &lt;code&gt;StackProf.start&lt;/code&gt; call before the code I was suspecting to be slow and also a &lt;code&gt;StackProf.stop&lt;/code&gt; after it. This is the code:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;iterate&lt;/span&gt;
  &lt;span class="no"&gt;StackProf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;start&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;mode: :cpu&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="vi"&gt;@config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;enable_profiling?&lt;/span&gt;

  &lt;span class="n"&gt;writables&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;each&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="ss"&gt;:notify_writeable&lt;/span&gt;
  &lt;span class="n"&gt;readables&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;each&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="ss"&gt;:notify_readable&lt;/span&gt;
  &lt;span class="n"&gt;new_clients&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;each&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;io&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
    &lt;span class="n"&gt;accept_client_connection&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;io&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="no"&gt;StackProf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stop&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="vi"&gt;@config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;enable_profiling?&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;The &lt;code&gt;iterate&lt;/code&gt; method above is run multiple times in a loop that only stops when the server is shut down. I only wanted to profile that portion, because that's where most of the stuff happens.&lt;/p&gt;

&lt;p&gt;I wanted to be able to run the server with and without the profiler, so I made it configurable via those &lt;code&gt;@config.enable_profiling?&lt;/code&gt; probes.&lt;/p&gt;

&lt;p&gt;I also had to add some code to finish up the profiler. I added that in the code that calls the method above. It looks like this:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;run_forever&lt;/span&gt;
  &lt;span class="kp"&gt;loop&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="n"&gt;iterate&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="vi"&gt;@signaled&lt;/span&gt;
      &lt;span class="no"&gt;Debug&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;info&lt;/span&gt; &lt;span class="s1"&gt;'Signaled, exiting...'&lt;/span&gt;

      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="vi"&gt;@config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;enable_profiling?&lt;/span&gt;
        &lt;span class="no"&gt;Debug&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;info&lt;/span&gt; &lt;span class="s1"&gt;'Dumping profile info...'&lt;/span&gt;

        &lt;span class="no"&gt;StackProf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;results&lt;/span&gt; &lt;span class="vi"&gt;@config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;profiling_result_file&lt;/span&gt;

        &lt;span class="no"&gt;Debug&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;info&lt;/span&gt; &lt;span class="s1"&gt;'Done'&lt;/span&gt;
      &lt;span class="k"&gt;end&lt;/span&gt;

      &lt;span class="k"&gt;return&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;The &lt;code&gt;run_forever&lt;/code&gt; method above runs the loop that only finishes once the server is shut down. When the &lt;code&gt;@signaled&lt;/code&gt; variable evaluates to &lt;em&gt;true&lt;/em&gt;, the server has to shut down. The important piece of code is the line that calls &lt;code&gt;StackProf.results&lt;/code&gt;. It will dump all the profiler data collected so far to a file.&lt;/p&gt;

&lt;p&gt;Then I ran the server, fired some downloads using &lt;code&gt;curl&lt;/code&gt; and stopped the server. The profiler results were now dumped into a file. To visualize them, there is another awesome ruby gem called &lt;a href="https://rubygems.org/gems/stackprof-webnav"&gt;stackprof-webnav&lt;/a&gt;. It provides a command to view the profiler results. The following installs it and gets it running:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;gem &lt;span class="nb"&gt;install &lt;/span&gt;stackprof-webnav
&lt;span class="nv"&gt;$ &lt;/span&gt;stackprof-webnav
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;That has to be run in the same folder where the profiler result files are stored. It opens a web server and then there is a web UI to view the profiler results.&lt;/p&gt;
&lt;h1&gt;
  
  
  Finding the bottleneck
&lt;/h1&gt;

&lt;p&gt;Have done the first run with the profiler on, the results revealed the following:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--IAKZvRCv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/w2z91jq08s9u7z68wlj9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--IAKZvRCv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/w2z91jq08s9u7z68wlj9.png" alt="Alt Text" width="880" height="351"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This was a profile run of that file download operation. And that has a lot to say: 42% of the time just running &lt;code&gt;String.unpack&lt;/code&gt;! It's almost half of everything that is being done there.&lt;/p&gt;

&lt;p&gt;It struck me. Since HTTP/2 is a binary based protocol, the server needs to deal with bitwise operations to deliver data. Remember how I told before that every chunk of data has to be wrapped in &lt;em&gt;frames&lt;/em&gt;?&lt;/p&gt;

&lt;p&gt;There are two important things there: &lt;em&gt;chunk&lt;/em&gt; and &lt;em&gt;wrapped&lt;/em&gt;. The http/2 spec dictates that the maximum size of a frame is only 16 KiB long. This means that every 16 KiB of data has to be &lt;em&gt;wrapped&lt;/em&gt; into a frame. That is a lot of processing involved.&lt;/p&gt;

&lt;p&gt;Wrapping the chunk is a simple but repetitive operation. And repetitions, namely loops, are the Achilles' heel of interpreted languages like Ruby. If the frames could be larger, it would be possible to wrap it less often, which would more easily allow for a higher performance.&lt;/p&gt;

&lt;p&gt;Nevertheless, here is the code of that slowest method call, the &lt;code&gt;BitWriter#write_bytes&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def write_bytes value
  value = value.unpack('C*') if value.is_a?(String)
  @buffer += value
  @cursor += value.length
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;The &lt;code&gt;BitWriter&lt;/code&gt; class is responsible for writing binary data into http/2 frames. It has methods to write bytes, &lt;a href="https://tools.ietf.org/html/rfc7541#section-5.1"&gt;integers&lt;/a&gt; and &lt;a href="https://tools.ietf.org/html/rfc7541#section-5.2"&gt;strings&lt;/a&gt;. The &lt;code&gt;write_bytes&lt;/code&gt; method just writes an array of bytes, so it only has to copy the data.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;BitWriter&lt;/code&gt; stores all the data internally as an &lt;code&gt;Array&lt;/code&gt; of which element is a number from 0 to 255 - thus, a byte. The &lt;code&gt;write_bytes&lt;/code&gt; method accepts either an array of bytes as input or a &lt;code&gt;String&lt;/code&gt;. For the latter case, it has to first convert it to an array of numbers (bytes). That conversion &lt;em&gt;should&lt;/em&gt; be a very simple operation. But that doesn't seem to be what the data is showing there.&lt;/p&gt;

&lt;p&gt;My first suspect is the &lt;code&gt;String#unpack&lt;/code&gt;, because I knew that most of the calls were actually passing a &lt;code&gt;String&lt;/code&gt; to the method. Performing a quick benchmark of the &lt;code&gt;String#unpack&lt;/code&gt; method, I concluded it was in fact much more slower than I had anticipated. You can check it yourself how it performs, just hit the Run button below:&lt;/p&gt;


&lt;div class="ltag__replit"&gt;
  &lt;iframe height="550px" src="https://repl.it/@andrepiske/RubberyLinedClosedsource?lite=true"&gt;&lt;/iframe&gt;
&lt;/div&gt;



&lt;p&gt;That took 6.8 seconds to run on my machine. And that's only 10 thousand times!&lt;/p&gt;

&lt;p&gt;Going back to the benchmark results above, the second most busy method call was in OpenSSL's &lt;code&gt;write_nonblock&lt;/code&gt; method. That is the method that actually sends the data to the client. However that is a third party library that I have no control over, so I skipped it and went to look after the &lt;code&gt;bytes_array&lt;/code&gt; method of the &lt;code&gt;BitWriter&lt;/code&gt; class.&lt;/p&gt;

&lt;p&gt;This class of mine was causing trouble. Two methods of it together are using almost 70% of the CPU time alone. And it shocks me again, since the code for the &lt;code&gt;bytes_array&lt;/code&gt; method is even simpler than the other one. It is exactly this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;bytes_array&lt;/span&gt;
  &lt;span class="n"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pack&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'C*'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Here, &lt;code&gt;bytes&lt;/code&gt; is just an alias to the variable that stores the internal bytes. It is an &lt;code&gt;Array&lt;/code&gt; of numbers. I just had found out that the &lt;code&gt;unpack&lt;/code&gt; method is very slow, and now I'm about to find out that &lt;code&gt;pack&lt;/code&gt; is also very slow. Doing a benchmark in the same manner as before:&lt;/p&gt;


&lt;div class="ltag__replit"&gt;
  &lt;iframe height="550px" src="https://repl.it/@andrepiske/UnwittingTautKeygens?lite=true"&gt;&lt;/iframe&gt;
&lt;/div&gt;



&lt;p&gt;This one took about 7.3 seconds on my machine. Again, very slow.&lt;/p&gt;

&lt;p&gt;I tried playing around with different ways of achieving the same results. I even went to look into Ruby's source code to check out how that was implemented to why it was so slow. Hint: Reading ruby's source code in C is not that easy. So I went to look for another solution.&lt;/p&gt;

&lt;h1&gt;
  
  
  The solution
&lt;/h1&gt;

&lt;p&gt;I had played with buffers in JavaScript in the past using classes like the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView"&gt;DataView&lt;/a&gt; and &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer"&gt;ArrayBuffer&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Then I thought to myself: What if I had those classes in Ruby? &lt;/p&gt;

&lt;p&gt;I went to search for something like that, but couldn't find it. So I thought I should do it myself. Finding out that there was no such thing for Ruby was an additional motivation for me, since I'd be filling a gap!&lt;/p&gt;

&lt;p&gt;There was another thing that motivated me. It had been quite a while since I wanted to learn how to integrate Ruby code with C code. That was it. I could make it in C, which would be great for achieving the performance needs, and use it in Ruby!&lt;/p&gt;

&lt;p&gt;And that is how I launched the arraybuffer gem:&lt;/p&gt;


&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--566lAguM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev.to/assets/github-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/andrepiske"&gt;
        andrepiske
      &lt;/a&gt; / &lt;a href="https://github.com/andrepiske/rb-arraybuffer"&gt;
        rb-arraybuffer
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      Low level byte operators and buffers for Ruby
    &lt;/h3&gt;
  &lt;/div&gt;
  &lt;div class="ltag-github-body"&gt;
    
&lt;div id="readme" class="md"&gt;
&lt;h1&gt;
arraybuffer gem&lt;/h1&gt;
&lt;p&gt;&lt;a rel="noopener noreferrer" href="https://github.com/andrepiske/rb-arraybuffer/actions/workflows/ci.yml/badge.svg"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_0hZzFvQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://github.com/andrepiske/rb-arraybuffer/actions/workflows/ci.yml/badge.svg" alt="Tests"&gt;&lt;/a&gt;
&lt;a href="https://badge.fury.io/rb/arraybuffer" rel="nofollow"&gt;&lt;img src="https://camo.githubusercontent.com/9f061a43d2237c5911fcabd3128fd59061cba6cf403afdc9c897c2cf027f4c7a/68747470733a2f2f62616467652e667572792e696f2f72622f61727261796275666665722e737667" alt="Gem Version"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1&gt;
What?&lt;/h1&gt;
&lt;p&gt;Ruby lacks classes like array buffer or byte array.&lt;/p&gt;
&lt;p&gt;This gem aims to solve that by implementing that natively in C so that
a decent performance can be achieved.&lt;/p&gt;
&lt;p&gt;The design of the classes follows standards that are implemented in the
Web world, like:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" rel="nofollow"&gt;DataView&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" rel="nofollow"&gt;ArrayBuffer&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The standards above are, however, not strictly followed.&lt;/p&gt;
&lt;h1&gt;
Why?&lt;/h1&gt;
&lt;p&gt;The only way I found to do this was manipulating an array of numbers
treating them as bytes and using &lt;code&gt;buffer.pack('C*')&lt;/code&gt; to transform it
into a &lt;code&gt;String&lt;/code&gt; object. However, for some reason the &lt;code&gt;Array#pack&lt;/code&gt; method
is painfully slow.&lt;/p&gt;
&lt;p&gt;Other gems exist out there, like the class
&lt;a href="https://www.rubydoc.info/gems/nio4r/2.5.2/NIO/ByteBuffer" rel="nofollow"&gt;ByteBuffer&lt;/a&gt;
class in &lt;a href="https://rubygems.org/gems/nio4r" rel="nofollow"&gt;nio4r&lt;/a&gt;. However, they
didn't had a design I was satisfied with or they had different purposes
that wouldn't suit my use case :)&lt;/p&gt;
&lt;h1&gt;
What about JRuby?&lt;/h1&gt;
&lt;p&gt;Feel free to open a pull request for that!&lt;/p&gt;
&lt;/div&gt;



&lt;/div&gt;
&lt;br&gt;
  &lt;div class="gh-btn-container"&gt;&lt;a class="gh-btn" href="https://github.com/andrepiske/rb-arraybuffer"&gt;View on GitHub&lt;/a&gt;&lt;/div&gt;
&lt;br&gt;
&lt;/div&gt;
&lt;br&gt;


&lt;p&gt;After &lt;a href="https://github.com/andrepiske/wowhttp/blob/852ea061fe142414b48154ac365e63ed5cc711f1/lib/appmaker/net/h2/bit_writer.rb"&gt;changing all&lt;/a&gt; of the &lt;code&gt;BitWriter&lt;/code&gt; code to use the &lt;code&gt;arraybuffer&lt;/code&gt; gem with the &lt;code&gt;ArrayBuffer&lt;/code&gt; and &lt;code&gt;DataView&lt;/code&gt; classes, I went to profile it again. The result was really encouraging:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--hRg4eHJg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/fj99cqvdby1yq2pzhszs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--hRg4eHJg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/fj99cqvdby1yq2pzhszs.png" alt="Really good results!" width="880" height="353"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Amazing! Now the next challenging bottleneck is the OpenSSL writing operation. I have no idea on how to optimize that yet, but will surely look for a way. Coming in second and third places is the mark-and-sweep garbage collector in action. My code is probably producing too much trash. That will also be challenging to profile and optimize. I hope that renders me enough material to write a post!&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>showdev</category>
      <category>performance</category>
    </item>
    <item>
      <title>Building my next HTTP server, part 2</title>
      <dc:creator>André Diego Piske</dc:creator>
      <pubDate>Tue, 18 Feb 2020 20:14:49 +0000</pubDate>
      <link>https://forem.com/andrepiske/building-my-next-http-server-part-2-466g</link>
      <guid>https://forem.com/andrepiske/building-my-next-http-server-part-2-466g</guid>
      <description>&lt;p&gt;&lt;em&gt;This is the second post of a series about my HTTP server&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;On my first post of this series I explained how the plan for my HTTP server is for it to be asynchronous, so it makes the best use of I/O and CPU at the same time.&lt;/p&gt;

&lt;p&gt;In order to be asynchronous, the problem I have to solve is that a call to &lt;a href="https://ruby-doc.org/stdlib-2.5.0/libdoc/socket/rdoc/TCPServer.html#method-i-accept"&gt;&lt;code&gt;TCPServer#accept&lt;/code&gt;&lt;/a&gt; or to &lt;a href="https://ruby-doc.org/core-2.5.0/IO.html#method-i-read"&gt;&lt;code&gt;TCPServer#read&lt;/code&gt;&lt;/a&gt; is a blocking call. This means that the method call will block the execution flow until there is something to be read from the other side.&lt;/p&gt;

&lt;p&gt;If there are two clients connected to the server and one of the clients gets stuck, it may block the whole server. A single client must never be able to block a whole server!&lt;/p&gt;

&lt;p&gt;There are a few technologies that can be used to solve this issue. But all of them have the same basic idea behind them. If we think about a blocking read operation, we can break it down into two pieces. Let's take those pieces to picture what could be the implementation of the &lt;code&gt;TCPServer#read&lt;/code&gt; method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;TCPServer&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;read&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;wait_until_bytes_available&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;read_available_bytes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Of course those two methods being called are fictitious, only for the purpose of illustrating. The idea is that the &lt;code&gt;wait_until_bytes_available&lt;/code&gt; method waits until &lt;code&gt;length&lt;/code&gt; amount of bytes are available to be read from the wire. This is where the actual blocking occurs, as this method will only return when there is enough bytes to be read. If the client never sends more data, the method would not return. In reality, the method would return with an error state if the connection is broken.&lt;/p&gt;

&lt;p&gt;After that waiting, the &lt;code&gt;read_available_bytes&lt;/code&gt; method call then does the actual reading. It reads &lt;code&gt;length&lt;/code&gt; amount of bytes from the wire without blocking and then returns those bytes.&lt;/p&gt;

&lt;p&gt;Now, in order to have it working asynchronously, it's just a matter of removing the waiting part. That is done by removing the &lt;code&gt;wait_until_bytes_available&lt;/code&gt; method call. Now we're only left with the actual reading method. And ruby, in fact, has a method just for that. It's the &lt;a href="https://ruby-doc.org/core-2.5.0/IO.html#method-i-read_nonblock"&gt;&lt;code&gt;IO#read_nonblock&lt;/code&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;#read_nonblock&lt;/code&gt; method call takes one argument that is how many bytes should be read &lt;em&gt;at maximum&lt;/em&gt;. That is, the method will never read more bytes than what was passed in the argument, but it can read less than it in case there just isn't enough bytes available to be read.&lt;/p&gt;

&lt;p&gt;Now, the &lt;code&gt;read&lt;/code&gt; method is not the only one that will block. We also have to solve the issue with the &lt;code&gt;accept&lt;/code&gt; method. And that is very easy, because Ruby has the &lt;a href="https://ruby-doc.org/stdlib-2.5.0/libdoc/socket/rdoc/TCPServer.html#method-i-accept_nonblock"&gt;&lt;code&gt;TCPServer#accept_nonblock&lt;/code&gt;&lt;/a&gt; method! Also, which will be needed later, there is the non-blocking counterpart for the &lt;code&gt;write&lt;/code&gt; method, which is the, you guessed it, &lt;a href="https://ruby-doc.org/core-2.5.0/IO.html#method-i-write_nonblock"&gt;&lt;code&gt;IO#write_nonblock&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Those &lt;code&gt;*_nonblock&lt;/code&gt; methods are the way to go from here. But they introduce a lot of other difficulties to be dealt with. For instance, what should be done if there is nothing to read from the wire right now? That doesn't mean there won't be anything in the near future. Also, since the &lt;code&gt;read_nonblock&lt;/code&gt; method can read less bytes than specified in its argument, this would mean that the data can be received in small pieces. How to manage those pieces and process them together afterwards?&lt;/p&gt;

&lt;p&gt;I intend to cover those issues in the next post of this series, so see you there!&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>webdev</category>
      <category>showdev</category>
    </item>
    <item>
      <title>Building my next HTTP server</title>
      <dc:creator>André Diego Piske</dc:creator>
      <pubDate>Wed, 20 Nov 2019 20:27:39 +0000</pubDate>
      <link>https://forem.com/andrepiske/building-my-next-http-server-4le3</link>
      <guid>https://forem.com/andrepiske/building-my-next-http-server-4le3</guid>
      <description>&lt;p&gt;&lt;em&gt;This is my first post in what I intend to be a series. It's based on my experience of building an HTTP/(1.1|2) server in Ruby.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;So, I decided to hop into my next side project. I wanted to create a simple thing. But I wanted to make that thing not in the traditional way that I already know how to, but in a different one. I wanted to do a simple thing using a different approach from what I would normally use.&lt;/p&gt;

&lt;p&gt;What about an HTTP server? Seems like a good idea. I already know the nuts and bolts of the protocol, already did it in the past, so it would, in a sense, be a simple thing. You know, it's simple: open a TCP socket and wait for someone to perform a request. Once they're there, process the request and give back a nice response. Then, unless keep-alive is specified, close the client socket.&lt;/p&gt;

&lt;p&gt;So far so good. The code would look something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s2"&gt;"socket"&lt;/span&gt;

&lt;span class="n"&gt;server&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;TCPServer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt; &lt;span class="mi"&gt;3000&lt;/span&gt;
&lt;span class="kp"&gt;loop&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;accept&lt;/span&gt;
  &lt;span class="n"&gt;deal_with_http_stuff&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;
  &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;close&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;(Oh, by the way, I'm using Ruby. It's kinda my favorite language novadays.)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Then let the magic function &lt;code&gt;deal_with_http_stuff&lt;/code&gt; just read the HTTP headers and &lt;code&gt;send()&lt;/code&gt; back the result to the client.&lt;/p&gt;

&lt;p&gt;Now, the &lt;code&gt;deal_with_http_stuff&lt;/code&gt; function can be fairly simple. It can also get farily complex, like when doing some FastCGI stuff or acting as a reverse proxy. Despite those, that's not where the "different approach" that I'm looking for would be. There is a different kind of complexity that can be added, and that is where I would like to experiment with different approach.&lt;/p&gt;

&lt;p&gt;But before diving that way, let's quickly analyze the solution above. Upon the connection of a client, the next steps are to receive the request information, process it and send back the results. It seems simple at first, but there already are some immediate issues.&lt;/p&gt;

&lt;p&gt;The first one is that while one request is being read, processed and then having the response be sent back to the client, other requests will have to wait in the line. That is, this server can only process one request at a time. It lacks any parallelism or concurrency capabilities.&lt;/p&gt;

&lt;p&gt;A common and relatively easy solution for this would be to delegate the &lt;code&gt;deal_with_http_stuff&lt;/code&gt; processing to a separate processing thread or even a different process (by using &lt;code&gt;fork&lt;/code&gt;). The threaded version of the code could look a bit like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s2"&gt;"socket"&lt;/span&gt;

&lt;span class="n"&gt;server&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;TCPServer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt; &lt;span class="mi"&gt;3000&lt;/span&gt;
&lt;span class="kp"&gt;loop&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;accept&lt;/span&gt;
  &lt;span class="no"&gt;Thread&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="n"&gt;deal_with_http_stuff&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;
    &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;close&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;fork&lt;/code&gt; approach would look similar to that. But those approaches have their own issues as well. Also, that is not the way I want to go. I have been there in the past already and I'm looking for a different kind of fun!&lt;/p&gt;

&lt;h3&gt;
  
  
  Enter asynchronicity
&lt;/h3&gt;

&lt;p&gt;As you may already be familiar with, nginx is a well known HTTP server out there. Somewhere in its wiki page, it's written that:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Unlike traditional servers, NGINX doesn’t rely on threads to handle requests. Instead it uses a much more scalable event-driven (asynchronous) architecture.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Right there. That is the approach I want to go with: &lt;em&gt;asynchronous architecture&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;But how is that then different from the threaded approach? Because threads are asynchronous citizens, aren't they? In fact they do. One could argue, though, that, in MRI ruby implementation, threads are not &lt;em&gt;really&lt;/em&gt; asynchronous, since MRI uses only one CPU core (in contrast to, for instance, JRuby, which does uses real threads offered by the operating system).&lt;/p&gt;

&lt;p&gt;It is exactly there where the difference lies upon. The threaded approach depends on the level of operating system and CPU to manage the asynchronicity. The same applies to the &lt;code&gt;fork&lt;/code&gt; approach. The developer writes code just as if everything was synchronous and the OS in partnership with the CPU are the ones doing the heavy lifting of scheduling different threads at different times to different CPU cores.&lt;/p&gt;

&lt;p&gt;The approach I want to use, which is what nginx uses, sort of moves that heavy lifting into the server itself. It is then the developer of the server the one who has to deal with the asynchronicity at all times (and by &lt;em&gt;all times&lt;/em&gt;, I do mean it!) This way, it is possible to serve multiple requests simultaneously using only one real thread -- that is, a single CPU core.&lt;/p&gt;

&lt;h3&gt;
  
  
  Two things
&lt;/h3&gt;

&lt;p&gt;The approach nginx uses, which I want to use, is really about two things: CPU and I/O. The concurrency that nginx uses, what it really addresses, is the fact that with just regular programming, either one of those are busy while the other is kept free.&lt;/p&gt;

&lt;p&gt;For instance, reading a file from disk means telling the disk driver to fetch the contents of that file and then waiting for it to return the file contents. It takes time. Likewise, sending a package of data to the network means waiting for the network driver to do all electrical signaling and stuff required to deliver that package. Setting a timer, like in Javascript's &lt;code&gt;setTimeout&lt;/code&gt; function, means just waiting for a certain amount of time to pass without even doing something useful at all.&lt;/p&gt;

&lt;p&gt;The issue then is that while some I/O is being performed, the CPU is not being used. By doing the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;content&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;File&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;read&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/tmp/foo"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;load&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nb"&gt;puts&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'foobar'&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the first statement makes the disk busy and frees the CPU from doing any work. It asks the disk for the file data and waits for it to finish reading all the file before proceeding to the next line of code. Once finished, the data is transferred to the JSON module for it to parse the string in memory, producing ruby objects along the way. The parsing itself is, of course, a CPU intensive and I/O-free task. The last statement just prints the data. But pay attention here, because that &lt;code&gt;puts&lt;/code&gt; instruction won't return until the statement has been printed, which also means waiting for the I/O to finish.&lt;/p&gt;

&lt;p&gt;What if we could just minimize the total amount of time that both CPU and I/O are free? That is, to keep them busy as possible. When CPU is already being used to the max, do some I/O. When I/O is being used to the max, do some CPU intensive tasks.&lt;/p&gt;

&lt;p&gt;This is exactly my plan, and I hope to explain how that works in future posts.&lt;/p&gt;

</description>
      <category>showdev</category>
      <category>webdev</category>
      <category>ruby</category>
    </item>
  </channel>
</rss>
