msync

package module
v0.3.3 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2024 License: BSD-3-Clause Imports: 5 Imported by: 0 modules, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

msync

GoDoc CI

This repository defines a library of Go types to help in the management of concurrency.

Documentation

Overview

Package msync defines some helpful types for managing concurrency.

Index

Constants

This section is empty.

Variables

View Source
var ErrClosed = errors.New("collector is closed")

ErrClosed is the sentinel error reported by a collector that is closed before a value could be delivered.

Functions

This section is empty.

Types

type Collector added in v0.0.4

type Collector[T any] struct {
	// contains filtered or unexported fields
}

A Collector manages a fan-in channel shared by multiple readers and writers. It wraps and behaves like a normal buffered channel, but when c is closed any pending writes are safely terminated and report errors rather than panicking.

func Collect added in v0.3.0

func Collect[T any](ch chan T) *Collector[T]

Collect returns a new collector wrapping the specified channel.

The collector takes ownership of ch, and the caller must not access ch except via the methods of the Collector. The channel may be buffered or unbuffered.

func (*Collector[T]) Close added in v0.0.4

func (c *Collector[T]) Close() error

Close closes the collector, which closes the receiver and causes any pending sends to fail. If c is already closed, Close returns ErrClosed. Close can be called repeatedly, but from at most one goroutine at a time.

func (*Collector[T]) Recv added in v0.0.4

func (c *Collector[T]) Recv() <-chan T

Recv returns a channel to which sent values are delivered. The returned channel is closed when c is closed. After c is closed, Recv returns a nil channel.

func (*Collector[T]) Send added in v0.0.4

func (c *Collector[T]) Send(ctx context.Context, v T) error

Send sends v to the collector. It blocks until v is delivered, c closes, or ctx ends. If c closes or ctx ends before v is sent, Send reports an error; otherwise Send returns nil.

type Flag added in v0.1.0

type Flag[T any] struct {
	// contains filtered or unexported fields
}

A Flag is a level-triggered single-value buffer shared by a producer and a consumer. A producer calls [Flag.Send] to make a value available, and a consumer calls Flag.Ready to obtain a channel which delivers the most recently-sent value.

Setting value on the flag does not block: Once a value is set, additional values are discarded until the buffered value is consumed.

The Flag.Ready method returns a channel that delivers buffered values to the consumer. Once a value has been consumed, the buffer is empty and the producer can send another.

func NewFlag added in v0.1.0

func NewFlag[T any]() *Flag[T]

NewFlag constructs a new empty flag.

func (*Flag[T]) Ready added in v0.1.0

func (f *Flag[T]) Ready() <-chan T

Ready returns a channel that delivers a value when one is available. Once a value is received, further reads on the channel will block until the flag is Set again.

func (*Flag[T]) Set added in v0.1.0

func (f *Flag[T]) Set(v T) bool

Set buffers or discards v, and reports whether v was buffered (true) or discarded (false). Set does not block.

type Linked added in v0.2.0

type Linked[T any] struct {
	// contains filtered or unexported fields
}

Linked is a snapshot of a Value acquired by a call to its LoadLink method.

A linked snapshot may be "valid" or "invalid". It is "valid" if a call to its StoreCond method could succeed at some point in the future; otherwise it is "invalid". A valid snapshot may become invalid, but an invalid snapshot is permanently so.

func (*Linked[T]) Get added in v0.2.0

func (lv *Linked[T]) Get() T

Get returns the current contents of the snapshot.

func (*Linked[T]) StoreCond added in v0.2.0

func (lv *Linked[T]) StoreCond(v T) bool

StoreCond attempts to update the linked Value with v, and reports whether doing so succeeded. Once StoreCond has been called, lv is invalid.

StoreCond succeeds if no successful StoreCond or Set operation has been applied to the underlying Value since the LoadLink that initialized lv.

func (*Linked[T]) Validate added in v0.2.0

func (lv *Linked[T]) Validate() bool

Validate reports whether a call to StoreCond would have succeeded given the current state of lv. When Validate reports true, it means lv was valid at the time of the call; it may have become invalid by the time the caller receives the result. If Validate reports false, lv is forever invalid.

type Throttle added in v0.3.2

type Throttle[T any] struct {
	// contains filtered or unexported fields
}

A Throttle coalesces calls to a function so that all goroutines concurrently executing Throttle.Call share the result of a single execution of the function made by one of the participants.

A Throttle is initially idle. The first goroutine to execute Throttle.Call on an idle throttle begins a new session. All goroutines that call the throttle during an active session block until either:

  • The context governing that call ends, in which case it reports a zero value and the error that ended the context.

  • The goroutine executing the throttled function completes its call and reports a value and error, which is then shared among all the goroutines participating in the session.

If the execution of the throttled function ends because the context governing its calling goroutine ended, another waiting goroutine (if any) is woken up and given an oppoartunity to call the throttled function. Once all concurrent goroutines have returned, the throttle is once again idle, and the next caller will begin a new session.

func NewThrottle added in v0.3.2

func NewThrottle[T any](run func(context.Context) (T, error)) *Throttle[T]

NewThrottle constructs a new Throttle that executes run.

func (*Throttle[T]) Call added in v0.3.2

func (t *Throttle[T]) Call(ctx context.Context) (T, error)

Call invokes the function guarded by t. Call is safe for concurrent use by multiple goroutines.

type ThrottleSet added in v0.3.3

type ThrottleSet[T any] struct {
	// contains filtered or unexported fields
}

ThrottleSet is a collection of Throttle values indexed by key. A zero value is ready for use, but must not be copied after first use.

func (*ThrottleSet[T]) Call added in v0.3.3

func (t *ThrottleSet[T]) Call(ctx context.Context, key string, run func(context.Context) (T, error)) (T, error)

Call calls the throttle associated with key, constructing a new one if necessary. Call is safe for use by multiple concurrent goroutines.

All concurrent callers of Call with a given key share a single Throttle.

type Trigger

type Trigger struct {
	// contains filtered or unexported fields
}

Trigger is an edge-triggered condition shared by multiple goroutines. The Trigger.Ready method returns a channel that is closed when the trigger is activated.

When a trigger is first created it is inactive. It remains inactive until Trigger.Set or Trigger.Signal is called, either of which causes the current ready channel to be closed. Once a trigger has been activated, it remains active until it is reset. Use Trigger.Reset to make the trigger inactive.

The Trigger.Signal method activates and then immediately resets the trigger, acting as Set and Reset done in a single step.

A zero Trigger is ready for use, and is inactive, but must not be copied after any of its methods have been called.

func NewTrigger

func NewTrigger() *Trigger

NewTrigger constructs a new inactive Trigger.

func (*Trigger) Ready

func (t *Trigger) Ready() <-chan struct{}

Ready returns a channel that is closed when t is activated. If t is active when Ready is called, the returned channel will already be closed.

func (*Trigger) Reset added in v0.3.0

func (t *Trigger) Reset()

Reset resets the trigger. If the trigger was already inactive, it has no effect.

func (*Trigger) Set added in v0.3.0

func (t *Trigger) Set()

Set activates the trigger. If the trigger was already active, it has no effect.

func (*Trigger) Signal

func (t *Trigger) Signal()

Signal activates and immediately resets the trigger. If the trigger was already active, this is equivalent to Trigger.Reset.

type Value added in v0.0.2

type Value[T any] struct {
	// contains filtered or unexported fields
}

A Value is a mutable container for a single value of type T that can be concurrently accessed by multiple goroutines. A zero Value is ready for use, but must not be copied after its first use.

The Value takes ownership of the value in its custody. In particular, if a pointer or a slice is stored in a Value, the caller must not modify the contents without separate synchronization.

func NewValue added in v0.0.2

func NewValue[T any](init T) *Value[T]

NewValue creates a new Value with the given initial value.

func (*Value[T]) Get added in v0.0.2

func (v *Value[T]) Get() T

Get returns the current value stored in v.

func (v *Value[T]) LoadLink(lv *Linked[T]) *Linked[T]

LoadLink links a view of the current value of v. If lv == nil, a new linked value is allocated and returned. Otherwise, the contents of *lv are replaced and lv is returned.

func (*Value[T]) Set added in v0.0.2

func (v *Value[T]) Set(newValue T)

Set updates the value stored in v to newValue. Calling Set also wakes any goroutines that are blocked in the Wait method. Set invalidates any linked snapshots open on v, even if the target value does not change.

func (*Value[T]) Wait added in v0.0.2

func (v *Value[T]) Wait(ctx context.Context) (T, bool)

Wait blocks until v.Set is called, or until ctx ends, and returns the current value in v. The flag indicates whether Set was called (true) or ctx timed out (false).

If v.Set is not called before ctx ends, Wait returns the value v held when Wait was called. Otherwise, Wait returns the value from one of the v.Set calls made during its execution.

If multiple goroutines set v concurrently with a call to Wait, the Wait call will return the value from one of them, but not necessarily the first.

Jump to

Keyboard shortcuts

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