Documentation
¶
Index ¶
- func Alloc[T any](v T) *T
- func ForEach[T any](ctx context.Context, ch <-chan T, handler func(T) error) error
- func GenBytes(rng *rand.Rand, n int) []byte
- func GenMap[K comparable, V any](rng *rand.Rand, genK GenF[K], genV GenF[V]) map[K]V
- func GenMapN[K comparable, V any](rng *rand.Rand, n int, genK GenF[K], genV GenF[V]) map[K]V
- func GenSlice[T any](rng *rand.Rand, gen GenF[T]) []T
- func GenSliceN[T any](rng *rand.Rand, n int, gen GenF[T]) []T
- func GenString(rng *rand.Rand, n int) string
- func GenTimestamp(rng *rand.Rand) time.Time
- func GetWSEndpoint(endpoint string) string
- func IgnoreCancel(err error) error
- func ProtoClone[T proto.Message](item T) T
- func ProtoEqual[T proto.Message](a, b T) bool
- func Recv[T any](ctx context.Context, ch <-chan T) (zero T, err error)
- func RecvOrClosed[T any](ctx context.Context, ch <-chan T) (T, bool, error)
- func Send[T any](ctx context.Context, ch chan<- T, v T) error
- func SendOrDrop[T any](ch chan<- T, v T) error
- func Sleep(ctx context.Context, d time.Duration) error
- func SleepUntil(ctx context.Context, t time.Time) error
- func Slice[T any](v ...T) []T
- func TestDiff[T any](want, got T) error
- func TestEqual[T any](a, b T) bool
- func TestRng() *rand.Rand
- func TestRngSplit(rng *rand.Rand) *rand.Rand
- func WaitFor(ctx context.Context, interval time.Duration, check func() bool) error
- func WaitForWithTimeout(ctx context.Context, interval, timeout time.Duration, check func() bool) error
- func WithTimeout(ctx context.Context, d time.Duration, f func(ctx context.Context) error) error
- func WithTimeout1[R any](ctx context.Context, d time.Duration, f func(ctx context.Context) (R, error)) (R, error)
- func Zero[T any]() (zero T)
- type AtomicRecv
- type AtomicSend
- func (w *AtomicSend) Iter(ctx context.Context, f func(ctx context.Context, v T) error) error
- func (w *AtomicSend) Load() T
- func (w *AtomicSend[T]) Send(value T)
- func (w *AtomicSend[T]) Subscribe() AtomicRecv[T]
- func (w *AtomicSend[T]) Update(f func(T) (T, bool))
- func (w *AtomicSend) Wait(ctx context.Context, pred func(T) bool) (T, error)
- type AtomicWatch
- func (w *AtomicWatch) Iter(ctx context.Context, f func(ctx context.Context, v T) error) error
- func (w *AtomicWatch) Load() T
- func (w *AtomicWatch[T]) Store(value T)
- func (w *AtomicWatch[T]) Subscribe() AtomicRecv[T]
- func (w *AtomicWatch[T]) Update(f func(T) (T, bool))
- func (w *AtomicWatch) Wait(ctx context.Context, pred func(T) bool) (T, error)
- type Duration
- type GenF
- type Hash
- type Mutex
- type NoCompare
- type NoCopy
- type Option
- type ProtoConv
- func (c ProtoConv[T, P]) DecodeOpt(p P) (Option[T], error)
- func (c ProtoConv[T, P]) DecodeReq(p P) (T, error)
- func (c ProtoConv[T, P]) DecodeSlice(p []P) ([]T, error)
- func (c ProtoConv[T, P]) EncodeOpt(mv Option[T]) P
- func (c ProtoConv[T, P]) EncodeSlice(t []T) []P
- func (c ProtoConv[T, P]) Test(want T) error
- type ProtoMessage
- type ReadOnly
- type Semaphore
- type Watch
- type WatchCtrl
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ForEach ¶
ForEach is a helper function that reads from a channel and calls a handler for each item. this avoids needing a lot of for/select boilerplate everywhere.
func GenTimestamp ¶
GenTimestamp generates a random timestamp.
func GetWSEndpoint ¶
func IgnoreCancel ¶
IgnoreCancel returns nil if the error is context.Canceled, err otherwise.
func ProtoClone ¶
ProtoClone clones a proto.Message object.
func ProtoEqual ¶
ProtoEqual compares two proto.Message objects.
func RecvOrClosed ¶
RecvOrClosed receives a value from a channel, returns false if channel got closed, or returns an error if the context is canceled.
func SendOrDrop ¶
SendOrDrop send a value to channel if not full or drop the item if the channel is full.
func SleepUntil ¶
SleepUntil sleeps until deadline t or until the context is canceled.
func Slice ¶
func Slice[T any](v ...T) []T
Slice constructs a slice. It is a syntax sugar for `[]T{v...}`, which avoids spelling out T. Not very useful if you need to spell out T to construct the elements: in that case you might prefer the []T{{...},{...}} syntax instead.
func TestRngSplit ¶
TestRngSplit returns a new random number splitted from the given one. This is a very primitive splitting, known to result with dependent randomness. If that ever causes a problem, we can switch to SplitMix.
func WaitForWithTimeout ¶
func WaitForWithTimeout(ctx context.Context, interval, timeout time.Duration, check func() bool) error
WaitForWithTimeout polls a check function until it returns true, the context is canceled, or the timeout is reached.
func WithTimeout ¶
WithTimeout executes a function with a timeout.
Types ¶
type AtomicRecv ¶
type AtomicRecv[T any] struct { // contains filtered or unexported fields }
AtomicRecv is a read-only reference to AtomicWatch.
func (AtomicRecv) Iter ¶
Iter executes sequentially the function f on each value of the atomic watch. Context passed to f is canceled when the next value is available. Exits when the returned error is different from nil and context.Canceled, or when the context passed to Iter is canceled (after f exits).
type AtomicSend ¶
type AtomicSend[T any] struct { // contains filtered or unexported fields }
func NewAtomicSend ¶
func NewAtomicSend[T any](value T) (w AtomicSend[T])
func (*AtomicSend) Iter ¶
Iter executes sequentially the function f on each value of the atomic watch. Context passed to f is canceled when the next value is available. Exits when the returned error is different from nil and context.Canceled, or when the context passed to Iter is canceled (after f exits).
func (*AtomicSend) Load ¶
func (w *AtomicSend) Load() T
Load returns the current value of the atomic watch. Does not do any locking.
func (*AtomicSend[T]) Send ¶
func (w *AtomicSend[T]) Send(value T)
Store updates the value of the atomic watch.
func (*AtomicSend[T]) Subscribe ¶
func (w *AtomicSend[T]) Subscribe() AtomicRecv[T]
func (*AtomicSend[T]) Update ¶
func (w *AtomicSend[T]) Update(f func(T) (T, bool))
Update conditionally updates the value of the atomic watch.
type AtomicWatch ¶
type AtomicWatch[T any] struct { // contains filtered or unexported fields }
AtomicWatch stores a pointer to an IMMUTABLE value. Loading and waiting for updates do NOT require locking. TODO(gprusak): remove mutex and rename to AtomicSend, this will allow for sharing a mutex across multiple AtomicSenders.
func NewAtomicWatch ¶
func NewAtomicWatch[T any](value T) (w AtomicWatch[T])
NewAtomicWatch creates a new AtomicWatch with the given initial value.
func (*AtomicWatch) Iter ¶
Iter executes sequentially the function f on each value of the atomic watch. Context passed to f is canceled when the next value is available. Exits when the returned error is different from nil and context.Canceled, or when the context passed to Iter is canceled (after f exits).
func (*AtomicWatch) Load ¶
func (w *AtomicWatch) Load() T
Load returns the current value of the atomic watch. Does not do any locking.
func (*AtomicWatch[T]) Store ¶
func (w *AtomicWatch[T]) Store(value T)
Store updates the value of the atomic watch.
func (*AtomicWatch[T]) Subscribe ¶
func (w *AtomicWatch[T]) Subscribe() AtomicRecv[T]
Subscribe returns a view-only API of the atomic watch.
func (*AtomicWatch[T]) Update ¶
func (w *AtomicWatch[T]) Update(f func(T) (T, bool))
Update conditionally updates the value of the atomic watch.
type Duration ¶
Duration is a wrapper type around time.Duration that supports JSON marshaling/unmarshaling. nolint:recvcheck
func (Duration) MarshalText ¶
MarshalText implements json.TextMarshaler interface to convert Duration to JSON string.
func (*Duration) UnmarshalText ¶
UnmarshalText implements json.TextUnmarshaler.
type Mutex ¶
type Mutex[T any] struct { // contains filtered or unexported fields }
Mutex guards access to object of type T.
type NoCompare ¶
type NoCompare [0]func()
NoCompare may be added to structs which must not be used as map keys.
type NoCopy ¶
type NoCopy struct{}
NoCopy may be added to structs which must not be copied after the first use.
See https://golang.org/issues/8005#issuecomment-190753527 for details.
Note that it must not be embedded, otherwise Lock and Unlock methods will be exported.
type Option ¶
Option type inspired https://pkg.go.dev/github.com/samber/mo.
func (Option[T]) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface. Note that it is defined on value, not pointer, because json.Marshal cannot call pointer methods on fields (i.e. it is broken by design).
func (*Option[T]) Or ¶
func (o *Option[T]) Or(def T) T
Or returns the value if present, otherwise returns the default value.
func (*Option[T]) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaler interface.
type ProtoConv ¶
type ProtoConv[T any, P ProtoMessage] struct { Encode func(T) P Decode func(P) (T, error) }
ProtoConv is a pair of functions to encode and decode between a type and a ProtoMessage.
func (ProtoConv[T, P]) DecodeOpt ¶
DecodeOpt decodes a ProtoMessage into a T, returning nil if p is nil.
func (ProtoConv[T, P]) DecodeReq ¶
DecodeReq decodes a ProtoMessage into a T, returning an error if p is nil.
func (ProtoConv[T, P]) DecodeSlice ¶
DecodeSlice decodes a slice of P into a slice of T.
func (ProtoConv[T, P]) EncodeSlice ¶
func (c ProtoConv[T, P]) EncodeSlice(t []T) []P
EncodeSlice encodes a slice of T into a slice of P.
type ProtoMessage ¶
type ProtoMessage interface {
comparable
proto.Message
}
ProtoMessage is comparable proto.Message.
type ReadOnly ¶
type ReadOnly struct{}
ReadOnly - if a struct embeds ReadOnly, its private fields will be compared by TestEqual.
type Semaphore ¶
type Semaphore struct {
// contains filtered or unexported fields
}
Semaphore provides a way to bound concurrenct access to a resource.
func NewSemaphore ¶
NewSemaphore constructs a new semaphore with n permits.
type Watch ¶
type Watch[T any] struct { // contains filtered or unexported fields }
Watch stores a value of type T. Essentially a mutex, that can be awaited for updates.
func NewWatch ¶
NewWatch constructs a new watch with the given value. Note that value in the watch cannot be changed, so T should be a pointer type if updates are required.
func (*Watch[T]) Lock ¶
Lock returns an iterator which locks the watch and yields the guarded object. The watch is unlocked when the iterator is done. If the watch is nil, the iterator is a no-op. Additionally the WatchCtrl object is provided to the yield function: * to unlock -> wait for the update -> lock again, call ctrl.Wait(ctx) * to signal an update, call ctrl.Updated().
type WatchCtrl ¶
type WatchCtrl struct {
// contains filtered or unexported fields
}
WatchCtrl controls the locked object in a Watch. It is provided only in the iterator returned by Lock(). Should NOT be stored anywhere.
func (*WatchCtrl) Updated ¶
func (c *WatchCtrl) Updated()
Updated signals waiters that the value in the watch has been updated.
func (*WatchCtrl) Wait ¶
Wait waits for the value in the watch to be updated. Should be called only after locking the watch, i.e. within Lock() iterator. It unlocks -> waits for the update -> locks again.