Skip to content

Commit

Permalink
make requester take a job
Browse files Browse the repository at this point in the history
  • Loading branch information
samjjc committed Jan 10, 2018
1 parent 69ed0df commit 14d607d
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
Binary file removed balance
Binary file not shown.
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ func main() {
pool := NewPool(*poolSize, done)
balancer := Balancer{*pool, done, true}
go balancer.balance(work)
InfiniteRequester(work)
InfiniteRequester(work, randomFailingJob)
}
12 changes: 6 additions & 6 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,25 @@ type Request struct {
err chan error
}

//InfiniteRequester sends requests to the work channel every 0.1 to 0.4 seconds
func InfiniteRequester(work chan<- Request) {
//InfiniteRequester sends custom requests to the work channel every 0.1 to 0.4 seconds
func InfiniteRequester(work chan<- Request, job func() int) {
c := make(chan int, 5)
e := make(chan error, 5)
for {
randDuration := time.Duration(rand.Intn(300)) * time.Millisecond
time.Sleep(randDuration + 100*time.Millisecond)
select {
case work <- Request{randomJob, c, e}:
case work <- Request{job, c, e}:
case <-c:
// here would be where you'd process the results
case err := <-e:
log.Printf("%v\n", err)
work <- Request{randomJob, c, e}
work <- Request{job, c, e}
}
}
}

func randomJob() int {
func randomFailingJob() int {
randDuration := time.Duration(rand.Intn(4000)) * time.Millisecond
time.Sleep(randDuration)
if randDuration <= time.Millisecond*400 {
Expand All @@ -47,6 +47,6 @@ func constantJob() int {

func fastJob() int {
y := rand.Intn(4000) / 5
x := 2 + 5/4 + y
x := 2 * y
return x
}

0 comments on commit 14d607d

Please sign in to comment.