Documentation
¶
Index ¶
- func Load[K comparable, T any](store Store[K, any], key K) (value T, ok bool)
- func LoadAndDelete[K comparable, T any](store Store[K, any], key K) (value T, loaded, ok bool)
- func LoadOrStore[K comparable, T any](store Store[K, any], key K, value any) (actual T, loaded, ok bool)
- func Swap[K comparable, T any](store Store[K, any], key K, value any) (previous T, loaded, ok bool)
- type RunGroup
- type Runner
- type RunnerFunc
- type Store
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Load ¶
func Load[K comparable, T any](store Store[K, any], key K) (value T, ok bool)
Load is the helper function for Store. It provides typed operation for the store with the any typed value. Load returns the stored value for a key from the store. If the key was not found, it returns zero value of type T and false. If type conversion failed, it returns zero value of type T and false. If nil was loaded, it always results in type assertion failure.
func LoadAndDelete ¶
func LoadAndDelete[K comparable, T any](store Store[K, any], key K) (value T, loaded, ok bool)
LoadAndDelete is the helper function for Store. It provides typed operation for the store with the any typed value. LoadAndDelete deletes the value for a key, returning the previous value if any. The loaded result reports whether the key was present. If type conversion failed, it returns zero value of type T and false as ok. If nil was loaded, it always results in type assertion failure.
func LoadOrStore ¶
func LoadOrStore[K comparable, T any](store Store[K, any], key K, value any) (actual T, loaded, ok bool)
LoadOrStore is the helper function for Store. It provides typed operation for the store with the any typed value. LoadOrStore returns the existing value for the key if present obtained from the store. Otherwise, it stores and returns the given value. The loaded result is true if the value was loaded, false if stored. If type conversion failed, it returns zero value of type T and false as ok. If nil was loaded, it always results in type assertion failure.
func Swap ¶
Swap is the helper function for Store. It provides typed operation for the store with the any typed value. Swap swaps the value for a key and returns the previous value if any. The loaded result reports whether the key was present. If type conversion failed, it returns zero value of type T and false as ok. If nil was loaded, it always results in type assertion failure.
Types ¶
type RunGroup ¶
type RunGroup struct { // OnStart is called for all runners before they actually run. // The runner is given by the first argument. OnStart func(r Runner) // OnExit is called for all runners after they exited. // The runner is given by the first argument. // The error its runner returned is passed by the second argument. OnExit func(r Runner, err error) // contains filtered or unexported fields }
RunGroup is the group of Runner. It can awake all registered runners using [RunGroup.]
func (*RunGroup) Register ¶
Register registers the given runners to the group. It's safe to call Register from different goroutine simultaneously. Registered runners are used from the next run and does not affect the function group already running.
func (*RunGroup) RegisterFunc ¶
RegisterFunc registers the given function as a runner to the group. It's safe to call RegisterFunc from different goroutine simultaneously. Registered function is used from the next run and does not affect the function group already running.
func (*RunGroup) Run ¶
Run is the alias for RunGroup.RunAndFailFast.
func (*RunGroup) RunAndFailFast ¶
RunAndFailFast runs the runners in a new goroutine each. RunAndFailFast waits runners to return. If one of the runners returned non nil error, it cancel the context that passed to the all runners and return with the obtained non nil error. RunGroup.RunAndFailFast and RunGroup.RunAndWaitAll works same except for the case that at least one runner returned non nil error. It's safe to call RunAndWaitAll from different goroutine simultaneously. If given ctx is nil, it uses a new context created with context.Background.
func (*RunGroup) RunAndWaitAll ¶
RunAndWaitAll runs the runners in a new goroutine each. It waits all runners to return and collects the errors returned from them. It returns the collected errors joined by errors.Join. RunGroup.RunAndFailFast and RunGroup.RunAndWaitAll works same except for the case that at least one runner returned non nil error. It's safe to call RunAndWaitAll from different goroutine simultaneously. If given ctx is nil, it uses a new context created with context.Background.
type Runner ¶
type Runner interface { // Run run the process that implements this interface. Run(context.Context) error }
Runner run the process that implements this interface.
type RunnerFunc ¶
RunnerFunc is the runnable function. It implements Runner interface.
type Store ¶
type Store[K comparable, T any] interface { // Store sets the value for a key. // It the key already exists, it replaces the value. Store(key K, value T) // Load returns the stored value for a key. // If the kwy was not found, it returns zero value of type T and false. Load(key K) (value T, ok bool) // Delete deletes the value for a key. Delete(key K) // Clear deletes all the entries. Clear() // Swap swaps the value for a key and returns the previous value if any. // The loaded result reports whether the key was present. Swap(key K, value T) (previous T, loaded bool) // LoadAndDelete deletes the value for a key, returning the previous value if any. // The loaded result reports whether the key was present. LoadAndDelete(key K) (value T, loaded bool) // LoadOrStore returns the existing value for the key if present. // Otherwise, it stores and returns the given value. // The loaded result is true if the value was loaded, false if stored. LoadOrStore(key K, value T) (actual T, loaded bool) // CompareAndSwap swaps the old and new values for key // if the value stored is equal to old. // The old value must be of a comparable type. CompareAndSwap(key K, old, new T) (swapped bool) // CompareAndDelete deletes the entry for key if its value is equal to old. // The old value must be of a comparable type. // If there is no current value for key, CompareAndDelete returns false. CompareAndDelete(key K, old T) (deleted bool) // Range calls f sequentially for each key and value. // If f returns false, range stops the iteration. Range(f func(key K, value any) bool) }
Store stores sets of key and values and provide their operation. sync.Map satisfies the Store[any,any] interface.