syncutil

package
v1.1.0-beta.0...-33d3863 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LockGroup

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

LockGroup is a map of mutex that locks entries with different id separately. It's used levitate lock contentions of using a global lock.

func NewLockGroup

func NewLockGroup(options ...LockGroupOption) *LockGroup

NewLockGroup create and return an empty lockGroup.

func (*LockGroup) Lock

func (g *LockGroup) Lock(id uint32)

Lock locks the target mutex base on the hash of id.

func (*LockGroup) Unlock

func (g *LockGroup) Unlock(id uint32)

Unlock unlocks the target mutex based on the hash of the id.

type LockGroupOption

type LockGroupOption func(lg *LockGroup)

LockGroupOption configures the lock group.

func WithHash

func WithHash(hashFn func(id uint32) uint32) LockGroupOption

WithHash sets the lockGroup's hash function to provided hashFn.

func WithRemoveEntryOnUnlock

func WithRemoveEntryOnUnlock(removeEntryOnUnlock bool) LockGroupOption

WithRemoveEntryOnUnlock sets the lockGroup's removeEntryOnUnlock to provided value.

type Mutex

type Mutex struct {
	sync.Mutex
}

Mutex is a mutual exclusion lock. The zero value for a Mutex is an unlocked mutex.

Mutex must not be copied after first use.

type OrderedSingleFlight

type OrderedSingleFlight[TResult any] struct {
	// contains filtered or unexported fields
}

OrderedSingleFlight is a utility to make concurrent calls to a function internally only one execution, and shares the execution result to the outer callers. This is similar to the normal singleflight.Group from golang.org/x/sync/singleflight package, but with the following differences:

  • Key is not supported. An OrderedSingleFlight instance runs only one function.
  • The invocation and inner execution is *strongly ordered*, which means, a call to Do gets the result of an execution that is guaranteed to started after the beginning of the invocation, and an earlier execution that started before the current invocation to Do won't be reused for this call. If there's already an execution running in progress and a call to Do happens, it enters the next batch instead of sharing the result of the already-started execution.
  • The return type is generic, instead of interface{}.

func NewOrderedSingleFlight

func NewOrderedSingleFlight[TResult any]() *OrderedSingleFlight[TResult]

NewOrderedSingleFlight creates an instance of OrderedSingleFlight.

func (*OrderedSingleFlight[TResult]) Do

func (s *OrderedSingleFlight[TResult]) Do(ctx context.Context, f func(context.Context) (TResult, error)) (TResult, error)

Do tries to execute the function `f`, or if possible, wait and reuse the result of an execution triggered by another goroutine. See comments of OrderedSingleFlight for details.

func (*OrderedSingleFlight[TResult]) ExecCount

func (s *OrderedSingleFlight[TResult]) ExecCount() int64

ExecCount returns the accumulated number of executions of the inner function.

type RWMutex

type RWMutex struct {
	sync.RWMutex
}

RWMutex is a reader/writer mutual exclusion lock. The lock can be held by an arbitrary number of readers or a single writer. The zero value for a RWMutex is an unlocked mutex.

RWMutex must not be copied after first use.

Jump to

Keyboard shortcuts

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