3 ms·
It’s trivial to implement a channel read with timeout using select and time.After(). The first N google results for “golang channel timeout” show exactly that.
by packetslave 9y ago
It’s trivial to implement a channel read with timeout using select and time.After(). The first N google results for “golang channel timeout” show exactly that.
- jnordwick 9y agoIt is still overly complex and only handles one specific timeout case. A timeout per connection? That can be a lot of extra goroutines for such a simple thing. Or if you want a 30 second timeout since the last read? Way more complex. Go's channels seem more than a little under developed.
- kasey_junk 9y agoIt’s worth noting that eliminates the range option which means you need to handle all the edge cases around closed/nil channels by hand. On the publish side in most production cases you don’t want to use time.after as it leaks. So you’ll use either a context or a timer. All told it’s a fair bit of boilerplate to get right everytime (which you can’t genericize) which is why I tend to use buffered channels that are sized such that being full is a degenerate case.