FAQ
This is probably obvious to many people, but it's easy to overlook its
cost. Imagine this example at high QPS:

select {
   case m := <-c:
     handle(m)
   case <-time.After(5 * time.Minute):
     fmt.Println("timed out")
}

Every iteration creates a channel that's going to stay alive for 5 minutes.
At 1KQPS, you're going to end up with a steady state of 300K live objects
and timers.
In such cases, I think it's better to go with something like this:

tm := time.NewTimer(5 * time.Minute)
defer tm.Stop()
select {
   case m := <-c:
     handle(m)
   case <-tm.C:
     fmt.Println("timed out")
}

I stress-tested the above code snippet, and I didn't say any memory leaks
or degradation.

Any better ideas?

--
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

  • Caleb Spare at Sep 24, 2014 at 9:20 pm
    I do this as well. Also, if you're doing this inside a loop, you can
    keep reusing the same timer by calling Reset.

    -Caleb
    On Wed, Sep 24, 2014 at 2:15 PM, Sugu Sougoumarane wrote:
    This is probably obvious to many people, but it's easy to overlook its cost.
    Imagine this example at high QPS:

    select {
    case m := <-c:
    handle(m)
    case <-time.After(5 * time.Minute):
    fmt.Println("timed out")
    }

    Every iteration creates a channel that's going to stay alive for 5 minutes.
    At 1KQPS, you're going to end up with a steady state of 300K live objects
    and timers.
    In such cases, I think it's better to go with something like this:

    tm := time.NewTimer(5 * time.Minute)
    defer tm.Stop()
    select {
    case m := <-c:
    handle(m)
    case <-tm.C:
    fmt.Println("timed out")
    }

    I stress-tested the above code snippet, and I didn't say any memory leaks or
    degradation.

    Any better ideas?

    --
    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.
    --
    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
postedSep 24, '14 at 9:15p
activeSep 24, '14 at 9:20p
posts2
users2
websitegolang.org

2 users in discussion

Caleb Spare: 1 post Sugu Sougoumarane: 1 post

People

Translate

site design / logo © 2021 Grokbase