Skip to content

Latest commit

 

History

History
executable file
·
36 lines (32 loc) · 947 Bytes

README.md

File metadata and controls

executable file
·
36 lines (32 loc) · 947 Bytes

distributedsemaphore

License

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

Usage

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