suspend

package
v0.0.0-...-202847b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 1, 2023 License: Apache-2.0 Imports: 4 Imported by: 0

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

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 NewState

func NewState() State

NewState creates a new idle State.

func NewStateWithHash

func NewStateWithHash(h hash.Hash64) State

NewStateWithHash convenience helper function to create a new State with a hash algorithm.

func (State) Done

func (s State) Done(key uint64) error

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) DoneBytes

func (s State) DoneBytes(key []byte) error

DoneBytes same as Done.

func (State) Initialized

func (s State) Initialized() bool

Initialized returns true if the type State has been initialized with the call to NewHashState().

func (State) Len

func (s State) Len() int

Len returns the number of processed Hashes.

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

func (s State) ShouldStart(key uint64) bool

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

func (s State) ShouldStartBytes(key []byte) bool

ShouldStartBytes same as ShouldStart.

func (State) ShouldWait

func (s State) ShouldWait(key uint64) bool

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

func (s State) ShouldWaitBytes(key []byte) bool

ShouldWaitBytes same as ShouldWait.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL