mirror of
https://github.com/kemko/nomad.git
synced 2026-01-07 02:45:42 +03:00
loader and singleton
This commit is contained in:
committed by
Michael Schurter
parent
b9f36134dc
commit
c19cd2e5cf
54
pluginutils/singleton/future.go
Normal file
54
pluginutils/singleton/future.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package singleton
|
||||
|
||||
import (
|
||||
"github.com/hashicorp/nomad/helper/uuid"
|
||||
"github.com/hashicorp/nomad/pluginutils/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)
|
||||
}
|
||||
Reference in New Issue
Block a user