Kaveh Shahbazian
Kaveh Shahbazian

Reputation: 13523

Notify all goroutines

I am working on a TCP Server in Go. Now I want to notify all goroutines that are talking to clients to drop their connections, dump what they've got and stop.

Closing a channel is a way to notify all of them.

Question is: Is that idiomatic Go? If I am wrong; then what should I do (for notifying all of goroutines - something like ManualResetEvent in .NET)?

Note: I am a Go newbie, just learning and started with TCP Server because I have written that before in C#.

Upvotes: 5

Views: 2193

Answers (2)

fortytwo
fortytwo

Reputation: 531

As of Go 1.7, you can use the context package.

Upvotes: 2

Daniel
Daniel

Reputation: 38771

Yes, closing a channel is an idiomatic Go way of communicating between Goroutines.

You'll need to pass a channel into each goroutine as it launches and check the channel with the select call after each network event.

You'll also want to set timeouts on network events so that you don't have connections hanging around forever.

Upvotes: 6

Related Questions