-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
option.go
197 lines (175 loc) · 5.34 KB
/
option.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
* MIT License
*
* Copyright (c) 2022-2024 Arsene Tochemey Gandote
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package actors
import (
"time"
"github.com/tochemey/goakt/v2/discovery"
"github.com/tochemey/goakt/v2/hash"
"github.com/tochemey/goakt/v2/log"
)
// Option is the interface that applies a configuration option.
type Option interface {
// Apply sets the Option value of a config.
Apply(sys *actorSystem)
}
// enforce compilation error
var _ Option = OptionFunc(nil)
// OptionFunc implements the Option interface.
type OptionFunc func(*actorSystem)
func (f OptionFunc) Apply(c *actorSystem) {
f(c)
}
// WithExpireActorAfter sets the actor expiry duration.
// After such duration an idle actor will be expired and removed from the actor system
func WithExpireActorAfter(duration time.Duration) Option {
return OptionFunc(
func(a *actorSystem) {
a.expireActorAfter = duration
},
)
}
// WithLogger sets the actor system custom log
func WithLogger(logger log.Logger) Option {
return OptionFunc(
func(a *actorSystem) {
a.logger = logger
},
)
}
// WithActorInitMaxRetries sets the number of times to retry an actor init process
func WithActorInitMaxRetries(max int) Option {
return OptionFunc(
func(a *actorSystem) {
a.actorInitMaxRetries = max
},
)
}
// WithPassivationDisabled disable the passivation mode
func WithPassivationDisabled() Option {
return OptionFunc(
func(a *actorSystem) {
a.expireActorAfter = -1
},
)
}
// WithSupervisorDirective sets the supervisor strategy directive
// that will be globally used when there is no supervisor directive sets
func WithSupervisorDirective(directive SupervisorDirective) Option {
return OptionFunc(
func(a *actorSystem) {
a.supervisorDirective = directive
},
)
}
// WithRemoting enables remoting on the actor system
func WithRemoting(host string, port int32) Option {
return OptionFunc(
func(a *actorSystem) {
a.remotingEnabled.Store(true)
a.port = port
a.host = host
},
)
}
// WithClustering enables the cluster mode.
// Deprecated: use rather WithCluster which offers a fluent api to set cluster configuration
func WithClustering(provider discovery.Provider, partitionCount uint64, minimumPeersQuorum uint16, discoveryPort, peersPort int, kinds ...Actor) Option {
return OptionFunc(
func(a *actorSystem) {
a.clusterEnabled.Store(true)
replicaCount := 2
if minimumPeersQuorum < 2 {
replicaCount = 1
}
a.clusterConfig = NewClusterConfig().
WithDiscovery(provider).
WithPartitionCount(partitionCount).
WithDiscoveryPort(discoveryPort).
WithPeersPort(peersPort).
WithMinimumPeersQuorum(uint32(minimumPeersQuorum)).
WithReplicaCount(uint32(replicaCount)).
WithKinds(kinds...)
},
)
}
// WithCluster enables the cluster mode
func WithCluster(config *ClusterConfig) Option {
return OptionFunc(
func(a *actorSystem) {
a.clusterEnabled.Store(true)
a.clusterConfig = config
},
)
}
// WithShutdownTimeout sets the shutdown timeout
// The timeout needs to be considerable reasonable based upon the total number of actors
// the system will probably needs. The graceful timeout is shared amongst all actors and children
// actors created in the system to graceful shutdown via a cancellation context.
func WithShutdownTimeout(timeout time.Duration) Option {
return OptionFunc(
func(a *actorSystem) {
a.shutdownTimeout = timeout
},
)
}
// WithStash sets the stash buffer size
func WithStash() Option {
return OptionFunc(
func(a *actorSystem) {
a.stashEnabled = true
},
)
}
// WithPartitionHasher sets the partition hasher.
func WithPartitionHasher(hasher hash.Hasher) Option {
return OptionFunc(
func(a *actorSystem) {
a.partitionHasher = hasher
},
)
}
// WithActorInitTimeout sets how long in seconds an actor start timeout
func WithActorInitTimeout(timeout time.Duration) Option {
return OptionFunc(
func(a *actorSystem) {
a.actorInitTimeout = timeout
},
)
}
// WithPeerStateLoopInterval sets the peer state loop interval
func WithPeerStateLoopInterval(interval time.Duration) Option {
return OptionFunc(
func(system *actorSystem) {
system.peersStateLoopInterval = interval
},
)
}
// WithJanitorInterval sets the janitor interval
func WithJanitorInterval(interval time.Duration) Option {
return OptionFunc(
func(system *actorSystem) {
system.janitorInterval = interval
},
)
}