Hello, Does anybody know of some reference code or common patterns I can use to keep track of worker goroutines? For context, I want a user to be able to issue a "stop" command using a CLI tool which will prompt my server to gracefully terminate one of my workers.
For example, when a user issues a "start" command through a CLI tool, this will signal my server to spawn a goroutine that perpetually runs and polls an HTTP endpoint until a user initiates a "stop" command. When the user issues a "stop" command through the same CLI tool, I want the server to be able to signal the goroutine to stop. Any reference code would be much appreciated! One approach I thought of was by passing a `context.WithCancel()` and holding a reference to the cancel function in a global map (with the worker ID as keys). When the user issues a "stop" command against the worker ID, another function is executed which calls the context's cancel function. Example code below (appreciate this is horrendously breaking a lot of rules, but I want to focus only on the elements around passing the cancel function around - happy to discuss anything else that is of concern to someone though): ``` // invoked when a CLI start subcommand is issued to my main CLI tool func (w *WorkerGroup) Start(ctx context.Context, id string) { _, cancel := context.WithCancel(ctx) w.Workers.Lock() w.Workers[id] := cancel w.Workers.Unlock() go startWorker(id) } // invoked when a CLI stop subcommand is issued to my main CLI tool func (w *WorkerGroup) Stop(id string) { cancelWorker := w.Workers[id] cancelWorker() } ``` I have read that passing a context's cancel function around is a bad idea, but in an example like this, is it justified? Thanks in advance to any help! -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/c8685198-d7fe-4386-ad1f-374493924a06n%40googlegroups.com.