-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
65 lines (55 loc) · 1.59 KB
/
main.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
56
57
58
59
60
61
62
63
64
65
package main
import (
"github.com/asynkron/protoactor-go/actor"
"github.com/asynkron/protoactor-go/cluster"
"github.com/asynkron/protoactor-go/cluster/clusterproviders/etcd"
"github.com/asynkron/protoactor-go/cluster/identitylookup/disthash"
"github.com/asynkron/protoactor-go/remote"
clientv3 "go.etcd.io/etcd/client/v3"
"log"
"os"
"os/signal"
"protoactor-go-sender-example/cluster/messages"
"time"
)
type ponger struct {
}
func (*ponger) Receive(ctx actor.Context) {
switch msg := ctx.Message().(type) {
case *messages.PingMessage:
pong := &messages.PongMessage{Cnt: msg.Cnt}
log.Print("Received ping message")
ctx.Respond(pong)
default:
}
}
func main() {
// Set up actor system
system := actor.NewActorSystem()
// Prepare a remote env that listens to 8080
remoteConfig := remote.Configure("127.0.0.1", 8080)
// Configure a cluster on top of the above remote env
clusterKind := cluster.NewKind(
"Ponger",
actor.PropsFromProducer(func() actor.Actor {
return &ponger{}
}))
// Configure cluster on top of the above remote env
cp, err := etcd.NewWithConfig("/protoactor", clientv3.Config{
Endpoints: []string{"127.0.0.1:2379"},
DialTimeout: time.Second * 5,
})
if err != nil {
panic(err)
}
lookup := disthash.New()
clusterConfig := cluster.Configure("cluster-example", cp, lookup, remoteConfig, cluster.WithKinds(clusterKind))
c := cluster.New(system, clusterConfig)
// Manage the cluster node's lifecycle
c.StartMember()
defer c.Shutdown(false)
// Run till a signal comes
finish := make(chan os.Signal, 1)
signal.Notify(finish, os.Interrupt, os.Kill)
<-finish
}