https://github.com/jrhouston/k8slock
A distributed locking module for Go using the Lease resource from the Kubernetes coordination API
https://github.com/jrhouston/k8slock
coordination distributed-lock distributed-systems kubernetes locking
Last synced: 12 months ago
JSON representation
A distributed locking module for Go using the Lease resource from the Kubernetes coordination API
- Host: GitHub
- URL: https://github.com/jrhouston/k8slock
- Owner: jrhouston
- License: mit
- Created: 2020-06-07T04:25:39.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2024-04-14T23:14:53.000Z (about 2 years ago)
- Last Synced: 2025-05-07T02:28:38.039Z (12 months ago)
- Topics: coordination, distributed-lock, distributed-systems, kubernetes, locking
- Language: Go
- Size: 44.9 KB
- Stars: 26
- Watchers: 1
- Forks: 6
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# k8slock [](https://godoc.org/github.com/jrhouston/k8slock) [](https://goreportcard.com/report/github.com/jrhouston/k8slock)
k8slock is a Go module that makes it easy to do distributed locking using the [Lease](https://kubernetes.io/docs/reference/kubernetes-api/cluster-resources/lease-v1/) resource from the Kubernetes coordination API.
If you want to use Kubernetes to create a simple distributed lock, this module is for you.
This module implements the [sync.Locker](https://golang.org/pkg/sync/#Locker) interface using the `Lock()` and `Unlock()` functions.
This module also supports using contexts via the `LockContext()` and `UnlockContext()` functions.
## Basic Usage
```go
package main
import "github.com/jrhouston/k8slock"
func main() {
locker, err := k8slock.NewLocker("example-lock")
if err != nil {
panic(err)
}
locker.Lock()
// do some work
locker.Unlock()
}
```
## Basic Usage – Context
```go
package main
import (
"context"
"github.com/jrhouston/k8slock"
)
func main() {
locker, err := k8slock.NewLocker("example-lock")
if err != nil {
panic(err)
}
ctx := context.Background()
if err := locker.LockContext(ctx); err != nil {
fmt.Println("Error trying to lock", err)
}
// do some work
if err := locker.UnlockContext(ctx); err != nil {
fmt.Println("Error trying to unlock", err)
}
}
```
# Locker Options
The locker can be configured using the following [functional options](https://dave.cheney.net/2014/10/17/functional-options-for-friendly-apis):
| Option | Details |
|---|---|
| `TTL(duration)` | The duration until the lock expires and can be forcibly claimed. By default the lock can be held infinitely. |
| `RetryWaitDuration(duration)` | The duration to wait before retrying after failing to acquired the lock. Default: 1 second. |
| `InClusterConfig()` | Get the kubernetes client config from inside a pod. Defaults to a clientset using the local kubeconfig. |
| `Clientset(kubernetes.Interface)` | Configure a custom Kubernetes Clientset. Defaults to a clientset using the local kubeconfig. |
| `Namespace(string)` | The kubernetes namespace to store the Lease resource. Defaults to "default". |
| `ClientID(string)` | A unique ID for the client that is trying to obtain the lock. Defaults to a random UUID. |
| `CreateLease(bool)` | Create a Lease resource if it does not already exist. Defaults to `true`. |
e.g:
```go
locker, err := k8slock.NewLocker("example-lock", k8slock.Namespace("locks"), k8slock.ClientID("client-0"))
```