Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/michaelwp/go-semaphore
go-semaphore is a Go package that implements the semaphore pattern than can be used to manage the number of Goroutine to run concurrently.
https://github.com/michaelwp/go-semaphore
concurrent go semaphore
Last synced: 6 days ago
JSON representation
go-semaphore is a Go package that implements the semaphore pattern than can be used to manage the number of Goroutine to run concurrently.
- Host: GitHub
- URL: https://github.com/michaelwp/go-semaphore
- Owner: michaelwp
- License: mit
- Created: 2024-06-20T00:44:30.000Z (8 months ago)
- Default Branch: main
- Last Pushed: 2024-06-23T09:03:27.000Z (8 months ago)
- Last Synced: 2024-07-26T13:28:48.274Z (7 months ago)
- Topics: concurrent, go, semaphore
- Language: Go
- Homepage: https://goblog.dev/articles/22
- Size: 7.81 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# go-semaphore
go-semaphore is a Go package that implements the semaphore pattern than can be used to manage the number of Goroutine
to run concurrently.### installation
```shell
go get -d github.com/michaelwp/go-semaphore
```### basic of use
```go
package mainimport (
"fmt"
gosemaphore "github.com/michaelwp/go-semaphore"
"time"
)func main() {
sem := gosemaphore.SemaphoreNew(5) // <-- set up the maximum allowed number of goroutine to run concurrently.
for i := 0; i < 50; i++ {
go func(i int) {
sem.Acquire() // <-- to acquire a seat in buffered channel. This has to be at the beginning of the process.
defer sem.Release() // <-- to release the seat. Strongly suggested to use defer to have a guarantee that this function will be executed when the process is finishes.
Process(i)
}(i)
}
}func Process(taskID int) {
fmt.Println(time.Now().Format("15:04:05"), "Running task with ID", taskID)
time.Sleep(2 * time.Second)
}
```