Yes you're right, the processing order is pseudo random. But my code also handle another edge case: If `srv.ListenAndServe()` returns an error BEFORE we reach the code `case err := <-errCh:`, with `default` the error is ignored.
My code ensure that: - there is no leaking goroutine - returned error is not ignored Le jeudi 6 avril 2017 11:04:29 UTC+2, Kemal Hadimli a écrit : > > Isn't the select processing order random? IIRC the only guarantee is > "default" case is handled as a low priority. > > So, something like this maybe? > > select { > case errCh <- err: > default: > select { > case <-ctx.Done(): > } > } > > Again, take this with a grain of salt. I didn't check the spec or code, > just off the top of my head. > > > Best, > > On Thursday, April 6, 2017 at 11:29:58 AM UTC+3, Pierre Durand wrote: >> >> I did it for a good reason: >> If the context is canceled `srv.Shutdown` is called. >> Then, `<-errCh` is not called anymore. >> This code ensures that there is not leaking goroutine. >> >> Le mercredi 5 avril 2017 07:13:19 UTC+2, Johnny Luo a écrit : >>> >>> func listenAndServe(ctx context.Context, srv *http.Server, errCh chan<- >>> error) { >>> err := srv.ListenAndServe() >>> select { >>> case errCh <- err: >>> case <-ctx.Done(): >>> } >>> } >>> ListenAndServe is blocking function, so the select will not happen until >>> ListenAndServe return. and errCh become no use >>> >>> >>> On Wednesday, April 5, 2017 at 4:02:16 AM UTC+10, Pierre Durand wrote: >>>> >>>> Hello >>>> >>>> I wrote a small helper to stop an HTTP Server when a Context is >>>> canceled. >>>> https://play.golang.org/p/Gl8APynVdh >>>> >>>> What do you think ? >>>> Is it OK to use context cancellation for stopping long running >>>> functions ? >>>> >>> -- 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. For more options, visit https://groups.google.com/d/optout.