On Monday, 13 July 2020 13:10:41 UTC+1, Olivier Mengué wrote: > > Calling SetDeadline is in fact the answer, but the problem becomes "when > to call d.SetDeadline(time.Now()) ?". The answer to that question is "just > when you cancel the context". > So a simpler answer is to just enrich the cancel function returned by > context.WithCancel to handle > > func handleConnection(ctx context.Context, conn net.Conn) { > ctx, cancelCtx := context.WithCancel(ctx) > cancel := func() { > cancel() > conn.SetDeadline(time.Now()) > } > defer cancel() > defer conn.Close() >
Presumably you mean cancelCtx() not cancel() inside the inner func? AFAICS, your code will only set the deadline if cancel() is explicitly called, or when the outer function terminates (due to the "defer cancel()") What I want is when either the inner or outer context is cancelled, the deadline is set, so as to break a read() loop. But as far as I can see, the only way to do this is by selecting on <-ctx.Done() in a goroutine. -- 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/60eabc21-5a6c-4c4d-9548-29ddb3b6963bo%40googlegroups.com.