-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathactor.go
84 lines (70 loc) · 1.9 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
package orchestrator
import (
"context"
"encoding/json"
)
// Actor represents a long-running flow that is capable of interacting with
// the outside world through its inbox and outbox.
type Actor struct {
cancel func()
inbox chan map[string]any
outbox chan Result
}
func NewActor(f func(ctx context.Context, ab *ActorBehavior)) *Actor {
inbox := make(chan map[string]any)
outbox := make(chan Result)
// Create a new cancellable context for the actor execution.
ctx, cancel := context.WithCancel(context.Background())
ab := &ActorBehavior{
ctx: ctx,
inbox: inbox,
outbox: outbox,
}
go f(ctx, ab)
return &Actor{
cancel: cancel,
inbox: inbox,
outbox: outbox,
}
}
func (a *Actor) Inbox() chan<- map[string]any {
return a.inbox
}
func (a *Actor) Outbox() <-chan Result {
return a.outbox
}
func (a *Actor) Stop() {
a.cancel()
}
func (a *Actor) String() string {
return "<Actor>"
}
func (a *Actor) MarshalJSON() ([]byte, error) {
return json.Marshal(a.String())
}
// ActorBehavior is a helper for sending and receiving data to/from the outside
// world on behalf of an actor (i.e. within the context of task execution).
type ActorBehavior struct {
ctx context.Context
inbox <-chan map[string]any
outbox chan<- Result
}
// Send sends data to the outside world through the outbox. If the internal
// context is done (cancelled or timed out), it will return immediately.
func (ab *ActorBehavior) Send(output Output, err error) {
select {
case ab.outbox <- Result{Output: output, Err: err}:
case <-ab.ctx.Done():
}
}
// Receive receives data from the outside world through the inbox. If the
// internal context is done (cancelled or timed out), it will return nil immediately.
func (ab *ActorBehavior) Receive() map[string]any {
select {
case input := <-ab.inbox:
return input
case <-ab.ctx.Done():
// Return nil to indicate that the corresponding actor has been canceled.
return nil
}
}