<?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 Dmitriy Prokudin on Medium]]></title>
        <description><![CDATA[Stories by Dmitriy Prokudin on Medium]]></description>
        <link>https://medium.com/@skvoch?source=rss-55dab6bcf44e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*KNdLqxsXnuHG4hBCJJR4sw.jpeg</url>
            <title>Stories by Dmitriy Prokudin on Medium</title>
            <link>https://medium.com/@skvoch?source=rss-55dab6bcf44e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 02:21:59 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@skvoch/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[Golang task scheduler with etcd sync]]></title>
            <link>https://skvoch.medium.com/golang-task-scheduler-with-etcd-sync-2e23d3c6da8f?source=rss-55dab6bcf44e------2</link>
            <guid isPermaLink="false">https://medium.com/p/2e23d3c6da8f</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[open-source]]></category>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[go]]></category>
            <dc:creator><![CDATA[Dmitriy Prokudin]]></dc:creator>
            <pubDate>Sun, 31 Jan 2021 11:17:03 GMT</pubDate>
            <atom:updated>2023-05-27T00:40:23.772Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*k2lLBKMbddHssWJ_iwdiAg.png" /></figure><p>Have you ever faced a situation when you need to do something cyclically, for example after one minute delay? The obvious solution is using <em>time. Ticker</em>, the possible implementation looks something like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/035c119a5f3c86f21d031faca83eb4ab/href">https://medium.com/media/035c119a5f3c86f21d031faca83eb4ab/href</a></iframe><blockquote>Fine, we found a good solution for your issue, isn’t it? what could go wrong?</blockquote><p>Let’s find possible issues, which you might face. The most important is the state of the application, Considering you decided to scale our service, multiple replicas will call the handler function at the same time.</p><p>This is a common situation for any service that has its own state, what should we do to fix this problem? There are a couple of solutions:</p><h4>Using etcd for implementing a leader election algorithm</h4><p>Why not? Only one instance of our service will work at the same time, the rest instances waiting for their turn. But I’m sure that our service should deal with something else besides periodic tasks. We must find a more convenient solution.</p><h4>Using etcd for locking handler function</h4><p>Let’s see what happens in this solution:<br>1. The ticker will send signals to chan<br>2. Our select will be triggered and after that locking <em>etcd</em> mutex and calling the handler function<br>3. Finally, calling unlock function for etcd mutex</p><p>For simplification, I used a ready module for <em>etcd</em> locks. The implementation of this algorithm looks like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/85054f751600321dfc1da964bfb60544/href">https://medium.com/media/85054f751600321dfc1da964bfb60544/href</a></iframe><p>Now it looks much better, and the last thing that we should implement — storing the last action time to key-value storage. On each iteration check the difference between the current time and the last action time. This is a bit of protection for premature calling the handler function.</p><h4>Conclusion</h4><p>Our final solution will look like this:</p><ol><li>Checking the time since the last action</li><li>If the difference between <em>time.Now()</em> and last action time is less than the interval — skipping</li><li>Locking and calling a handler func</li><li>Updating the last action time, and unlocking</li></ol><p>By the way, I implemented a <a href="https://github.com/skvoch/reter">library</a> that uses the same algorithm</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/30ed6a3a2487a70e2ba6c16182911a13/href">https://medium.com/media/30ed6a3a2487a70e2ba6c16182911a13/href</a></iframe><p>Links:</p><ol><li><a href="https://github.com/skvoch/reter">https://github.com/skvoch/reter</a></li><li><a href="https://github.com/Scalingo/go-etcd-lock/">https://github.com/Scalingo/go-etcd-lock/</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2e23d3c6da8f" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>