Re: [go-nuts] Clean shutdown when blocked on I/O

2020-07-13 Thread Brian Candler
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 cance

Re: [go-nuts] Clean shutdown when blocked on I/O

2020-07-13 Thread Olivier Mengué
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? >>>

Re: [go-nuts] Clean shutdown when blocked on I/O

2020-07-12 Thread Olivier Mengué
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 > R

Re: [go-nuts] Clean shutdown when blocked on I/O

2020-07-08 Thread Brian Candler
Cool, thanks. One thing I also need to unblock is the Listen loop. Unfortunately, net.Listener doesn't have a SetDeadline call, so I'll just have to Close it anyway. -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this gro

Re: [go-nuts] Clean shutdown when blocked on I/O

2020-07-08 Thread Andrei Tudor Călin
Indeed, servers are easier, and if all you're looking to do is to issue a cancellation signal which immediately closes every active connection, the code you posted is perfectly adequate. I don't expect calling SetDeadline to ever cause a panic, regardless of the state of the connection. On Wed, J

Re: [go-nuts] Clean shutdown when blocked on I/O

2020-07-08 Thread Brian Candler
On Wednesday, 8 July 2020 11:17:56 UTC+1, Andrei Tudor Călin wrote: > > I think this works for some cases, but it is potentially wasteful (and > even leaky) in terms of resource usage. > > For example, if ctx is context.Background(), it leaks a goroutine for > every connection > If this is a net

Re: [go-nuts] Clean shutdown when blocked on I/O

2020-07-08 Thread Andrei Tudor Călin
I think this works for some cases, but it is potentially wasteful (and even leaky) in terms of resource usage. For example, if ctx is context.Background(), it leaks a goroutine for every connection. It also keeps the additional goroutine around for the entire lifetime of the connection. I'd like t

Re: [go-nuts] Clean shutdown when blocked on I/O

2020-07-08 Thread Brian Candler
Thank you, that was what I was looking for. I had forgotten about deadlines, and I didn't realise that you could change a deadline even while a read was in progress. In case it's helpful to anyone, here's a proof-of-concept. It doesn't work on play.golang.org because of the network communicat

Re: [go-nuts] Clean shutdown when blocked on I/O

2020-07-06 Thread Robert Engels
I only meant that the “socket closed” error is a good (perfect?) signal that the Go routine should exit rather than using an additional state var. > On Jul 6, 2020, at 6:43 PM, Ian Lance Taylor wrote: > > On Mon, Jul 6, 2020 at 4:23 PM robert engels wrote: >> >> If you set the deadline to z

Re: [go-nuts] Clean shutdown when blocked on I/O

2020-07-06 Thread Ian Lance Taylor
On Mon, Jul 6, 2020 at 4:23 PM robert engels wrote: > > If you set the deadline to zero, don’t you need to use another flag to know > that it “should be terminated”, otherwise you can’t know if the deadline > exceeded was regular - I guess, unless you do a “get deadline” on the error > and chec

Re: [go-nuts] Clean shutdown when blocked on I/O

2020-07-06 Thread robert engels
If you set the deadline to zero, don’t you need to use another flag to know that it “should be terminated”, otherwise you can’t know if the deadline exceeded was regular - I guess, unless you do a “get deadline” on the error and check for 0, but that seems racy ? > On Jul 6, 2020, at 1:36 PM, I

Re: [go-nuts] Clean shutdown when blocked on I/O

2020-07-06 Thread Ian Lance Taylor
On Mon, Jul 6, 2020 at 10:57 AM Robert Engels wrote: > > You need to close the socket from another Go routine. Otherwise the Reader > api would need to be changed to always take a Context. Either close the socket or set the deadline to zero. Ian > On Jul 6, 2020, at 12:36 PM, Brian Candler w

Re: [go-nuts] Clean shutdown when blocked on I/O

2020-07-06 Thread Robert Engels
You need to close the socket from another Go routine. Otherwise the Reader api would need to be changed to always take a Context. > On Jul 6, 2020, at 12:36 PM, Brian Candler wrote: > >  >> On Monday, 6 July 2020 14:53:55 UTC+1, Ian Davis wrote: >> Can you write your own ContextReader that ch

Re: [go-nuts] Clean shutdown when blocked on I/O

2020-07-06 Thread Brian Candler
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 wrap

Re: [go-nuts] Clean shutdown when blocked on I/O

2020-07-06 Thread Ian Davis
Can you write your own ContextReader that checks ctx.Done in its Read method? On Mon, 6 Jul 2020, at 2:40 PM, Brian Candler wrote: > I am looking for the safe way to do a clean shutdown when blocked on I/O. > Here is a basic example: > > package main > > import ( > "encoding/json" > "fmt" > "io