based

package module
v1.0.7 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2024 License: MIT Imports: 10 Imported by: 6

README

based

Go Reference Go Report Go Coverage CodeQL

Go utilities for pet projects.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AwaitSignal

func AwaitSignal(ctx context.Context, signals ...os.Signal) error

AwaitSignal waits for specified signals.

func Close added in v1.0.0

func Close(value any) error

func Nop

func Nop()

Nop is an empty function.

func ReentrantLock

func ReentrantLock(ctx context.Context, key any, requiredLevel uint, lock LockerFunc) (context.Context, context.CancelFunc)

ReentrantLock will check context to see if the lock with required level or higher is already acquired. If not, it will call provided LockerFunc.

func Validate

func Validate(value any) error

Types

type Clock

type Clock interface {

	// Now returns the current time according to this Clock.
	Now() time.Time
}

Clock is a simple clock interface.

var StandardClock Clock = ClockFunc(time.Now)

StandardClock provides the current local time using time.Now.

type ClockFunc

type ClockFunc func() time.Time

ClockFunc is a functional Clock adapter.

func (ClockFunc) Now

func (fn ClockFunc) Now() time.Time

type FuncRef added in v1.0.0

type FuncRef[T any] func(ctx context.Context) (T, error)

func (FuncRef[T]) Get added in v1.0.0

func (r FuncRef[T]) Get(ctx context.Context) (T, error)

type Goroutine

type Goroutine interface {

	// Cancel signals cancellation to the goroutine.
	Cancel()

	// Join waits for goroutine to finish.
	Join(ctx context.Context) error
}

Goroutine describes goroutine handle.

func Go

func Go(ctx context.Context, fn func(ctx context.Context)) Goroutine

Go starts the provided function in a new goroutine and returns the Goroutine handle.

type Locker

type Locker interface {

	// Lock acquires the lock.
	// It returns a new Context which should be checked for error before proceeding with the execution.
	// Context scope must be limited to holding the lock.
	// CancelFunc must not be nil and is used in defer statement to release the lock or perform other cleanup,
	// even in cases of context errors.
	//
	// Example usage:
	//  ctx, cancel := mu.Lock(ctx)
	//  defer cancel()
	//  if err := ctx.Err(); err != nil {
	//    return err
	//  }
	Lock(ctx context.Context) (context.Context, context.CancelFunc)
}

Locker provides synchronization semantics similar to mutex. Implementations must provide interruptibility and reentrant locking.

func Semaphore

func Semaphore(clock Clock, size int, interval time.Duration) Locker

Semaphore allows at most `size` events at given time interval. This becomes a classic semaphore when interval = 0, and a simple mutex when size = 1.

type LockerFunc

type LockerFunc func(ctx context.Context) (context.Context, context.CancelFunc)

LockerFunc is a functional Locker adapter.

var Unlocker LockerFunc = func(ctx context.Context) (context.Context, context.CancelFunc) {
	return ctx, Nop
}

Unlocker does nothing.

func (LockerFunc) Lock

type Lockers

type Lockers []Locker

Lockers provides a way to simultaneously acquire and release multiple locks.

func (Lockers) Lock

type RWMutex

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

RWMutex is a sync.RWMutex implementation.

func (*RWMutex) Lock

func (*RWMutex) RLock

type Ref

type Ref[T any] interface {
	Get(ctx context.Context) (T, error)
}

Ref references a value which may require calculation.

func FutureFuncRef added in v1.0.0

func FutureFuncRef[T any](ctx context.Context, ref FuncRef[T]) Ref[T]

func FutureRef added in v1.0.0

func FutureRef[T any](ctx context.Context, ref Ref[T]) Ref[T]

FutureRef represents a result of an asynchronous computation that may or may not be available yet.

func LazyFuncRef added in v1.0.0

func LazyFuncRef[T any](ref FuncRef[T]) Ref[T]

func LazyRef added in v1.0.0

func LazyRef[T any](ref Ref[T]) Ref[T]

LazyRef creates a Ref with on-demand execution of the provided Ref. The result of the first execution will be cached and returned immediately for all consequent calls.

func SafeFuncRef added in v1.0.0

func SafeFuncRef[T any](ref Ref[T]) Ref[T]

func SafeRef added in v1.0.0

func SafeRef[T any](ref Ref[T]) Ref[T]

SafeRef calls Ref with panic handling and other safeguards.

type Unit

type Unit struct{}

Unit represents an empty type, similar to Unit in Scala.

type WriteThroughCache

type WriteThroughCache[K comparable, V comparable] struct {
	// contains filtered or unexported fields
}

WriteThroughCache provides a simple write-through cache implementation.

func NewWriteThroughCache

func NewWriteThroughCache[K comparable, V comparable](storage WriteThroughCacheStorage[K, V]) *WriteThroughCache[K, V]

NewWriteThroughCache creates a WriteThroughCache instance.

func (*WriteThroughCache[K, V]) Get

func (c *WriteThroughCache[K, V]) Get(ctx context.Context, key K) (V, error)

Get attempts to retrieve the value from cache. If no cached value is present, it will be retrieved from underlying storage. If the value retrieved from storage is not zero, it will be cached.

func (*WriteThroughCache[K, V]) Update

func (c *WriteThroughCache[K, V]) Update(ctx context.Context, key K, value V) error

Update updates the value in cache and underlying storage.

type WriteThroughCacheStorage

type WriteThroughCacheStorage[K comparable, V comparable] interface {

	// Load loads the value from storage.
	Load(ctx context.Context, key K) (V, error)

	// Update persists the value in storage.
	Update(ctx context.Context, key K, value V) error
}

WriteThroughCacheStorage defines an interface for the underlying storage of WriteThroughCache.

type WriteThroughCacheStorageFunc

type WriteThroughCacheStorageFunc[K comparable, V comparable] struct {
	LoadFn   func(ctx context.Context, key K) (V, error)
	UpdateFn func(ctx context.Context, key K, value V) error
}

WriteThroughCacheStorageFunc provides a functional adapter for WriteThroughCacheStorage.

func (WriteThroughCacheStorageFunc[K, V]) Load

func (f WriteThroughCacheStorageFunc[K, V]) Load(ctx context.Context, key K) (V, error)

func (WriteThroughCacheStorageFunc[K, V]) Update

func (f WriteThroughCacheStorageFunc[K, V]) Update(ctx context.Context, key K, value V) error

type WriteThroughCached

type WriteThroughCached[V comparable] struct {
	// contains filtered or unexported fields
}

WriteThroughCached provides write-through cache semantics for a single value.

func NewWriteThroughCached

func NewWriteThroughCached[K comparable, V comparable](storage WriteThroughCacheStorage[K, V], key K) *WriteThroughCached[V]

NewWriteThroughCached creates a WriteThroughCached instance.

func (*WriteThroughCached[V]) Get

func (c *WriteThroughCached[V]) Get(ctx context.Context) (V, error)

Get attempts to retrieve the value from cache. If no cached value is present, it will be retrieved from underlying storage. If the value retrieved from storage is not zero, it will be cached.

func (*WriteThroughCached[V]) Update

func (c *WriteThroughCached[V]) Update(ctx context.Context, value V) error

Update updates the value in cache and underlying storage.

Jump to

Keyboard shortcuts

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