<?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[debugger22 - Medium]]></title>
        <description><![CDATA[Shortlog - Medium]]></description>
        <link>https://medium.com/debugger22?source=rss----3542c29f8930---4</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>debugger22 - Medium</title>
            <link>https://medium.com/debugger22?source=rss----3542c29f8930---4</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 03 Dec 2026 13:47:06 GMT</lastBuildDate>
        <atom:link href="https://medium.com/feed/debugger22" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How to exit a child goroutine?]]></title>
            <link>https://medium.com/debugger22/how-to-exit-a-child-goroutine-65fcb917e994?source=rss----3542c29f8930---4</link>
            <guid isPermaLink="false">https://medium.com/p/65fcb917e994</guid>
            <category><![CDATA[chat]]></category>
            <category><![CDATA[goroutines]]></category>
            <category><![CDATA[go]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[golang]]></category>
            <dc:creator><![CDATA[Sudhanshu Mishra]]></dc:creator>
            <pubDate>Sat, 12 Feb 2022 12:43:33 GMT</pubDate>
            <atom:updated>2022-02-12T12:45:04.115Z</atom:updated>
            <content:encoded><![CDATA[<p>Lately, I’ve been working on a hobby project which is a distributed real-time chat app. The objective of this project was to learn the Go programming language.</p><p>Having worked with Python for over the past 6 years, I found Go extremely fascinating! Go has this amazing cheap and lightweight thread called <em>goroutine </em>that helps create concurrent applications so much easier<em>. </em>It is similar to how everything runs in a process in Erlang or Elixir.</p><p>While writing the chat app, I came across a problem of cleaning up the resources when a user disconnects from the server. A general rule of thumb is you should figure out a way how your goroutine is going to exit. If it is not handled correctly, your application’s memory and CPU usage will keep on increasing and eventually end up with panic.</p><p>The following function creates a goroutine and listens for any message on a Redis channel</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a270c0c131d51c6b83a2359a69b623e7/href">https://medium.com/media/a270c0c131d51c6b83a2359a69b623e7/href</a></iframe><p>This will work as expected. It gets any message on channel <em>r </em>and sends it to another channel <em>w </em>which I’m using to send the message to a connected user. This goroutine will also exit as expected when the connection to Redis is lost.</p><p>But what happens when the user gets disconnected? There should be a way to tell this goroutine that we don’t need it now.</p><p>This is where the <em>context </em>package comes into the picture. The same goroutine can be rewritten as follows</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0d758b49389b047735be83197a8dd34d/href">https://medium.com/media/0d758b49389b047735be83197a8dd34d/href</a></iframe><p>This is a very clean and correct way to handle the cleanup of a goroutine. I had to waste a lot of time to find a solution like this. I hope this article helps anyone looking for something like this.</p><p>I hope to write more on my learnings while making this chat application.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=65fcb917e994" width="1" height="1" alt=""><hr><p><a href="https://medium.com/debugger22/how-to-exit-a-child-goroutine-65fcb917e994">How to exit a child goroutine?</a> was originally published in <a href="https://medium.com/debugger22">debugger22</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>