<?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 wisma on Medium]]></title>
        <description><![CDATA[Stories by wisma on Medium]]></description>
        <link>https://medium.com/@wismaeka?source=rss-93f84bf9a65a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*dmbNkD5D-u45r44go_cf0g.png</url>
            <title>Stories by wisma on Medium</title>
            <link>https://medium.com/@wismaeka?source=rss-93f84bf9a65a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 06:00:25 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@wismaeka/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[Pop a Party in Concurrent Way]]></title>
            <link>https://medium.com/xendit-engineering/pop-a-party-in-concurrent-way-7c846b6b09f5?source=rss-93f84bf9a65a------2</link>
            <guid isPermaLink="false">https://medium.com/p/7c846b6b09f5</guid>
            <category><![CDATA[concurrency]]></category>
            <category><![CDATA[goroutines]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[parallelism]]></category>
            <category><![CDATA[golang]]></category>
            <dc:creator><![CDATA[wisma]]></dc:creator>
            <pubDate>Wed, 07 Dec 2022 02:01:44 GMT</pubDate>
            <atom:updated>2022-12-07T02:49:35.373Z</atom:updated>
            <content:encoded><![CDATA[<p><em>In this party, I have to compare the best solution to prepare the party using waitgroup and channel.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tg8IeMWKZSIZpwuu5qq6iQ.jpeg" /></figure><p>Ah, sweet summer breeze! What a waste it’d be if I didn’t throw a pool party! I can’t wait to meet all my friends.</p><p>Many preparations to be done, such as venue booking, check, invitation, check, food, hmm, durian <em>es teler</em> would make a great dessert, but I’m not sure if my friends could take the odor 😔 hence if any of them can’t take it I’ll switch to pineapple ice 🥳.</p><p><strong>A sequential solution</strong></p><p>Without any further ado, I call them one by one and ask about durian <em>es teler </em>being the main dessert.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7a076b83f8f2b86815e98b7f574139cd/href">https://medium.com/media/7a076b83f8f2b86815e98b7f574139cd/href</a></iframe><p>I just texted my friend one by one, how long it took for calling 4 friends ? Run the full snippet <a href="https://go.dev/play/p/H-kuzDKXaoW">here</a>. Now imagine if I have 500 friends, probably I need a whole day waiting for the response which leaves me no time.</p><p><strong>Want to save more time?</strong></p><p>I am thinking of another way to save time. But how? Suddenly, a SMS from my telco notified me that I get bonus of 1000 free SMSes. Woah, that’s a jackpot! I sent all of my friends a message “<em>would you like to have durian es teler as dessert for the upcoming party</em>?”.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/25682dc0f9cc53740f02a068bbd0ea59/href">https://medium.com/media/25682dc0f9cc53740f02a068bbd0ea59/href</a></iframe><p>Ok! This is slightly better. I utilize the <em>waitgroup </em>which save more time than previous solution. Now all I need to do is just wait for all of them to reply to it. If you are curious how much time that saved please run the code <a href="https://go.dev/play/p/w63rO8aoj5w">here</a> .</p><p><strong>Could it be faster?</strong></p><p>The solution above is much better, now I just need to wait until the last of them replies, but come to think of it, do I really need to?</p><p>I don’t think it’s worth to wait all day for them to reply therefore I will eliminate the durian <em>es teler</em> for dessert when one of them say “<strong>no for durian</strong>”</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2c0fc10929004a4dc082d4bc08ec7975/href">https://medium.com/media/2c0fc10929004a4dc082d4bc08ec7975/href</a></iframe><p>Awesome, with this <em>channel</em> based solution I can save more time for preparing the party. Please reveal the surprise <a href="https://go.dev/play/p/LEZOC3PEfTq">here</a>.</p><blockquote><strong>Why</strong> is this solution faster than using <em>waitgroup</em>? Let’s talk about technical things a bit 😅 .</blockquote><p>Look at line 19, I added a condition to exit the process earlier when the desirable answer is fulfilled. <strong>But</strong> there is another problem, when the main goroutine exits, the spawned goroutines, which are still checking with my other friends, can’t return because when they eventually send the result to the channel, there’d be no consumer. This caused them to leak, which means they would hold on to resources such as memory. When this happens, the memory consumption will continuously build up, and the app eventually crashes. We don’t want it, right? So, we need to allow them to exit the goroutine once we receive the desired result.</p><p>The sample below utilizes a “<strong>stopCh</strong>”. As long as one “yes” answer is received, it’s closed. On the other hand, all spawned goroutines also poll the <strong>stopCh</strong>. This means that if any goroutine tries to send to <strong>dataCh</strong> after the main exits and gets stuck, the “<strong>&lt;-stopCh</strong>” case will be unblocked and it will exit. This is because reading from a closed channel immediately returns. More explanation in <a href="https://dave.cheney.net/2014/03/19/channel-axioms">Dave Cheney’s Channel Axioms</a>. To run the full code just click <a href="https://go.dev/play/p/CaUL9Q5i_cT">https://go.dev/play/p/CaUL9Q5i_cT</a>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/049bcfdc822da36de5985fc375989aeb/href">https://medium.com/media/049bcfdc822da36de5985fc375989aeb/href</a></iframe><p><strong>Conclusion</strong></p><p>Implementing concurrency could be a consideration for handling numerous of independent tasks. Utilizing sync.WaitGroup is usually a good choice, but when early termination is feasible, channels can be the alternative. On top of that, always ensure all goroutines can exit to avoid the goroutine leak so you can pop your party with less worry!</p><p>Wait what ?? Less worry ?? How to be not worry at all? I called my teammate Jason to help me improve this solution (the article will publish soon) so I have no worries to pop the party!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7c846b6b09f5" width="1" height="1" alt=""><hr><p><a href="https://medium.com/xendit-engineering/pop-a-party-in-concurrent-way-7c846b6b09f5">Pop a Party in Concurrent Way</a> was originally published in <a href="https://medium.com/xendit-engineering">Xendit Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>