usync

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Dec 12, 2024 License: BSD-3-Clause Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Combine added in v0.6.0

func Combine[T any, C ChannelRecv[T]](ctx context.Context, channels ...C) <-chan T

Combine returns a channel that continuously returns the result of the select until all input sources are exhauste, or the context is canceled.

func Select added in v0.6.0

func Select[T any, C ChannelRecv[T]](ctx context.Context, channels ...C) (int, T, bool)

A type-safe wrapper around reflect.Select. Taken from: https://stackoverflow.com/questions/19992334

Types

type ChannelRecv added in v0.6.0

type ChannelRecv[T any] interface {
	~chan T | ~<-chan T
}

ChannelRecv is a type constraint that includes all channel types that can be recieved from.

type Error

type Error string

Error defines errors that this package can produce

const (
	ErrAlreadyClosed Error = "AlreadyClosed"
)

func (Error) Error

func (err Error) Error() string

Error fullfills the error interface.

type Gate

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

Gate wraps a channel and allows the receiver to abruptly stop receiving messages without causing the sender to lock up.

func NewBufferedGate

func NewBufferedGate[T any](buffer int) Gate[T]

NewBufferedGate creates a new gate with a buffer.

func NewGate

func NewGate[T any]() Gate[T]

NewGate creates a new gate with no buffer.

func (*Gate[T]) Cap

func (this *Gate[T]) Cap() int

Cap returns the amount of items the channel can hold.

func (*Gate[T]) Close

func (this *Gate[T]) Close() error

Close closes the gate, drains all remaining messages, and closes the channel.

func (*Gate[T]) Len

func (this *Gate[T]) Len() int

Len returns the amount of items in the channel.

func (*Gate[T]) Open

func (this *Gate[T]) Open() bool

Open returns whether the gate is open.

func (*Gate[T]) Receive

func (this *Gate[T]) Receive() <-chan T

Receive returns a receive-only channel that can be used to receive items.

func (*Gate[T]) Reset

func (this *Gate[T]) Reset() error

Reset re-opens the gate if it is closed, and creates a new channel.

func (*Gate[T]) Send

func (this *Gate[T]) Send(item T) bool

Send sends and item to the channel, returning whether the item was sent.

type Locker added in v0.6.0

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

Locker guards access to a value. It must not be copied after first use.

func NewLocker added in v0.6.0

func NewLocker[T any](value T) Locker[T]

NewLocker creates a new locker with the specified value.

func (*Locker[T]) Borrow added in v0.6.0

func (this *Locker[T]) Borrow() (T, func())

Borrow borrows the value from the locker, and returns a function that must immediately be deferred, like this:

value, done := locker.Borrow()
defer done()

From the time Borrow is called to the time the done function is called, it is safe to access the locked object from within the current goroutine.

func (*Locker[T]) Set added in v0.6.0

func (this *Locker[T]) Set(value T)

Set sets the value of the locker.

type RWLocker added in v0.7.0

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

RWLocker guards separate read/write access to a value.

func NewRWLocker added in v0.7.0

func NewRWLocker[T any](value T) RWLocker[T]

NewRWLocker creates a new locker with the specified value. It must not be copied after first use.

func (*RWLocker[T]) Borrow added in v0.7.0

func (this *RWLocker[T]) Borrow() (T, func())

Borrow borrows the value from the locker for write access, and returns a function that must immediately be deferred, like this:

value, done := locker.Borrow()
defer done()

From the time Borrow is called to the time the done function is called, it is safe to access the locked object from within the current goroutine.

func (*RWLocker[T]) RBorrow added in v0.7.0

func (this *RWLocker[T]) RBorrow() (T, func())

RBorrow is like Borrow, but returns the item for read access only. Do not under any circumstances write to an item returned from this method.

func (*RWLocker[T]) Set added in v0.7.0

func (this *RWLocker[T]) Set(value T)

Set sets the value of the locker.

Jump to

Keyboard shortcuts

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