hashque

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2025 License: BSD-3-Clause Imports: 3 Imported by: 0

Documentation

Overview

Package hashqueue provides a serialisation function call execution mechanism.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ResultChan

func ResultChan[K comparable, V any](g *Group[K], key K, f func() (V, error)) <-chan Result[V]

ResultChan executes a function that returns a value and an error, and returns a channel that will receive the result. This allows for non-blocking usage patterns.

func ResultFunc

func ResultFunc[K comparable, T any](g *Group[K], key K, f func() (T, error)) (T, error)

ResultFunc executes a function that returns a value and an error.

func TryValueFunc

func TryValueFunc[K comparable, V any](g *Group[K], key K, f func() V) (V, bool)

TryValueFunc attempts to execute a function that returns a single value, without blocking if the channel is full. It returns the value and a boolean indicating success.

func ValueFunc

func ValueFunc[K comparable, T any](g *Group[K], key K, f func() T) T

ValueFunc executes a function that returns a single value.

Types

type Group

type Group[K comparable] struct {
	// contains filtered or unexported fields
}

Group provides a mechanism to deduplicate function calls by key. When multiple goroutines call functions with the same key concurrently, only one execution occurs while all callers receive the result of that execution.

func (*Group[K]) Do

func (g *Group[K]) Do(key K, f func())

Do executes the given function once for each key, blocking until the function completes.

func (*Group[K]) DoContext added in v0.3.2

func (g *Group[K]) DoContext(ctx context.Context, key K, f func()) error

DoContext attempts to queue the given function for execution but doesn't block if the channel is full. It returns an error if the context is cancelled or if the queue is full. The key parameter determines which worker the function will be queued to.

func (*Group[K]) TryDo

func (g *Group[K]) TryDo(key K, f func()) bool

TryDo attempts to queue the given function for execution but doesn't block if the channel is full. It returns a boolean indicating whether the function was successfully queued.

type Result

type Result[V any] struct {
	Val V
	Err error
}

Result encapsulates the return value and error from a function call.

Jump to

Keyboard shortcuts

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