FAQ
So if I'm receiving stuff from a channel, I can use select to timeout:

select {
case y := <-ch:
fmt.Println(y)
case <-time.After(200 * time.Millisecond):
fmt.Println("timed out")
}

What's the best way to time out a channel send? I'd like to do something
like:

select {
case ch <- y:
fmt.Println("sent y to ch")
case <-time.After(200 * time.Millisecond):
fmt.Println("timed out")
}

but I know select is only for receive operations.

If it times out, I want to make sure the original send never completes.

The reason I want this is I have some code where ch is buffered and
occasionally it fills up for a while and the code that sends stuff to ch
then starts blocking and causing problems. Instead of blocking for a long
time, I'd like to do something else like save the data to a file until the
channel isn't full. Perhaps just checking the length of the channel is
best and going into failsafe mode when len(ch) / cap(ch) > 0.95? But a
timeout on the send would also work...

Thanks!

--

Search Discussions

  • David Anderson at Nov 2, 2012 at 9:33 pm

    On Fri, Nov 2, 2012 at 2:32 PM, Jerry Worthey wrote:

    So if I'm receiving stuff from a channel, I can use select to timeout:

    select {
    case y := <-ch:
    fmt.Println(y)
    case <-time.After(200 * time.Millisecond):
    fmt.Println("timed out")
    }

    What's the best way to time out a channel send? I'd like to do something
    like:

    select {
    case ch <- y:
    fmt.Println("sent y to ch")
    case <-time.After(200 * time.Millisecond):
    fmt.Println("timed out")
    }

    but I know select is only for receive operations.
    Select also works with send operations. One of the channel operations in
    the select statement will proceed. That statement can be a send or a
    receive.

    - Dave

    If it times out, I want to make sure the original send never completes.

    The reason I want this is I have some code where ch is buffered and
    occasionally it fills up for a while and the code that sends stuff to ch
    then starts blocking and causing problems. Instead of blocking for a long
    time, I'd like to do something else like save the data to a file until the
    channel isn't full. Perhaps just checking the length of the channel is
    best and going into failsafe mode when len(ch) / cap(ch) > 0.95? But a
    timeout on the send would also work...

    Thanks!

    --

    --
  • Jerry Worthey at Nov 2, 2012 at 9:39 pm
    Oh, thanks. I'm an idiot...apparently I can't read (or even try code out).

    On Fri, Nov 2, 2012 at 4:33 PM, David Anderson wrote:
    On Fri, Nov 2, 2012 at 2:32 PM, Jerry Worthey wrote:

    So if I'm receiving stuff from a channel, I can use select to timeout:

    select {
    case y := <-ch:
    fmt.Println(y)
    case <-time.After(200 * time.Millisecond):
    fmt.Println("timed out")
    }

    What's the best way to time out a channel send? I'd like to do something
    like:

    select {
    case ch <- y:
    fmt.Println("sent y to ch")
    case <-time.After(200 * time.Millisecond):
    fmt.Println("timed out")
    }

    but I know select is only for receive operations.
    Select also works with send operations. One of the channel operations in
    the select statement will proceed. That statement can be a send or a
    receive.

    - Dave

    If it times out, I want to make sure the original send never completes.

    The reason I want this is I have some code where ch is buffered and
    occasionally it fills up for a while and the code that sends stuff to ch
    then starts blocking and causing problems. Instead of blocking for a long
    time, I'd like to do something else like save the data to a file until the
    channel isn't full. Perhaps just checking the length of the channel is
    best and going into failsafe mode when len(ch) / cap(ch) > 0.95? But a
    timeout on the send would also work...

    Thanks!

    --

    --
  • Bryanturley at Nov 2, 2012 at 9:58 pm
    I think this is what you are looking for
    select {
    case ch <- x:
    // sent x down ch and didn't block
    default:
    // sent nothing and would have blocked
    }

    --

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedNov 2, '12 at 9:32p
activeNov 2, '12 at 9:58p
posts4
users3
websitegolang.org

People

Translate

site design / logo © 2023 Grokbase