mirror of
https://github.com/golang/go
synced 2024-11-21 21:34:40 -07:00
doc: add round-robin flag to io2010 balance example.
R=r CC=golang-dev https://golang.org/cl/2050042
This commit is contained in:
parent
7a2daa7d3a
commit
724b66fb15
@ -6,6 +6,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"container/heap"
|
"container/heap"
|
||||||
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"rand"
|
"rand"
|
||||||
"time"
|
"time"
|
||||||
@ -14,6 +15,8 @@ import (
|
|||||||
const nRequester = 100
|
const nRequester = 100
|
||||||
const nWorker = 10
|
const nWorker = 10
|
||||||
|
|
||||||
|
var roundRobin = flag.Bool("r", false, "use round-robin scheduling")
|
||||||
|
|
||||||
// Simulation of some work: just sleep for a while and report how long.
|
// Simulation of some work: just sleep for a while and report how long.
|
||||||
func op() int {
|
func op() int {
|
||||||
n := rand.Int63n(1e9)
|
n := rand.Int63n(1e9)
|
||||||
@ -125,7 +128,7 @@ func (b *Balancer) print() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (b *Balancer) dispatch(req Request) {
|
func (b *Balancer) dispatch(req Request) {
|
||||||
if false {
|
if *roundRobin {
|
||||||
w := b.pool[b.i]
|
w := b.pool[b.i]
|
||||||
w.requests <- req
|
w.requests <- req
|
||||||
w.pending++
|
w.pending++
|
||||||
@ -144,7 +147,7 @@ func (b *Balancer) dispatch(req Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (b *Balancer) completed(w *Worker) {
|
func (b *Balancer) completed(w *Worker) {
|
||||||
if false {
|
if *roundRobin {
|
||||||
w.pending--
|
w.pending--
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -156,6 +159,7 @@ func (b *Balancer) completed(w *Worker) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
flag.Parse()
|
||||||
work := make(chan Request)
|
work := make(chan Request)
|
||||||
for i := 0; i < nRequester; i++ {
|
for i := 0; i < nRequester; i++ {
|
||||||
go requester(work)
|
go requester(work)
|
||||||
|
Loading…
Reference in New Issue
Block a user