<?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 Yinon Eliraz on Medium]]></title>
        <description><![CDATA[Stories by Yinon Eliraz on Medium]]></description>
        <link>https://medium.com/@yinoneliraz?source=rss-eb8cfc6db017------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*e4ItrPdibd3yb_9D.jpg</url>
            <title>Stories by Yinon Eliraz on Medium</title>
            <link>https://medium.com/@yinoneliraz?source=rss-eb8cfc6db017------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 11:41:27 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@yinoneliraz/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[What can you do with interfaces in Golang]]></title>
            <link>https://medium.com/@yinoneliraz/what-can-you-do-with-interfaces-in-golang-160807e3ba63?source=rss-eb8cfc6db017------2</link>
            <guid isPermaLink="false">https://medium.com/p/160807e3ba63</guid>
            <category><![CDATA[interfaces]]></category>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[duck-typing]]></category>
            <dc:creator><![CDATA[Yinon Eliraz]]></dc:creator>
            <pubDate>Sun, 21 Apr 2019 08:43:00 GMT</pubDate>
            <atom:updated>2019-04-21T08:43:00.684Z</atom:updated>
            <content:encoded><![CDATA[<p>I have been writing code in golang for about 18 months now. One of the most misunderstood topics in golang is interfaces. As most programmers come from an object-oriented background, where interfaces are used for certain things, interfaces in golang can and should be used differently.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Uzd2n_pZTnQkCK0_MHE81w.jpeg" /></figure><p>Usually, when talking about interfaces, we see simple examples to show the basic usage of interfaces. However, when writing complex services, interfaces can be used entirely different.</p><p>Let’s say our service uses an external package to send service’s metrics. The external package exposes many methods to send events, named metrics and many more. But our service only needs to send one type of metric, and the rest of the API is useless to us.</p><p>We would also like to write testable code, which in golang (also) means writing mockable components. So when we want to test our component’s integration with the external service, we will need to mock it. If the external service has an interface we can mock it all, this has 2 problems:<br>1. If someone will read our code, it will be hard to understand which methods are being used by our component.<br>2. We will get a massive mock, but in reality, we will use a fraction of it.</p><p>And if the service does not have an interface, we just have to create one for mocking. So what should we do?</p><p>Now golang’s interfaces come in handy. All we need to do is to create an interface that only includes the methods we need. Since golang associates structs with interfaces in a <a href="https://en.wikipedia.org/wiki/Duck_typing">duck typing</a> fashion, the struct will automatically “implement” the interface. That way we can mock this interface and pass it to our service as the external service. Now we have a clean mock and interface, and it is clear to readers what our service needs.</p><p>Another usage for interfaces, that has been introduced in almost every chance, is using a signature as an abstraction of components. If we stick with the monitoring example, we now can have 2 external providers we want to report metrics to, both have the same usage but are simply different ones. In this case, we can use our interface for both of them and simply keep them as a map of our monitoring services. Every time we would like to report a metric we will iterate through the map and call the report method.</p><p>After applying these practices to my code, I got a more understandable code, cleaner and testable. I hope these examples helped you understand another way to use interfaces.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=160807e3ba63" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Get to Know the Transport Object]]></title>
            <link>https://medium.com/@yinoneliraz/get-to-know-the-transport-object-1b6f9f66abf5?source=rss-eb8cfc6db017------2</link>
            <guid isPermaLink="false">https://medium.com/p/1b6f9f66abf5</guid>
            <category><![CDATA[http-client]]></category>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[network-protocols]]></category>
            <dc:creator><![CDATA[Yinon Eliraz]]></dc:creator>
            <pubDate>Wed, 27 Feb 2019 18:12:46 GMT</pubDate>
            <atom:updated>2019-03-11T10:06:14.076Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/954/1*xASZ-PN3MqO6ELS3w_fr8w.png" /></figure><p>If you ever wrote an HTTP client in Golang, you might have come across the Transport struct. If you don’t, its because your client was standard, and didn’t need any special handling.</p><p>After writing a client that had to use special SSL certificates. It looks something like this:</p><pre>client := &amp;http.Client{<br>   <em>Transport</em>: &amp;http.Transport{<br>      <em>TLSClientConfig</em>: &amp;tls.Config{<br>         <em>InsecureSkipVerify</em>: <strong>true</strong>,<br>         <em>Certificates</em>:       []tls.Certificate{},<br>      },<br>   },<br>   <em>Timeout</em>: 10 * time.<em>Second</em>,<br>}</pre><p>This code is running every time the service is receiving an HTTP request. It then sends a request to a different server and waits for a response. When this code was introduced to the service, the memory usage of that service started to rise quickly.</p><p>So what’s the problem here?</p><p>To understand the problem we need to take a look at the Transport struct:</p><pre><strong>type </strong>Transport <strong>struct </strong>{<br>..<br>   idleConn   <strong>map</strong>[connectMethodKey][]*persistConn <br>   idleConnCh <strong>map</strong>[connectMethodKey]<strong>chan </strong>*persistConn<br>..<br>}</pre><p>We can see there is a field that stores idle connections, and it maps to a channel. Looking back at my code, and the problem begins to show — there is a map that holds channels open FOR EACH REQUEST. This means that all of the requests that are arriving have a trace in the memory.</p><p>This is the time to check to docs:</p><p>“By default, Transport caches connections for future re-use. This may leave many open connections when accessing many hosts. This behavior can be managed using Transport’s CloseIdleConnections method and the MaxIdleConnsPerHost and DisableKeepAlives fields.”</p><p>Huh.</p><p>So how should you use this object? Well, turns out Transport is a thread-safe object:</p><p>“Transports should be reused instead of created as needed. Transports are safe for concurrent use by multiple goroutines.”</p><p>A fix for that can simply be to transport the transport (ha) to a global var and simply use the same one. You can also use a singleton pattern if it makes you feel better. This fixed the memory issue right away.</p><p>But what else can you do with Transport? Well, a lot. Transport is the engine that drives the HTTP client you use. All clients have a Transport object (a default one is provided if no other object is introduced). It implements a RoundTripper interface:</p><pre>type RoundTripper interface{<br>   RoundTrip(*Request) (*Response, error)<br>}</pre><p>That does all the heavy lifting of the connection. It is being called every time the client wants to send a request. So let&#39;s say you have a service that uses an external API to calculate the distance between 2 points. You would obviously want to save as much time as you can, you can simply use a costume RoundTripper that caches the responses from the external API and if the request has a response, it doesn’t do a round trip and simply returns the response it has.</p><p>Transport also allows you to define other protocols to use, like FTP, or any other protocol you want to implement, since the proto schema is being validated in the Transport object.</p><p>To conclude, the Transport object (or the RoundTripper interface, actually) is the engine of the HTTP client we use, and even though it is not being used explicitly, it can be important to know how it works and how to use it.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1b6f9f66abf5" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Always manage your goroutines]]></title>
            <link>https://medium.com/@yinoneliraz/always-manage-your-goroutines-7567fa5ec3da?source=rss-eb8cfc6db017------2</link>
            <guid isPermaLink="false">https://medium.com/p/7567fa5ec3da</guid>
            <category><![CDATA[goroutines]]></category>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[memory-management]]></category>
            <category><![CDATA[design-patterns]]></category>
            <dc:creator><![CDATA[Yinon Eliraz]]></dc:creator>
            <pubDate>Mon, 11 Feb 2019 20:52:33 GMT</pubDate>
            <atom:updated>2019-02-11T20:52:33.643Z</atom:updated>
            <content:encoded><![CDATA[<p>One of the great things in golang are goroutines. They are easy to use, light wieghted, and are managed by the scheduler. However, they come with a cost.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/958/1*AY-HBbzOnRiogJtfZp0-WA.png" /></figure><p>The graph above describes the memory utilization of a service written in golang, serving requests from clients. One of the endpoints triggers another request from the service to another service. To speed things up (and since we can), the service is triggering the second request asynchronisly. It looks something like this:</p><pre><strong>func </strong>Invoke(ctx context.Context, job *api.Job) (*pb.Empty, error) {<br>   <strong>go func</strong>() {<br>      <strong>if </strong>err := ma.invoke(job); err != <strong>nil </strong>{<br>         logger.Errorf(ctx, &quot;failed to invoke&quot;)<br>      }<br>   }()<br>   <strong>return </strong>&amp;pb.Empty{}, <strong>nil<br></strong>}</pre><p>This was a quick fix, and made us overlook the consequences. When this service was loaded with requests, the number of goroutines went out of hand and the service crashed.</p><p>Once we realized where the problem was, the solution was very clear: we are not managing the goroutines and we create them on the fly. To solve this issue we simply sent the job, with the function that handles it to a channel. On the other side of the channel, a worker then unloads the job and applies the function to the job:</p><pre><strong>func </strong>Invoke(ctx context.Context, job *api.Job) (*pb.Empty, error{<br>   ma.workerChannel &lt;- &amp;worker.Work{<br>      <em>Task</em>:           job,<br>      <em>WorkerFunction</em>: ma.invoke,<br>   }<br>   <strong>return </strong>&amp;protobuf.Empty{}, <strong>nil<br></strong>}</pre><p>Worker:</p><pre><strong>func </strong>(worker *Worker) Start(){<br>   <strong>go func</strong>(){<br>      <strong>for </strong>{<br>         <strong>select</strong>{<br>         <strong>case </strong>job :=&lt;- worker.tasks:{<br>            <strong>if </strong>job != <strong>nil</strong>{<br>               err := job.WorkerFunction(job.<em>Task</em>)<br>               <strong>if </strong>err != <strong>nil </strong>{<br>                  logger.Errorf(&quot;worker function failed&quot;)<br>               }<br>            }<br>         }<br>         <strong>case </strong>&lt;- worker.stop:<br>            <strong>break<br>         </strong>}<br>      }<br>   }()<br>}</pre><p>Creating 100 workers solved the memory issue and sped up the response time under loads. This design is simple and easy to implement — use it!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7567fa5ec3da" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>