sync

package module
v0.0.0-...-add63db Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2025 License: Apache-2.0 Imports: 9 Imported by: 6

README

go-sync

Go Report Card GitHub go.mod Go version Slack Invite

A collection of synchronization utilities.

Status

Consider this project to be in alpha. The API is not stable and may change at any time.

Overview

sync.Executor - a simple executor interface, with a bounded executor implementation available by using sync.NewExecutor

sync.Collector - a simple interface to concurrently execute tasks and get the results

sync.List - a concurrent list, queue, and stack implementation

Documentation

Index

Constants

View Source
const ExecutorDefault = ""

Variables

This section is empty.

Functions

func Collect

func Collect[From, To any](ctx *context.Context, executorName string, iterator iter.Seq[From], processor func(From) (To, error), accumulator func(From, To)) error

Collect iterates over the provided iterator, executing the processor in parallel to map each incoming value to a result. The accumulator is used to apply the results, with an exclusive lock; accumulator will never execute in parallel. All errors returned from processor functions will be joined with errors.Join as the returned error.

func Collect2

func Collect2[From1, From2, To any](ctx *context.Context, executorName string, iterator iter.Seq2[From1, From2], processor func(From1, From2) (To, error), accumulator func(From1, From2, To)) error

Collect2 is a specialized Collect call which accepts an iter.Seq2 and maps to processor and accumulator taking 2 input parameters

func CollectMap

func CollectMap[From comparable, To any](ctx *context.Context, executorName string, values iter.Seq[From], processor func(From) (To, error), result map[From]To) error

CollectMap is a specialized Collect call which fills a map using the incoming value as a key, mapped to the result

func CollectSlice

func CollectSlice[From, To any](ctx *context.Context, executorName string, values iter.Seq[From], processor func(From) (To, error), slice *[]To) error

CollectSlice is a specialized Collect call which appends results to a slice

func HasContextExecutor

func HasContextExecutor(ctx context.Context, name string) bool

HasContextExecutor returns true when the named executor is available in the context

func ParallelWriter

func ParallelWriter(ctx context.Context, executorName string, writers ...io.Writer) io.Writer

ParallelWriter returns a writer that writes the contents of each write call in parallel to all provided writers

func SetContextExecutor

func SetContextExecutor(ctx context.Context, name string, executor Executor) context.Context

SetContextExecutor returns a context with the named executor for use with GetExecutor

func ToIndexSeq

func ToIndexSeq[T any](values []T) iter.Seq2[int, T]

ToIndexSeq converts a []T to an iter.Seq2[int,T] where the index is the first parameter

func ToSeq

func ToSeq[T any](values []T) iter.Seq[T]

ToSeq converts a slice to an iter.Seq

func ToSeq2

func ToSeq2[K comparable, V any](values map[K]V) iter.Seq2[K, V]

ToSeq2 converts a map[K]V to an iter.Seq2[K,V]

func ToSlice

func ToSlice[T any](values iter.Seq[T]) (everything []T)

ToSlice takes an iter.Seq and returns a slice of the values returned

Types

type Appender

type Appender[T any] interface {
	Append(value T)
}

Appender to allow values to be appended

type ChildExecutor

type ChildExecutor interface {
	ChildExecutor() Executor
}

ChildExecutor interface, if implemented, will cause ContextExecutor calls to replace the provided context with one containing a child executor returned from this function. This is used when it is not safe to nest Go calls

type Collection

type Collection[T any] interface {
	Iterable[T]
	Appender[T]
	Remove(value T)
	Contains(value T) bool
	Len() int
}

Collection is a generic collection of values, which can be added to, removed from, and provide a length

type Executor

type Executor interface {
	// Go adds a unit of work to be executed by the executor. Depending on the execution strategy this may be blocking
	// or may execute the function directly
	Go(func())

	// Wait blocks and waits for all the executing functions to be completed before returning, or the context is cancelled.
	// if more functions are added to be executed by this executor after the Wait call, these will also complete before Wait proceeds
	// If the context is canceled, any queued functions will not be executed
	Wait(context.Context)
}

Executor the executor interface allows for different strategies to execute units of work and wait for all units of work to be completed

func ContextExecutor

func ContextExecutor(ctx *context.Context, name string) Executor

ContextExecutor returns an executor in context with the given name, or a serial executor if none exists and replaces the context with one that contains a new executor which won't deadlock

func NewExecutor

func NewExecutor(maxConcurrency int) Executor

NewExecutor returns an Executor based on the desired concurrency:

< 0: unbounded, spawn a new goroutine for each Go call
  0: serial, executes in the same thread/routine as the caller of Go
> 0: a bounded executor with the maximum concurrency provided

type Iterable

type Iterable[T any] interface {
	// Seq provides an iter.Seq compatible iterator
	Seq(fn func(value T) bool)
}

Iterable provides a function to iterate a series of values

type List

type List[T comparable] struct {
	Locking
	// contains filtered or unexported fields
}

func (*List[T]) Append

func (s *List[T]) Append(value T)

func (*List[T]) Clear

func (s *List[T]) Clear()

Clear removes all values

func (*List[T]) Contains

func (s *List[T]) Contains(value T) bool

func (*List[T]) Dequeue

func (s *List[T]) Dequeue() (value T, ok bool)

func (*List[T]) Enqueue

func (s *List[T]) Enqueue(value T)

func (*List[T]) Len

func (s *List[T]) Len() int

func (*List[T]) Peek

func (s *List[T]) Peek() (value T, ok bool)

func (*List[T]) Pop

func (s *List[T]) Pop() (value T, ok bool)

func (*List[T]) Push

func (s *List[T]) Push(value T)

func (*List[T]) Remove

func (s *List[T]) Remove(value T)

func (*List[T]) RemoveAll

func (s *List[T]) RemoveAll(values iter.Seq[T])

func (*List[T]) Seq

func (s *List[T]) Seq(fn func(value T) bool)

Seq is an iter.Seq compatible iterator function with a read lock, as such it is not possible to modify this list during the loop -- use Values() to obtain a copy for those purposes

func (*List[T]) Update

func (s *List[T]) Update(updater func(values []T) []T)

func (*List[T]) Values

func (s *List[T]) Values() []T

Values returns a slice containing all the values at the time of the call, this should be used sparingly as it is only a snapshot of the current values

type Lockable

type Lockable interface {
	Lock() (unlock UnlockFunc)
	RLock() (unlock UnlockFunc)
}

Lockable implementors provide the ability to RW lock a resource

type Locking

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

Locking is a utility to add Lockable behavior to a struct

func (*Locking) IsExclusiveLock

func (l *Locking) IsExclusiveLock(unlockFunc UnlockFunc) (exclusive bool)

func (*Locking) Lock

func (l *Locking) Lock() (unlock UnlockFunc)

func (*Locking) RLock

func (l *Locking) RLock() (unlock UnlockFunc)

type Provider

type Provider[T any] interface {
	Get() T
}

Provider returns a single item

type Queue

type Queue[T any] interface {
	Enqueue(value T)
	Dequeue() (value T, ok bool)
}

Queue is a generic queue interface

type Stack

type Stack[T any] interface {
	Push(value T)
	Pop() (value T, ok bool)
	Peek() (value T, ok bool)
}

Stack is a generic stack interface

type UnlockFunc

type UnlockFunc func()

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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