- Notifications
You must be signed in to change notification settings - Fork12
🚦 Semaphore pattern implementation with timeout of lock/unlock operations.
License
kamilsk/semaphore
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Semaphore pattern implementation with timeout of lock/unlock operations.
The semaphore provides API to control access to a shared resource by multiple goroutines or limit throughput.
releaser,err:=semaphore.Acquire(breaker.BreakByTimeout(time.Second))iferr!=nil {// timeout exceeded}deferreleaser.Release()
Full description of the idea is availablehere.
...
limiter:=semaphore.New(1000)http.HandleFunc("/",func(rw http.ResponseWriter,req*http.Request) {if_,err:=limiter.Acquire(breaker.BreakByContext(context.WithTimeout(req.Context(),time.Second),),);err!=nil {http.Error(rw,http.StatusText(http.StatusTooManyRequests),http.StatusTooManyRequests)return}deferlimiter.Release()// handle request})log.Fatal(http.ListenAndServe(":80",http.DefaultServeMux))
The library usesSemVer for versioning, and it is notBC-safe through major releases.You can usego modules ordep to manage its version.
Themaster is a feature frozen branch for versions4.3.x and no longer maintained.
$ dep ensure -add github.com/kamilsk/semaphore@4.3.1
Thev4 branch is a continuation of themaster branch for versionsv4.4.xto better integration withgo modules.
$ go get -u github.com/kamilsk/semaphore/v4@v4.3.1
Thev5 branch is an actual development branch.
$ go get -u github.com/kamilsk/semaphore# inside GOPATH and for old Go versions$ go get -u github.com/kamilsk/semaphore/v5# inside Go module, works well since Go 1.11$ dep ensure -add github.com/kamilsk/semaphore@v5.0.0-rc1
Versionv5 focused on integration with the 🚧breaker package.
This example shows how to execute many console commands in parallel.
$ semaphore create 2$ semaphore add -- docker build$ semaphore add -- vagrant up$ semaphore add -- ansible-playbook$ semaphorewait --timeout=1m --notify
See more detailshere.
made with ❤️ for everyone
About
🚦 Semaphore pattern implementation with timeout of lock/unlock operations.