Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement poison-pill method to exit infinite loops
All cooperating goroutines regularly try to read from the shared "poison" channel. If the read succeeds, they exit by calling die(), assuming somebody else cracked open the poison pill. When any of these goroutines is done with its job, it signals other goroutines to exit by calling open_poison() on the shared channel. This approach takes advantage of the fact that reads from a closed channel always succeed. The driving goroutine (Client.Loop() in this case), is called from the "main" goroutine. And because when the "main" goroutine exits, the whole program exit (using os.Exit()) irrepective of liveness of other goroutines, we could not use the same "poison" channel to wait in the driving goroutine. Instead, we use sync.WaitGroup to wait for spawned goroutines, because we want the spawned goroutines to cleanup and exit cleanly.
- Loading branch information
Showing
1 changed file
with
78 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters