Le dimanche 12 juillet 2020 17:20:36 UTC+2, Olivier Mengué a écrit :
>
>
>
> Le lundi 6 juillet 2020 19:36:24 UTC+2, Brian Candler a écrit :
>>
>> On Monday, 6 July 2020 14:53:55 UTC+1, Ian Davis wrote:
>>>
>>> Can you write your own ContextReader that checks ctx.Done in its Read 
>>> method?
>>>
>>>
>> Inside a ContextReader I can check ctx.Done *before* calling the wrapped 
>> Read method - but if the context isn't done at that point and I called the 
>> wrapped Read, it will be blocked at that point.
>>
>
> I have implemented such Reader/Writer in my package 
> github.com/dolmen-go/contextio. Doc: 
> https://pkg.go.dev/github.com/dolmen-go/contextio/?tab=doc
>
> Olivier. 
>

Oh, I just noticed that you had already refered to my package in your 
original question.

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()
...

Olivier

-- 
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/d8c3e420-c12f-43ef-930e-e12994d5b2e9o%40googlegroups.com.

Reply via email to