-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmd.go
More file actions
108 lines (86 loc) · 2.08 KB
/
cmd.go
File metadata and controls
108 lines (86 loc) · 2.08 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package main
import (
"context"
"fmt"
"math/rand"
"syscall"
"time"
"github.com/vsdmars/actor"
"go.uber.org/zap"
)
func logActor(act actor.Actor) {
for {
select {
case <-act.Done():
fmt.Println("Sayonara, my friends~ -- logActor")
return
case v := <-act.Receive():
fmt.Printf("LOG MESSAGE: %v\n", v)
go act.Backup(string(v.(string)))
}
}
}
func pipe1Actor(act actor.Actor) {
logActor, _ := actor.Get("logger")
pipe2Actor, _ := actor.Get("pipe2")
for {
select {
case <-act.Done():
fmt.Println("Sayonara, my friends~ -- Pipe1Actor")
return
case msg := <-act.Receive():
fmt.Printf("Pipe1 received message: %v\n", msg)
go logActor.Send(fmt.Sprintf("pipe1 got message: %v", msg))
go pipe2Actor.Send(msg)
}
}
}
func pipe2Actor(act actor.Actor) {
logActor, _ := actor.Get("logger")
for {
select {
case <-act.Done():
fmt.Println("Sayonara, my friends~ -- Pipe2Actor")
return
case msg := <-act.Receive():
fmt.Printf("Pipe2 received message: %v\n", msg)
go logActor.Send(fmt.Sprintf("pipe2 got message: %v", msg))
}
}
}
func test(ctx context.Context) {
pipe1, _ := actor.NewActor(ctx, "pipe1", 3, pipe1Actor, -1)
actor.NewActor(ctx, "pipe2", 3, pipe2Actor, -1)
for {
select {
case <-ctx.Done():
return
default:
pipe1.Send(fmt.Sprintf("message %d", rand.Intn(100)))
time.Sleep(1 * time.Second)
}
}
}
func main() {
// setup log level
actor.SetLoggingLevel(zap.FatalLevel)
// create root context
ctx, cancel := context.WithCancel(context.Background())
quitSig := func() {
cancel()
}
// register signal dispositions
RegisterHandler(syscall.SIGQUIT, quitSig)
RegisterHandler(syscall.SIGTERM, quitSig)
RegisterHandler(syscall.SIGINT, quitSig)
// create logging actor 'logger' and standby
actor.NewActor(ctx, "logger", 3, logActor, 3)
// starts test
go test(ctx)
<-ctx.Done()
// Cleanup will call actor's close
actor.Cleanup()
// waits seconds for actors to safely clean up (graceful shutdown)
// http://vsdmars.blogspot.com/2019/02/golangdesign-graceful-shutdown.html
time.Sleep(3 * time.Second)
}