open-nomad/plugins/shared/singleton/future.go

55 lines
1.1 KiB
Go
Raw Normal View History

2018-09-18 17:08:46 +00:00
package singleton
import (
"github.com/hashicorp/nomad/helper/uuid"
"github.com/hashicorp/nomad/plugins/shared/loader"
)
// 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)
}