{"id":15678263,"url":"https://github.com/jrhouston/k8slock","last_synced_at":"2025-05-07T02:28:53.362Z","repository":{"id":108778402,"uuid":"270191239","full_name":"jrhouston/k8slock","owner":"jrhouston","description":"A distributed locking module for Go using the Lease resource from the Kubernetes coordination API","archived":false,"fork":false,"pushed_at":"2024-04-14T23:14:53.000Z","size":46,"stargazers_count":26,"open_issues_count":3,"forks_count":6,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-05-07T02:28:38.039Z","etag":null,"topics":["coordination","distributed-lock","distributed-systems","kubernetes","locking"],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jrhouston.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2020-06-07T04:25:39.000Z","updated_at":"2025-04-01T09:27:01.000Z","dependencies_parsed_at":"2024-04-15T00:22:50.047Z","dependency_job_id":"d566eb6d-0e59-4e79-9405-1468f363d1c0","html_url":"https://github.com/jrhouston/k8slock","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jrhouston%2Fk8slock","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jrhouston%2Fk8slock/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jrhouston%2Fk8slock/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jrhouston%2Fk8slock/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jrhouston","download_url":"https://codeload.github.com/jrhouston/k8slock/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252801233,"owners_count":21806279,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["coordination","distributed-lock","distributed-systems","kubernetes","locking"],"created_at":"2024-10-03T16:19:20.294Z","updated_at":"2025-05-07T02:28:53.326Z","avatar_url":"https://github.com/jrhouston.png","language":"Go","readme":"# k8slock [![Godoc](https://godoc.org/github.com/jrhouston/k8slock?status.svg)](https://godoc.org/github.com/jrhouston/k8slock) [![Go Report Card](https://goreportcard.com/badge/github.com/jrhouston/k8slock)](https://goreportcard.com/report/github.com/jrhouston/k8slock)\n\nk8slock 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. \n\nIf you want to use Kubernetes to create a simple distributed lock, this module is for you.\n\nThis module implements the [sync.Locker](https://golang.org/pkg/sync/#Locker) interface using the `Lock()` and `Unlock()` functions.\n\nThis module also supports using contexts via the `LockContext()` and `UnlockContext()` functions. \n\n\n## Basic Usage\n\n```go\npackage main\n\nimport \"github.com/jrhouston/k8slock\"\n\nfunc main() {\n    locker, err := k8slock.NewLocker(\"example-lock\")\n    if err != nil {\n        panic(err)\n    }\n\n    locker.Lock()\n    // do some work\n    locker.Unlock()\n}\n```\n\n## Basic Usage – Context\n\n```go\npackage main\n\nimport (\n    \"context\"\n    \"github.com/jrhouston/k8slock\"\n)\n\nfunc main() {\n    locker, err := k8slock.NewLocker(\"example-lock\")\n    if err != nil {\n        panic(err)\n    }\n\n    ctx := context.Background()\n    if err := locker.LockContext(ctx); err != nil {\n        fmt.Println(\"Error trying to lock\", err)\n    }\n\n    // do some work\n    \n    if err := locker.UnlockContext(ctx); err != nil {\n        fmt.Println(\"Error trying to unlock\", err)\n    }\n}\n```\n\n# Locker Options\n\nThe locker can be configured using the following [functional options](https://dave.cheney.net/2014/10/17/functional-options-for-friendly-apis):\n\n| Option | Details |\n|---|---|\n| `TTL(duration)` | The duration until the lock expires and can be forcibly claimed. By default the lock can be held infinitely. |\n| `RetryWaitDuration(duration)` | The duration to wait before retrying after failing to acquired the lock. Default: 1 second. |\n| `InClusterConfig()` | Get the kubernetes client config from inside a pod. Defaults to a clientset using the local kubeconfig. |\n| `Clientset(kubernetes.Interface)` | Configure a custom Kubernetes Clientset. Defaults to a clientset using the local kubeconfig. |\n| `Namespace(string)` | The kubernetes namespace to store the Lease resource. Defaults to \"default\". |\n| `ClientID(string)` | A unique ID for the client that is trying to obtain the lock. Defaults to a random UUID. |\n| `CreateLease(bool)` | Create a Lease resource if it does not already exist. Defaults to `true`. |\n\ne.g:\n\n```go\nlocker, err := k8slock.NewLocker(\"example-lock\", k8slock.Namespace(\"locks\"), k8slock.ClientID(\"client-0\"))\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjrhouston%2Fk8slock","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjrhouston%2Fk8slock","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjrhouston%2Fk8slock/lists"}