forked from hashicorp/nomad
/
future.go
54 lines (46 loc) · 1.15 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
package singleton
import (
"github.com/hashicorp/nomad/helper/pluginutils/loader"
"github.com/hashicorp/nomad/helper/uuid"
)
// future is a sharable future for retrieving a plugin instance or any error
// that may have occurred during the creation.
type future struct {
waitCh chan struct{}
id string
err error
instance loader.PluginInstance
}
// newFuture returns a new pull future
func newFuture() *future {
return &future{
waitCh: make(chan struct{}),
id: uuid.Generate(),
}
}
func (f *future) equal(o *future) bool {
if f == nil && o == nil {
return true
} else if f != nil && o != nil {
return f.id == o.id
} else {
return false
}
}
// wait waits till the future has a result
func (f *future) wait() *future {
<-f.waitCh
return f
}
// result returns the results of the future and should only ever be called after
// wait returns.
func (f *future) result() (loader.PluginInstance, error) {
return f.instance, f.err
}
// set is used to set the results and unblock any waiter. This may only be
// called once.
func (f *future) set(instance loader.PluginInstance, err error) {
f.instance = instance
f.err = err
close(f.waitCh)
}