chanutil

package
v0.55.1 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2025 License: AGPL-3.0 Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ChanContext

type ChanContext[T any] struct {
	Ctx context.Context
	Ch  chan T
}

ChanContext is a wrapper around a channel that binds it to a context.

func NewChanContext

func NewChanContext[T any](ctx context.Context, buffer int) ChanContext[T]

NewChanContext creates a new ChanContext instance.

func (ChanContext[T]) Close

func (cc ChanContext[T]) Close()

Close closes the channel.

func (ChanContext[T]) Recv

func (cc ChanContext[T]) Recv() (v T, ok bool)

Recv receives a value from the channel. If the context is canceled, this method returns immediately with a zero value and false. If the channel is closed, this method returns a zero value and false.

func (ChanContext[T]) Send

func (cc ChanContext[T]) Send(v T) bool

Send sends a value to the channel. If the context is canceled, this method returns immediately with false.

func (ChanContext[T]) SendNonBlocking

func (cc ChanContext[T]) SendNonBlocking(v T) bool

SendNonBlocking sends a value to the channel without blocking. If the context is canceled or the channel is full, this method returns immediately with false.

type FanIn

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

FanIn is a fan-in channel multiplexer. It takes multiple input channels and merges them into a single output channel. The implementation is thread-safe.

func NewFanIn

func NewFanIn[T any](output chan T) *FanIn[T]

NewFanIn creates a new FanIn instance.

func (*FanIn[T]) AutoClose

func (fi *FanIn[T]) AutoClose()

AutoClose will automatically close the output channel when all input channels are closed. This method must be called only after at least one input channel has been added. Otherwise, it will immediately close the output channel. This method is idempotent and non-blocking.

func (*FanIn[T]) Close

func (fi *FanIn[T]) Close()

Close closes the output channel. This method must be called only after all input channels are closed. Otherwise, the code may panic due to sending to a closed channel. To make sure that all input channels are closed, a call to this method can be preceded by a call to the Wait method. Alternatively, the AutoClose method can be used.

func (*FanIn[T]) Input

func (fi *FanIn[T]) Input(chs ...<-chan T)

Input adds a new input channel. If the fan-in is already closed, this method panics.

func (*FanIn[T]) Wait

func (fi *FanIn[T]) Wait()

Wait blocks until all the input channels are closed.

type FanOut

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

FanOut is a fan-out channel demultiplexer. It takes a single input channel and distributes its values to multiple output channels. The input channel is emptied even if there are no output channels. Output channels are closed when the input channel is closed. The implementation is thread-safe.

func NewFanOut

func NewFanOut[T any](input <-chan T) *FanOut[T]

NewFanOut creates a new FanOut instance.

func (*FanOut[T]) Close

func (fo *FanOut[T]) Close(ch <-chan T)

Close stops sending values to the given output channel and closes it.

If the output channel is already closed or it was not created by the Output or OutputContext method, this method does nothing.

func (*FanOut[T]) Output

func (fo *FanOut[T]) Output() <-chan T

Output returns a new output channel.

func (*FanOut[T]) OutputContext

func (fo *FanOut[T]) OutputContext(ctx context.Context) <-chan T

OutputContext returns a new output channel. The channel is closed when the given context is canceled.

Jump to

Keyboard shortcuts

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