Created
April 7, 2012 17:28
-
-
Save alphazero/2330644 to your computer and use it in GitHub Desktop.
auto-throttling an async fan-out
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"log" | |
"runtime" | |
"time" | |
"math/rand" | |
) | |
func init() { | |
runtime.GOMAXPROCS(runtime.NumCPU()) | |
} | |
func main() { | |
go run() | |
var latch chan int | |
<-latch | |
} | |
func run() { | |
var n int | |
var e int | |
var t int | |
var t0 time.Time = time.Now() | |
// tuning params | |
var wait = time.Duration(1000 * 10) | |
var bufflen = 4096 * 4 | |
errchan := make(chan interface{}, 256) | |
done := make(chan interface{}, bufflen) | |
for { | |
go perform(done, errchan) | |
select { | |
case <-time.After(wait): | |
n++ | |
t++ | |
case <-errchan: | |
n++ | |
e++ | |
case <-done: | |
n++ | |
} | |
if n >= 300000 { | |
emit(t0, n, e, t) | |
n, e, t , t0 = 0, 0, 0, time.Now() | |
} | |
} | |
} | |
func perform(done chan interface{}, sch chan interface{}) { | |
defer func() { | |
if p := recover(); p != nil { | |
sch <- p | |
} | |
}() | |
// simulate non-blocking task's latency without sleep, etc. | |
// so as to not side-effect scheduling | |
// using rands to prevent in-lining | |
a := rand.Int31n(1000000) | |
b := rand.Int31n(1000) | |
// simulate a low probability fault & panic | |
if b == 999 { | |
panic("Oh my God!") | |
} | |
done <- a % b | |
} | |
func emit(t0 time.Time, n int, e int, t int) { | |
delta := time.Now().Sub(t0) / 1000000 | |
log.Printf("%04d requests in %d msec (%d panicked - %d waits)\n", n, delta, e, t) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment