chancaster

package module
v0.1.2 Latest Latest
Warning

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

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

README

chancaster

chancaster is a Go package that simplifies and streamlines the creation and management of channels, making concurrent programming more intuitive and less error-prone.

Key Features:

  • Effortless Channel Creation: Easily spawn new channels with minimal boilerplate code.
  • Automated Workload Handling: Automatically places associated functions within a concurrent system.
  • State Management: Maintains and manages channel state effortlessly.
  • Simplified Concurrent Programming: Reduces the complexity of working with multiple channels.

chancaster empowers developers to write cleaner, more maintainable, and more efficient concurrent Go code.

Installation

go get -u github.com/AndrewShear/chancaster

Documentation

Index

Constants

View Source
const (
	// ChanNotFound error message returned when a channel is not found for a key
	ChanNotFound = "no channel found for key"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type ChanCaster

type ChanCaster[K comparable, T any] struct {
	// contains filtered or unexported fields
}

ChanCaster is a generic struct that manages concurrent channels associated with keys. It uses an errgroup to manage the execution of functions associated with each channel. K: The type of key used to access channels. T: The type of data that will be sent and received on the channels.

func New

func New[K comparable, T any](ctx context.Context) *ChanCaster[K, T]

New creates a new ChanCasters instance with the provided context. The context will be used to cancel the execution of functions within the errgroup. The returned errgroup context will be canceled the first time a function returns a non-nil error or the first time Wait returns, whichever occurs first.

func (*ChanCaster[K, T]) Add

func (c *ChanCaster[K, T]) Add(key K, fn ChanFunc) error

Add creates a new channel associated with the provided key and runs the provided function concurrently. The function should not take any arguments and must return an error if there is one. If the context is already canceled, an error is returned.

func (*ChanCaster[K, T]) All added in v0.1.2

func (c *ChanCaster[K, T]) All() []K

All returns a slice containing all keys associated with channels in the ChanCaster instance.

func (*ChanCaster[K, T]) Close

func (c *ChanCaster[K, T]) Close(key K) error

Close closes the channel associated with the provided key and removes it from the map. If the key is not found, a CHAN_NOT_FOUND error is returned.

func (*ChanCaster[K, T]) Get

func (c *ChanCaster[K, T]) Get(key K) (chan T, error)

Get retrieves the channel associated with the provided key. If the context is already canceled, an error is returned. If the key is not found, a nil channel and CHAN_NOT_FOUND error are returned.

func (*ChanCaster[K, T]) Publish added in v0.1.1

func (c *ChanCaster[K, T]) Publish(key K, data T) error

Publish sends the provided data to the channel associated with the given key.

If no channel is found for the specified key, a ChanNotFound error is returned.

func (*ChanCaster[K, T]) Wait

func (c *ChanCaster[K, T]) Wait() error

Wait blocks until all functions added with Add() have finished execution. It returns the first non-nil error (if any) from the functions.

type ChanFunc

type ChanFunc func() error

ChanFunc defines the function signature expected by ChanCasters.Add(). This function should not take any arguments and must return an error if there is one.

Jump to

Keyboard shortcuts

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