https://github.com/bluele/gosem
gosem provides multiple semaphore functions. Currently supports inmemory, redis implementation.
https://github.com/bluele/gosem
go golang redis semaphore
Last synced: 9 months ago
JSON representation
gosem provides multiple semaphore functions. Currently supports inmemory, redis implementation.
- Host: GitHub
- URL: https://github.com/bluele/gosem
- Owner: bluele
- License: mit
- Created: 2016-03-08T14:50:01.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2017-07-14T09:37:20.000Z (about 9 years ago)
- Last Synced: 2025-07-22T20:42:19.644Z (about 1 year ago)
- Topics: go, golang, redis, semaphore
- Language: Go
- Size: 12.7 KB
- Stars: 4
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# gosem
[](https://circleci.com/gh/bluele/gosem/tree/master)
gosem provides multiple semaphore functions.
Currently supports inmemory, redis implementation.
# Example
```go
// You can set a number of available resources.
var sem = gosem.NewSemaphore(3)
func main() {
for _, task := range tasks {
// Do task if available resources exists.
sem.Acquire()
go func(task Task) {
defer sem.Release()
do(task)
}(task)
}
}
```
# Features
## Memory-based semaphore
### Simple semaphore
Semaphore is a semaphore that can be used to coordinate the number of accessing shared data from multiple goroutine.
```go
sem := gosem.NewSemaphore(5)
// acquire a new resource
sem.Acquire()
// release the resource
sem.Release()
```
### Time semaphore
TimeSemaphore is a semaphore that can be used to coordinate the number of accessing shared data from multiple goroutine with specified time.
```go
sem := gosem.NewTimeSemaphore(5, time.Second)
// acquire a new resource
sem.Acquire()
// release the resource
sem.Release()
```
## Redis-based semaphore
Implements a semaphore using Redis.
It can be used between processes that are on the multiple host.
### Counting semaphore
```go
import "gopkg.in/redis.v3"
client := redis.NewClient(
&redis.Options{Network: "tcp", Addr: "127.0.0.1:6379"},
)
sem := gosem.NewRedisSemaphore(client, 5)
// acquire a new resource
sem.Acquire()
// release the resource
sem.Release()
```
# Contribution
1. Fork ([https://github.com/bluele/gosem/fork](https://github.com/bluele/gosem/fork))
1. Create a feature branch
1. Commit your changes
1. Rebase your local changes against the master branch
1. Run test suite with the `go test ./...` command and confirm that it passes
1. Run `gofmt -s`
1. Create new Pull Request
# Author
**Jun Kimura**
*
*