syncutil

package
v0.0.0-...-a5f7211 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2024 License: Apache-2.0 Imports: 4 Imported by: 4

Documentation

Overview

sync-related utils

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Async

func Async(fn func() error) <-chan error

runs an async error-returning task in a goroutine and eventually returns the result in a channel

func Concurrently

func Concurrently(
	ctx context.Context,
	concurrency int,
	task func(ctx context.Context) error,
	workSubmitter func(taskCtx context.Context),
) error

convenience wrapper around errgroup to run a simple task concurrently, and stop each worker if any of the tasks encounter an error during run

func Concurrently2

func Concurrently2[T any](
	ctx context.Context,
	concurrency int,
	consume func(ctx context.Context, task T) error,
	produce func(taskCtx context.Context, work chan T) error,
) error

convenience wrapper around errgroup to run a simple task concurrently, and stop each worker if any of the tasks encounter an error during run

func LockAndUnlock

func LockAndUnlock(mu *sync.Mutex) func()

helper for reducing lock boilerplate by using "defer LockAndUnlock(mu)()" to queue its unlocking

func ProducerForSlice

func ProducerForSlice[T any](input []T) func(context.Context, chan T) error

creates a `Concurrently2()` compatible producer that submits tasks to work queue from a slice

Types

type MutexMap

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

Think of this as an infinite number of named bathroom stalls. Each named stall can only be occupied by one person. When you TryLock(): a) it won't open if it's already occupied. (because it's locked inside) decide to do something else b) it opens and you get in and the stall gets reserved/locked for you. When you get out

you call the unlock callback you obtained from TryLock() to return the stall for use.

func NewMutexMap

func NewMutexMap() *MutexMap

func (*MutexMap) Lock

func (m *MutexMap) Lock(key string) func()

func (*MutexMap) TryLock

func (m *MutexMap) TryLock(key string) (func(), bool)

returns false if gate already open/reserved returns true if gate was opened for you. you have to use the returned func to release it

Jump to

Keyboard shortcuts

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