github.com/tschmi5/nomad@v0.11.8/helper/pluginutils/singleton/future.go (about) 1 package singleton 2 3 import ( 4 "github.com/hashicorp/nomad/helper/pluginutils/loader" 5 "github.com/hashicorp/nomad/helper/uuid" 6 ) 7 8 // future is a sharable future for retrieving a plugin instance or any error 9 // that may have occurred during the creation. 10 type future struct { 11 waitCh chan struct{} 12 id string 13 14 err error 15 instance loader.PluginInstance 16 } 17 18 // newFuture returns a new pull future 19 func newFuture() *future { 20 return &future{ 21 waitCh: make(chan struct{}), 22 id: uuid.Generate(), 23 } 24 } 25 26 func (f *future) equal(o *future) bool { 27 if f == nil && o == nil { 28 return true 29 } else if f != nil && o != nil { 30 return f.id == o.id 31 } else { 32 return false 33 } 34 } 35 36 // wait waits till the future has a result 37 func (f *future) wait() *future { 38 <-f.waitCh 39 return f 40 } 41 42 // result returns the results of the future and should only ever be called after 43 // wait returns. 44 func (f *future) result() (loader.PluginInstance, error) { 45 return f.instance, f.err 46 } 47 48 // set is used to set the results and unblock any waiter. This may only be 49 // called once. 50 func (f *future) set(instance loader.PluginInstance, err error) { 51 f.instance = instance 52 f.err = err 53 close(f.waitCh) 54 }