dsha256/plfa

Fix `RunAndListenClient`

dsha256 opened this issue · 0 comments

Problem

RunAndListenClient: multiple goroutines across multiple runs of RunAndListenClient are reading from the “done” channel, and which one will receive something from that channel – completely random. As a result, in case of a lost connection, some random goroutine will exit and its graceful shutdown functionality will be lost.


Solution

To avoid such problems, the code should be simple. It’s hard to figure out how this method works because of multiple goroutines
and channels, thus it’s more error-prone. The easier the code, the harder a bug to find a place to hide. This functionality across all the methods and their goroutines can be done with a single context passed from the main. All the goroutines will read from ctx.Done() in select to shutdown gracefully.