Fast distributed & resizable golang semaphore based on CAS.
- allows weighted acquire/release;
- supports cancellation via context;
- allows change semaphore limit after creation;
- faster than channel based semaphores.
Based off of github.com/marusama/semaphore
Initiate
import "github.com/consyse/semaphore"
...
sem := semaphore.New(5) // new semaphore with limit = 5
Acquire
sem.Acquire(ctx, n) // acquire n with context
sem.TryAcquire(n) // try acquire n without blocking
...
ctx := context.WithTimeout(context.Background(), time.Second)
sem.Acquire(ctx, n) // acquire n with timeout
Release
sem.Release(n) // release n
Change semaphore limit
sem.SetLimit(new_limit) // set new semaphore limit