-
Notifications
You must be signed in to change notification settings - Fork 1
/
spworker.go
55 lines (45 loc) · 1.21 KB
/
spworker.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//
// Simple Pirate worker.
// Connects REQ socket to tcp://*:5556
// Implements worker part of load-balancing
//
package main
import (
zmq "github.com/pebbe/zmq3"
"fmt"
"math/rand"
"time"
)
const (
WORKER_READY = "\001" // Signals worker is ready
)
func main() {
worker, _ := zmq.NewSocket(zmq.REQ)
defer worker.Close()
// Set random identity to make tracing easier
rand.Seed(time.Now().UnixNano())
identity := fmt.Sprintf("%04X-%04X", rand.Intn(0x10000), rand.Intn(0x10000))
worker.SetIdentity(identity)
worker.Connect("tcp://localhost:5556")
// Tell broker we're ready for work
fmt.Printf("I: (%s) worker ready\n", identity)
worker.Send(WORKER_READY, 0)
for cycles := 0; true; {
msg, err := worker.RecvMessage(0)
if err != nil {
break // Interrupted
}
// Simulate various problems, after a few cycles
cycles++
if cycles > 3 && rand.Intn(5) == 0 {
fmt.Printf("I: (%s) simulating a crash\n", identity)
break
} else if cycles > 3 && rand.Intn(5) == 0 {
fmt.Printf("I: (%s) simulating CPU overload\n", identity)
time.Sleep(3 * time.Second)
}
fmt.Printf("I: (%s) normal reply\n", identity)
time.Sleep(time.Second) // Do some heavy work
worker.SendMessage(msg)
}
}