Have you considered using a sync.WaitGroup?
John
John Souvestre - New Orleans LA
--
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...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Hi Evan.
I looked at the code you posted. I’m having a problem seeing exactly what you are trying to do. Also, it appears to be using multiple synchronization techniques: Mutex, WaitGroup, and 2 blocking channels.
Am I correct in guessing that you want to know how long it took to get all of the tasks running? Then you want to wait till all of the tasks are complete? If so, I’m thinking that using 2 WaitGroups would do the job – one to show when all the tasks are running and one to show when all the tasks are done. No mutex and no blocking channels.
Hi again.
Ø There's no way to test that a WaitGroup is done without waiting for it, and even if there was it would be racy because between the Close() and WaitGroup wait call tasks could complete.
If you don’t mind being blocked, then Wait is just what you want. Since it’s occurring in the same function (main) that the Add is taking place earlier, there is no race.
John
John Souvestre - New Orleans LA
From: golan...@googlegroups.com [mailto:golan...@googlegroups.com] On Behalf Of Evan Digby
Sent: 2016 September 13, Tue 14:19
To: golang-nuts
Ø The RW mutex is used by the implementation to guarantee that all handlers are complete before we return from "Close", which is what I'm attempting to test.
I’m not sure that the mutex is going to work like you want. There’s no guarantee that the h.RLock() in the goroutine will execute in a timely fashion. I think that you’d have to call it before the goroutine.
Hi Egon,This is essentially the strategy I'm taking; however, I am hoping to avoid the "Sleep and Pray" method. Reliable in practice, but not guaranteed. Also in a CI of thousands of tests, adding time arbitrarily can extend out the time it takes to test quite a bit.That said, if a sleep is the only way, a sleep is the only way. I hope it isn't!
h.Handle(...) <-- gets past the closed channel check, calls go ..., butthe goroutine doesn't execute yet.
h.Close() <-- closes the close channel, Locks and Unlocks,returns.
...now the goroutine executes and acquires the read lock.Hi Evan.
I still don’t quite understand exactly what you are shooting for. I tried to reimplement what you posted originally. Check out https://play.golang.org/p/koUJYCKFpa. Does this come close functionally?
You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/jh-nvt9ukBg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.
OK. Give me a minute to add that. I just wanted to make sure I was headed in the right direction. J
Note: In looking at your original code I didn’t see any way that the error could happen, so I ignored that case. Given this, there was no need for the h.closed channel.
Back in a few. J
John
John Souvestre - New Orleans LA
Hi John/Egon/Augusto,I should point out is that all we need to guarantee (barring abnormal termination of course) is that once a task starts processing, it finishes. Partially processed messages are bad, but http requests that don't result in a message being processed at all are okay.We don't need to guarantee that the result of every Accept in the HTTP server results in a processed message. We handle this on the OPS side by ensuring we stop sending requests to that instance before terminating the process. We just want to make sure, at that point, that the messages which did make it to the handler are flushed.So the case where:h.Handle(...) <-- gets past the closed channel check, calls go ..., butthe goroutine doesn't execute yet.
h.Close() <-- closes the close channel, Locks and Unlocks,returns.
...now the goroutine executes and acquires the read lock.We actually don't care if "Handle" completes in this example. We only care if that our task handler starts processing a message that it completes the processing.
--
You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/jh-nvt9ukBg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.
OK. Check this one out. https://play.golang.org/p/kO_96oykG1
John
John Souvestre - New Orleans LA
From: Evan Digby [mailto:evan...@gmail.com]
Sent: 2016 September 13, Tue 16:06
To: John Souvestre; golan...@googlegroups.com
Subject: Re: [go-nuts] Having difficulty testing this "cleanly"
Hi John,
Thank you!
Hi Egon,My requirements are more simple than a graceful http shutdown. I simply require that everything that enters the RLock completes to RUnlock. Accepted requests, or even calls to servehttp can die without issue as long as they haven't entered the processing in the RLock.
Or, if you have something else you’d rather be doing while waiting…
John
John Souvestre - New Orleans LA
From: John Souvestre [mailto:Jo...@Souvestre.com]
Sent: 2016 September 13, Tue 16:21
To: 'golan...@googlegroups.com'
Subject: RE: [go-nuts] Having difficulty testing this "cleanly"
OK. Check this one out. https://play.golang.org/p/kO_96oykG1
John
John Souvestre - New Orleans LA
Sent: 2016 September 13, Tue 16:06
To: John Souvestre; golan...@googlegroups.com
Subject: Re: [go-nuts] Having difficulty testing this "cleanly"
Hi John,
Thank you!
Lock locks rw for writing. If the lock is already locked for reading or writing, Lock blocks until the lock is available. To ensure that the lock eventually becomes available, a blocked Lock call excludes new readers from acquiring the lock.
Hi Egon,
Thanks for that. It seems to implement the same requirements as implemented in my example, although I prefer my implementation as it doesn't require a sleep/loop.