Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

🚦 Semaphore pattern implementation with timeout of lock/unlock operations.

License

NotificationsYou must be signed in to change notification settings

kamilsk/semaphore

Repository files navigation

Semaphore pattern implementation with timeout of lock/unlock operations.

BuildQualityDocumentationCoverageAwesome

💡 Idea

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.

🏆 Motivation

...

🤼‍♂️ How to

Quick start

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))

🧩 Integration

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.

🤲 Outcomes

Console tool for command execution in parallel

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

asciicast

See more detailshere.


made with ❤️ for everyone


[8]ページ先頭

©2009-2025 Movatter.jp