FAQ
Hello,

My goal is to have a long polling handler that waits on a sync.Cond.
I also want to stop waiting if the client stops the connection, to avoid
having ghost goroutines hanging around.

This simple idea quickly became hell.
First, I cannot put a cond.Wait() in a select case (only channel operations
are allowed). As a result, having the Wait() timeout in case of a receive
on the notify channel becomes non-trivial; what I did was spawn a new
goroutine to listen on the channel, and trigger a cond.Broadcast() in case
something happened.
Secondly, the notification channel is not closed when the request
completes. This means that the goroutine that was listening to this channel
was now hanging if the user didn't stop the connection. I solved *this* problem
with another timeout channel, feeded when the request completes, and read
as an alternative case alongside the close notification channel...

So, even though this wouldn't make my function as pretty as I'd like, I
think closing the channel returned by http.CloseNotifier could be a good
idea. The channel already returns a boolean (true in case of a closed
connection), so having it return false if the request completes
successfully would still allow to differentiate these two situations.

I'm open to any comment or idea.

--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Jan Mercl at Oct 13, 2014 at 2:42 pm

    On Sat, Oct 11, 2014 at 11:26 PM, Alexandre Bury wrote:
    First, I cannot put a cond.Wait() in a select case (only channel operations
    are allowed).
    Just a schema:

             cw := make(chan struct{})
             go func() { cond.Wait(); cw <- struct{}{} }()

             select {
             ...
             case <-cw:
             ...
             }

    -j

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Alexandre Bury at Oct 13, 2014 at 2:55 pm
    This still leaves a hanging goroutine waiting for the condition. It does
    limit the number of objects involved to just the condition and the channel,
    but if the condition is never used again, it will keep it in memory
    indefinitely.
    But in a situation where the condition _is_ eventually triggered, it does
    make the code much shorter (I'll just make the channel buffered) - thanks !
    On Monday, October 13, 2014 4:43:28 PM UTC+2, Jan Mercl wrote:

    On Sat, Oct 11, 2014 at 11:26 PM, Alexandre Bury
    <alexand...@gmail.com <javascript:>> wrote:
    First, I cannot put a cond.Wait() in a select case (only channel
    operations
    are allowed).
    Just a schema:

    cw := make(chan struct{})
    go func() { cond.Wait(); cw <- struct{}{} }()

    select {
    ...
    case <-cw:
    ...
    }

    -j
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedOct 13, '14 at 2:32p
activeOct 13, '14 at 2:55p
posts3
users2
websitegolang.org

2 users in discussion

Alexandre Bury: 2 posts Jan Mercl: 1 post

People

Translate

site design / logo © 2022 Grokbase