Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/teng231/glock
solution lock for golang, locallock and remote lock base on redis.
https://github.com/teng231/glock
ditributed golang lock redis
Last synced: about 2 months ago
JSON representation
solution lock for golang, locallock and remote lock base on redis.
- Host: GitHub
- URL: https://github.com/teng231/glock
- Owner: teng231
- License: apache-2.0
- Created: 2021-09-15T15:50:33.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2023-01-06T04:45:49.000Z (about 2 years ago)
- Last Synced: 2024-11-23T10:57:15.332Z (about 2 months ago)
- Topics: ditributed, golang, lock, redis
- Language: Go
- Homepage:
- Size: 63.5 KB
- Stars: 7
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.MD
- License: LICENSE
Awesome Lists containing this project
README
# Glock
all lock module and ratelimit using redis for controll.[![Go Reference](https://pkg.go.dev/badge/github.com/princjef/gomarkdoc.svg)](https://pkg.go.dev/github.com/teng231/glock)
## install
``` bash
go get github.com/teng231/glock
```## usage
many structs and methods, not all but i think it's help you for lock.### counter lock
Counter lock: It's distributed counter, counter can run up or run down. If run up, you can hanlder check and it's not atomic before all node.
Counter down or coundown it's good.
Helpful for count turn.``` go
func Run(){
cd, err := StartCountLock(&ConnectConfig{
RedisAddr:"localhost:6379",
RedisPw: "",
Prefix: "test3_",
Timelock: time.Minute,
RedisDb: 1,
})
if err != nil {
log.Print(err)
t.Fail()
}
cd.Start("test1", 100, time.Minute)
cur, _ := cd.DecrBy("test1", 1)
log.Print(cur)
if cur != 99 {
log.Print(cur)
t.Fail()
}
}
```### distributed lock
As You know about this. It's can be lock for all node. Request run one by one, depend on a `key`.
If 2 request going at the same time. Fist request come run and when its done. Second request continue process.``` go
func Run(){
dl, err := StartDistributedLock(&ConnectConfig{
RedisAddr:"localhost:6379",
RedisPw: "",
Prefix: "test3_",
Timelock: time.Minute,
RedisDb: 1,
})
if err != nil {
panic(err)
}
lctx, err := dl.Lock("test-redsync")
if err := dl.Unlock(lctx); err != nil {
panic(err)
}
}
```If many request coming. Distributed lock it not good because many ping request to check redis. Redis can be down then lock system down.
### kmutex
It's local lock like distributed lock but using mutex and waitgroup. You can using it combine with distributed lock.
``` go
func Run(){
km := CreateKmutexInstance()
km.Lock("key")
defer km.Unlock("key")
}
```### limiter
handler limit like counter lock. but have duration
``` go
func Run(){
r, err := StartLimiter(&ConnectConfig{
RedisAddr:"localhost:6379",
RedisPw: "",
Timelock: time.Minute,
RedisDb: 1,
})
if err != nil {
panic(err)
}
if err := r.Allow("key1", Second, 5); err != nil {
log.Print(err)
}
if err := r.Allow("key1", Second, 5); err != nil {
log.Print(err)
}
if err := r.Allow("key1", Second, 5); err != nil {
log.Print(err)
}
if err := r.Allow("key1", Second, 5); err != nil {
log.Print(err)
}
if err := r.Allow("key1", Second, 5); err != nil {
log.Print(err)
}
if err := r.Allow("key1", Second, 5); err != nil {
log.Print(err)
}
time.Sleep(1 * time.Second)
if err := r.Allow("key1", Second, 5); err != nil {
log.Print(err)
}
}
```### optimistic lock
like distributed lock but request 2 come at the same time. It's will return false.
Redis will lower traffics``` go
func Run(){
ol, err := StartOptimisticLock(&ConnectConfig{
RedisAddr:"localhost:6379",
RedisPw: "",
Prefix: "test3_",
Timelock: time.Minute,
RedisDb: 1,
}
)
if err != nil {
panic(err)
}
if err := ol.Lock("key1"); err != nil {
log.Print(err)
}
}
```