-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfuture.go
57 lines (48 loc) · 1.27 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
package jobq
import "time"
var futureTimeout = 10 * time.Second
// Future is a future result of a Job.
type Future struct {
result chan result
closed bool
}
// result is the result of a Job.
type result struct {
value interface{}
err error
}
// NewFuture creates a new Future.
func NewFuture() *Future {
// Note: The buffer size of 1 is important here. If the buffer size is 0, the result
// will be sent to the result channel and block until the result is read. If the
// buffer size is 1, the result will be sent to the result channel and the Job will
// continue executing.
return &Future{result: make(chan result, 1), closed: false}
}
// Result returns the result of the Job. This method blocks until the result is available.
func (f *Future) Result() (interface{}, error) {
// r := <-f.result
// return r.value, r.err
if f.closed {
return nil, ErrFutureClosed
}
r := <-f.result
f.Release()
return r.value, r.err
}
// SetResult sets the result of the Job.
func (f *Future) SetResult(value interface{}, err error) {
select {
case f.result <- result{value, err}:
return
case <-time.After(futureTimeout):
f.Release()
}
}
// Release releases the memory used by the Future.
func (f *Future) Release() {
if !f.closed {
f.closed = true
close(f.result)
}
}