-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest.go
41 lines (37 loc) · 960 Bytes
/
request.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package main
import (
"log"
"math/rand"
"time"
)
//Request job is executed by the worker and the result is sent on the result channel
type Request struct {
job func() int
result chan int
err chan error
}
//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, 25)
e := make(chan error, 5)
for {
randDuration := time.Duration(rand.Intn(300)) * time.Millisecond
time.Sleep(randDuration + 100*time.Millisecond)
select {
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{job, c, e}
}
}
}
func randomFailingJob() int {
randDuration := time.Duration(rand.Intn(4000)) * time.Millisecond
time.Sleep(randDuration)
if randDuration <= time.Millisecond*400 { //random failure
panic("3 FAST 5 ME")
}
return 1
}