/
future.go
156 lines (132 loc) · 3.06 KB
/
future.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
package actor
import (
"errors"
"sync"
"time"
"github.com/AsynkronIT/protoactor-go/log"
)
// ErrTimeout is the error used when a future times out before receiving a result.
var ErrTimeout = errors.New("future: timeout")
// NewFuture creates and returns a new actor.Future with a timeout of duration d
func NewFuture(d time.Duration) *Future {
ref := &futureProcess{Future{cond: sync.NewCond(&sync.Mutex{})}}
id := ProcessRegistry.NextId()
pid, ok := ProcessRegistry.Add(ref, "future"+id)
if !ok {
plog.Error("failed to register future process", log.Stringer("pid", pid))
}
ref.pid = pid
if d >= 0 {
ref.t = time.AfterFunc(d, func() {
ref.err = ErrTimeout
ref.Stop(pid)
})
}
return &ref.Future
}
type Future struct {
pid *PID
cond *sync.Cond
// protected by cond
done bool
result interface{}
err error
t *time.Timer
pipes []*PID
completions []func(res interface{}, err error)
}
// PID to the backing actor for the Future result
func (f *Future) PID() *PID {
return f.pid
}
// PipeTo forwards the result or error of the future to the specified pids
func (f *Future) PipeTo(pids ...*PID) {
f.cond.L.Lock()
f.pipes = append(f.pipes, pids...)
//for an already completed future, force push the result to targets
if f.done {
f.sendToPipes()
}
f.cond.L.Unlock()
}
func (f *Future) sendToPipes() {
if f.pipes == nil {
return
}
var m interface{}
if f.err != nil {
m = f.err
} else {
m = f.result
}
for _, pid := range f.pipes {
pid.Tell(m)
}
f.pipes = nil
}
func (f *Future) wait() {
f.cond.L.Lock()
for !f.done {
f.cond.Wait()
}
f.cond.L.Unlock()
}
// Result waits for the future to resolve
func (f *Future) Result() (interface{}, error) {
f.wait()
return f.result, f.err
}
func (f *Future) Wait() error {
f.wait()
return f.err
}
func (f *Future) continueWith(continuation func(res interface{}, err error)) {
f.cond.L.Lock()
defer f.cond.L.Unlock() //use defer as the continuation could blow up
if f.done {
continuation(f.result, f.err)
} else {
f.completions = append(f.completions, continuation)
}
}
// futureProcess is a struct carrying a response PID and a channel where the response is placed
type futureProcess struct {
Future
}
func (ref *futureProcess) SendUserMessage(pid *PID, message interface{}) {
_, msg, _ := UnwrapEnvelope(message)
ref.result = msg
ref.Stop(pid)
}
func (ref *futureProcess) SendSystemMessage(pid *PID, message interface{}) {
ref.result = message
ref.Stop(pid)
}
func (ref *futureProcess) Stop(pid *PID) {
ref.cond.L.Lock()
if ref.done {
ref.cond.L.Unlock()
return
}
ref.done = true
if ref.t != nil {
ref.t.Stop()
}
ProcessRegistry.Remove(pid)
ref.sendToPipes()
ref.runCompletions()
ref.cond.L.Unlock()
ref.cond.Signal()
}
//TODO: we could replace "pipes" with this
//instead of pushing PIDs to pipes, we could push wrapper funcs that tells the pid
//as a completion, that would unify the model
func (f *Future) runCompletions() {
if f.completions == nil {
return
}
for _, c := range f.completions {
c(f.result, f.err)
}
f.completions = nil
}