-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactor.go
280 lines (247 loc) · 6.4 KB
/
actor.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// Tideland Go Actor
//
// Copyright (C) 2019-2023 Frank Mueller / Tideland / Oldenburg / Germany
//
// All rights reserved. Use of this source code is governed
// by the new BSD license.
package actor // import "tideland.dev/go/actor"
//--------------------
// IMPORTS
//--------------------
import (
"context"
"fmt"
"sync/atomic"
"time"
)
//--------------------
// CONSTANTS
//--------------------
const (
// defaultQueueCap is the minimum and default capacity
// of the async actions queue.
defaultQueueCap = 256
)
//--------------------
// HELPER
//--------------------
// Action defines the signature of an actor action.
type Action func()
// Recoverer defines the signature of a function for recovering
// from a panic during executing an action. The reason is the
// panic value. The function should return the error to be
// returned by the Actor. If the error is nil, the Actor will
// continue to work.
type Recoverer func(reason any) error
// Finalizer defines the signature of a function for finalizing
// the work of an Actor. The error is the one returned by the
// Actor.
type Finalizer func(err error) error
//--------------------
// ACTOR
//--------------------
// request wraps an action with its context.
type request struct {
ctx context.Context
done chan struct{}
err error
action Action
}
// newRequest creates a request including a done channel. The
// Action is wrapped with a closure which closes the done channel
// after the action has been executed.
func newRequest(ctx context.Context, action Action) *request {
return &request{
ctx: ctx,
done: make(chan struct{}),
action: action,
}
}
// execute checks if the request context is canceled or timed out.
// If not, it performs the action and closes the done channel.
func (req *request) execute() {
defer close(req.done)
select {
case <-req.ctx.Done():
req.err = req.ctx.Err()
default:
req.action()
}
}
// Actor introduces the actor model, where call simply are executed
// sequentially in a backend goroutine.
type Actor struct {
ctx context.Context
cancel func()
requests chan *request
recoverer Recoverer
finalizer Finalizer
err atomic.Pointer[error]
done chan struct{}
}
// Go starts an Actor with the given options.
func Go(options ...Option) (*Actor, error) {
// Init with options.
act := &Actor{
ctx: context.Background(),
}
for _, option := range options {
if err := option(act); err != nil {
return nil, err
}
}
// Ensure default settings.
act.ctx, act.cancel = context.WithCancel(act.ctx)
if act.requests == nil {
act.requests = make(chan *request, defaultQueueCap)
}
if act.recoverer == nil {
act.recoverer = func(reason any) error {
return fmt.Errorf("panic during actor action: %v", reason)
}
}
if act.finalizer == nil {
act.finalizer = func(err error) error { return err }
}
// Start the backend, wait for it to be ready.
started := make(chan struct{})
go act.backend(started)
select {
case <-started:
case <-time.After(time.Second):
return nil, fmt.Errorf("actor backend did not start")
}
return act, nil
}
// DoAsync sends the actor function to the backend goroutine and returns
// when it's queued.
func (act *Actor) DoAsync(action Action) error {
return act.DoAsyncWithContext(context.Background(), action)
}
// DoAsyncWithContext send the actor function to the backend and returns
// when it's queued. A context allows to cancel the action or add a timeout.
func (act *Actor) DoAsyncWithContext(ctx context.Context, action Action) error {
req := newRequest(ctx, action)
return act.send(req)
}
// DoSync executes the actor function and returns when it's done.
func (act *Actor) DoSync(action Action) error {
return act.DoSyncWithContext(context.Background(), action)
}
// DoSyncWithContext executes the action and returns when it's done.
// A context allows to cancel the action or add a timeout.
func (act *Actor) DoSyncWithContext(ctx context.Context, action Action) error {
req := newRequest(ctx, action)
err := act.send(req)
if err != nil {
return err
}
return act.wait(req)
}
// Done returns a channel that is closed when the Actor terminates.
func (act *Actor) Done() <-chan struct{} {
return act.done
}
// IsDone allows to simply check if the Actor is done in a select
// or if statement.
func (act *Actor) IsDone() bool {
select {
case <-act.done:
return true
default:
return false
}
}
// Err returns information if the Actor has an error.
func (act *Actor) Err() error {
err := act.err.Load()
if err == nil {
return nil
}
return *err
}
// Stop terminates the Actor backend.
func (act *Actor) Stop() {
if act.IsDone() {
return
}
act.cancel()
}
// send sends a request to the backend.
func (act *Actor) send(req *request) error {
// Check if we're error free and still working.
if act.err.Load() != nil {
return *act.err.Load()
}
if act.IsDone() {
return fmt.Errorf("actor is done")
}
// Send the request to the backend.
select {
case act.requests <- req:
case <-req.ctx.Done():
return fmt.Errorf("action context sending: %v", req.ctx.Err())
case <-act.ctx.Done():
return fmt.Errorf("actor context sending: %v", act.ctx.Err())
}
return nil
}
// wait waits for synchronous requests to be done or returning an error.
func (act *Actor) wait(req *request) error {
select {
case <-req.done:
case <-req.ctx.Done():
return fmt.Errorf("action context waiting: %v", req.ctx.Err())
case <-act.ctx.Done():
return fmt.Errorf("actor context waiting: %v", act.ctx.Err())
}
return req.err
}
// backend runs the goroutine of the Actor.
func (act *Actor) backend(started chan struct{}) {
defer act.finalize()
close(started)
act.done = make(chan struct{})
// Work as long as we're not stopped.
for !act.IsDone() {
act.work()
}
}
// work runs the select in a loop, including
// a possible repairer.
func (act *Actor) work() {
defer func() {
// Check panics and possibly send notification.
if reason := recover(); reason != nil {
err := act.recoverer(reason)
if err != nil {
act.err.Store(&err)
close(act.done)
}
}
}()
// Select in loop.
for {
select {
case <-act.ctx.Done():
close(act.done)
return
case req := <-act.requests:
req.execute()
}
}
}
// finalize takes care for a clean loop finalization.
func (act *Actor) finalize() {
var ferr error
err := act.err.Load()
if err != nil {
ferr = act.finalizer(*err)
} else {
ferr = act.finalizer(nil)
}
if ferr != nil {
act.err.Store(&ferr)
}
}
// EOF