Documentation
¶
Overview ¶
Package suspend provides a complicated duplicate function call suppression mechanism.
It picks from n-Goroutines the first one to do a job and suspends the following.
The following Goroutines may continue once the first one calls a broadcast signal to release the suspended.
Index ¶
- type State
- func (s State) Done(key uint64) error
- func (s State) DoneBytes(key []byte) error
- func (s State) Initialized() bool
- func (s State) Len() int
- func (s *State) Reset()
- func (s State) ShouldStart(key uint64) bool
- func (s State) ShouldStartBytes(key []byte) bool
- func (s State) ShouldWait(key uint64) bool
- func (s State) ShouldWaitBytes(key []byte) bool
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type State ¶
type State struct { // Hash64 optional feature to be used when calling the functions which // accepts a byte slice as input argument. The byte slice gets hashed into // an uint64. If Hash64 remains empty, a panic will pop up. Depending on the // hashing algorithm, collisions can occur. // http://programmers.stackexchange.com/questions/49550/which-hashing-algorithm-is-best-for-uniqueness-and-speed hash.Hash64 // contains filtered or unexported fields }
State defines a struct which can atomically and in concurrent cases detect if a process runs, idles or has been finished. If another Goroutine checks the running state and returns true this Goroutine will be suspended and must wait until the main Goroutine calls Done().
Use case for the Hashstate is mainly in middleware Service types for net/http.Requests to load configuration values atomically and make other requests wait until the configuration has been fully loaded and applied. After that skip the whole access to State and use the configuration values cached in the middleware service type.
func NewStateWithHash ¶
NewStateWithHash convenience helper function to create a new State with a hash algorithm.
func (State) Done ¶
Done releases all the waiting Goroutines caught with the function IsRunning() and sets the internal state to done. Any subsequent calls to ShouldWait() and ShouldStart() with the same key will restart the process. You must call Reset() to fully clear the internal cache once all of your operations are done.
func (State) Initialized ¶
Initialized returns true if the type State has been initialized with the call to NewHashState().
func (*State) Reset ¶
func (s *State) Reset()
Reset clears the internal list of uint64(es). Will panic if called on an uninitialized State.
func (State) ShouldStart ¶
ShouldStart reports true atomically for a specific uint64, if a process can start. Safe for concurrent use. You should check ShouldStart() first in your switch statement:
switch { case hs.ShouldStart(scopeHash): // start here your process err := hs.Done(scopeHash) case hs.ShouldWait(scopeHash): // do nothing and wait ... } // proceed here with your program
func (State) ShouldStartBytes ¶
ShouldStartBytes same as ShouldStart.
func (State) ShouldWait ¶
ShouldWait checks atomically if the State has been set to run and if so the calling Goroutine waits until Done() has been called. You should use ShouldWait() as second case in your switch statement:
switch { case hs.ShouldStart(scopeHash): // start here your process err := hs.Done(scopeHash) case hs.ShouldWait(scopeHash): // do nothing and wait ... } // proceed here with your program
func (State) ShouldWaitBytes ¶
ShouldWaitBytes same as ShouldWait.