-
-
Notifications
You must be signed in to change notification settings - Fork 534
/
Copy pathconfig.go
137 lines (119 loc) · 4.67 KB
/
config.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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package cluster
import (
"log/slog"
"time"
"github.com/asynkron/protoactor-go/actor"
"github.com/asynkron/protoactor-go/remote"
)
type Config struct {
Name string
Address string
ClusterProvider ClusterProvider
IdentityLookup IdentityLookup
RemoteConfig *remote.Config
RequestTimeoutTime time.Duration
RequestsLogThrottlePeriod time.Duration
RequestLog bool
MaxNumberOfEventsInRequestLogThrottledPeriod int
ClusterContextProducer ContextProducer
MemberStrategyBuilder func(cluster *Cluster, kind string) MemberStrategy
Kinds map[string]*Kind
TimeoutTime time.Duration
GossipInterval time.Duration
GossipRequestTimeout time.Duration
GossipFanOut int
GossipMaxSend int
HeartbeatExpiration time.Duration // Gossip heartbeat timeout. If the member does not update its heartbeat within this period, it will be added to the BlockList
PubSubConfig *PubSubConfig
}
func Configure(clusterName string, clusterProvider ClusterProvider, identityLookup IdentityLookup, remoteConfig *remote.Config, options ...ConfigOption) *Config {
config := &Config{
Name: clusterName,
ClusterProvider: clusterProvider,
IdentityLookup: identityLookup,
RequestTimeoutTime: defaultActorRequestTimeout,
RequestsLogThrottlePeriod: defaultRequestsLogThrottlePeriod,
MemberStrategyBuilder: newDefaultMemberStrategy,
RemoteConfig: remoteConfig,
Kinds: make(map[string]*Kind),
ClusterContextProducer: newDefaultClusterContext,
MaxNumberOfEventsInRequestLogThrottledPeriod: defaultMaxNumberOfEvetsInRequestLogThrottledPeriod,
TimeoutTime: time.Second * 5,
GossipInterval: time.Millisecond * 300,
GossipRequestTimeout: time.Millisecond * 500,
GossipFanOut: 3,
GossipMaxSend: 50,
HeartbeatExpiration: time.Second * 20,
PubSubConfig: newPubSubConfig(),
}
for _, option := range options {
option(config)
}
return config
}
// ToClusterContextConfig converts this cluster Config Context parameters
// into a valid ClusterContextConfig value and returns a pointer to its memory
func (c *Config) ToClusterContextConfig(logger *slog.Logger) *ClusterContextConfig {
clusterContextConfig := ClusterContextConfig{
RequestsLogThrottlePeriod: c.RequestsLogThrottlePeriod,
MaxNumberOfEventsInRequestLogThrottledPeriod: c.MaxNumberOfEventsInRequestLogThrottledPeriod,
requestLogThrottle: actor.NewThrottleWithLogger(logger,
int32(defaultMaxNumberOfEvetsInRequestLogThrottledPeriod),
defaultRequestsLogThrottlePeriod,
func(logger *slog.Logger, i int32) {
logger.Info("Throttled %d Request logs", slog.Int("count", int(i)))
},
),
}
return &clusterContextConfig
}
func WithClusterIdentity(props *actor.Props, ci *ClusterIdentity) *actor.Props {
// inject the cluster identity into the actor context
p := props.Clone(
actor.WithOnInit(func(ctx actor.Context) {
SetClusterIdentity(ctx, ci)
}))
return p
}
func withClusterReceiveMiddleware() actor.PropsOption {
return actor.WithReceiverMiddleware(func(next actor.ReceiverFunc) actor.ReceiverFunc {
return func(c actor.ReceiverContext, envelope *actor.MessageEnvelope) {
// the above code as a type switch
switch envelope.Message.(type) {
case *actor.Started:
handleStarted(c, next, envelope)
case *actor.Stopped:
handleStopped(c, next, envelope)
default:
next(c, envelope)
}
return
}
})
}
func handleStopped(c actor.ReceiverContext, next actor.ReceiverFunc, envelope *actor.MessageEnvelope) {
/*
clusterKind.Dec();
*/
cl := GetCluster(c.ActorSystem())
identity := GetClusterIdentity(c)
if identity != nil {
cl.ActorSystem.EventStream.Publish(&ActivationTerminating{
Pid: c.Self(),
ClusterIdentity: identity,
})
cl.PidCache.RemoveByValue(identity.Identity, identity.Kind, c.Self())
}
next(c, envelope)
}
func handleStarted(c actor.ReceiverContext, next actor.ReceiverFunc, envelope *actor.MessageEnvelope) {
next(c, envelope)
cl := GetCluster(c.ActorSystem())
identity := GetClusterIdentity(c)
grainInit := &ClusterInit{
Identity: identity,
Cluster: cl,
}
ge := actor.WrapEnvelope(grainInit)
next(c, ge)
}