Sync

Semaphore

Semaphore is commonly used for limiting max concurrency, e.g. limiting max number of concurrent connections.

Use

import "github.com/burningxflame/gx/sync/sem"

// Create a semaphore.
// The ca specifies the capacity of the semaphore.
s := sem.New(ca)

// Acquire a permit from the semaphore.
// If none is available, block until one is available or ctx.Done channel is closed.
err := s.Acquire(ctx)

// Try to acquire a permit from the semaphore.
// Return true if available, false otherwise.
ok := s.TryAcquire()

// Release a permit to the semaphore.
s.Release()

// Return the number of available permits.
n := s.Available()

Benchmark

goos: darwin
goarch: amd64
cpu: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz

// Acquire + Release
BenchmarkAcquireRelease-12     22697646         51.96 ns/op        0 B/op        0 allocs/op

Keyed-Semaphores

Commonly used for limiting max concurrency per key, e.g. limiting max number of concurrent connections per client.

import "github.com/burningxflame/gx/sync/sem"

// Create Keyed-Semaphores.
// The ca specifies the capacity of every semaphore.
// If the number of semaphores exceeds sizeHint, will try to shrink, i.e. remove unused semaphores.
ks := sem.NewKSem[string](ca, sizeHint)

// Get the semaphore of a key, create if not exist.
s := ks.Get(key)

// ... use the semaphore

// Get the semaphore of another key, create if not exist.
s2 := ks.Get(key2)

// ... use the semaphore

Generic Concurrency-Safe Map

Generic version of std sync.Map.

Import

import "github.com/burningxflame/gx/sync/gmap"

API:
CompareAndDelete, CompareAndSwap, Delete, Load, LoadAndDelete, LoadOrStore, Range, Store, Swap

Sample:
map ↗

Generic Concurrency-Safe Set

Implemented based on std sync.Map.

Import

import "github.com/burningxflame/gx/sync/set"

API:
Add, Delete, Len, Contain, Range, Replace

Sample:
set ↗