https://github.com/nfangxu/ilocker
Golang locker interface
https://github.com/nfangxu/ilocker
go golang lock locker locking locks
Last synced: 9 months ago
JSON representation
Golang locker interface
- Host: GitHub
- URL: https://github.com/nfangxu/ilocker
- Owner: nfangxu
- Created: 2022-03-28T08:56:15.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2022-04-24T04:05:29.000Z (about 4 years ago)
- Last Synced: 2025-06-12T00:41:00.121Z (11 months ago)
- Topics: go, golang, lock, locker, locking, locks
- Language: Go
- Homepage:
- Size: 5.86 KB
- Stars: 2
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
### Golang locker interface
- Use the built-in memory locker
```go
package main
import (
"context"
"github.com/nfangxu/ilocker"
"time"
)
func main() {
locker := ilocker.NewMemoryLocker(time.Minute)
ld, err := locker.Lock(context.TODO(), "foo", time.Second)
if err != nil {
panic(err)
}
defer ld.UnLock(context.TODO())
// TODO: do something
}
```
- Custom locker
```go
package main
import (
"context"
"github.com/nfangxu/ilocker"
"time"
)
type RedisLocker struct {
}
func (r *RedisLocker) Lock(ctx context.Context, id string, ttl time.Duration) (ilocker.ILocked, error) {
// TODO: implement
return ilocker.Locked(r, id) // return a locked object
}
func (r *RedisLocker) Locking(ctx context.Context, id string) bool {
// TODO: implement
return false
}
func (r *RedisLocker) UnLock(ctx context.Context, id string) error {
// TODO: implement
return nil
}
```