So if I'm receiving stuff from a channel, I can use select to timeout:
select {
case y := <-ch:
fmt.Println(y)
case <-time.After(200 * time.Millisecond):
fmt.Println("timed out")
}
What's the best way to time out a channel send? I'd like to do something like:
select {
case ch <- y:
fmt.Println("sent y to ch")
case <-time.After(200 * time.Millisecond):
fmt.Println("timed out")
}
but I know select is only for receive operations.
If it times out, I want to make sure the original send never completes.
The reason I want this is I have some code where ch is buffered and occasionally it fills up for a while and the code that sends stuff to ch then starts blocking and causing problems. Instead of blocking for a long time, I'd like to do something else like save the data to a file until the channel isn't full. Perhaps just checking the length of the channel is best and going into failsafe mode when len(ch) / cap(ch) > 0.95? But a timeout on the send would also work...
Thanks!