channel

package
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Aug 6, 2025 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WaitForAllReady

func WaitForAllReady(ctx context.Context, ready ...*Ready) bool

WaitForAllReady returns true if all objects are ready, or false if one of them is closed or the context ended before that.

Types

type Reader

type Reader[T any] interface {
	WithContext[T]
	Read() (T, bool)
	ReadWithTimeout(time.Duration) (T, bool)
}

Reader helps in reading from channels with context.

func NewReader

func NewReader[T any](ctx context.Context, input <-chan T) Reader[T]

NewReader instantiate a Reader.

type ReaderWriter

type ReaderWriter[T any] interface {
	Reader[T]
	Writer[T]
}

ReaderWriter helps in reading and writing to channels with context. It enforces a quick release pattern so a worker will stop immediately when the context is done, and won't be hanged until the channel is closed (if ever). By using the helper instead of using channels directly, we can make sure our system never hangs on channels. This is helpful for two scenarios when close(chan) can't be used:

  1. We want to stop the worker even if there are still items in the channel.
  2. There are multiple writers to a channel, so it is difficult to close the channel while avoiding possible panics.

func Make

func Make[T any](ctx context.Context, size int) ReaderWriter[T]

Make create a new channel with context.

func NewReaderWriter

func NewReaderWriter[T any](ctx context.Context, inputOutput chan T) ReaderWriter[T]

NewReaderWriter instantiate a ReaderWriter.

type Ready

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

Ready supports waiting for readiness and notifying of readiness. It also supports closing to release waiters.

func NewReady

func NewReady() *Ready

NewReady instantiate a new Ready.

func (*Ready) Close

func (r *Ready) Close()

Close notifies of closing.

func (*Ready) Reset

func (r *Ready) Reset()

Reset resets the object to be reused.

func (*Ready) SignalReady

func (r *Ready) SignalReady()

SignalReady signals readiness.

func (*Ready) WaitForReady

func (r *Ready) WaitForReady(ctx context.Context) bool

WaitForReady returns true if the object is ready, or false if it is closed or the context ended before that.

type WithContext

type WithContext[T any] interface {
	Context() context.Context
	WithContext(ctx context.Context) ReaderWriter[T]
}

WithContext supports fetching and updating the context.

type Writer

type Writer[T any] interface {
	WithContext[T]
	Write(value T) bool
}

Writer helps in writing to channels with context.

func NewWriter

func NewWriter[T any](ctx context.Context, output chan<- T) Writer[T]

NewWriter instantiate a Writer.

Jump to

Keyboard shortcuts

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