Coder Social home page Coder Social logo

jrhouston / k8slock Goto Github PK

View Code? Open in Web Editor NEW
23.0 2.0 6.0 46 KB

A distributed locking module for Go using the Lease resource from the Kubernetes coordination API

License: MIT License

Go 100.00%
kubernetes locking distributed-lock coordination distributed-systems

k8slock's Introduction

k8slock Godoc Go Report Card

k8slock is a Go module that makes it easy to do distributed locking using the Lease 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 interface using the Lock() and Unlock() functions.

This module also supports using contexts via the LockContext() and UnlockContext() functions.

Basic Usage

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

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:

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:

locker, err := k8slock.NewLocker("example-lock", k8slock.Namespace("locks"), k8slock.ClientID("client-0"))

k8slock's People

Contributors

avorima avatar guettli avatar holyspectral avatar jrhouston avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

k8slock's Issues

Add non-blocking support

This package implements the sync.Locker interface, which blocks until the Lock is acquired. Some clients might prefer a non-blocking interface so they can control the strategy used to retry acquiring the lock.

Copy and paste error in `lock()`?

k8slock/locker.go

Lines 187 to 192 in 6f294b1

lease.Spec.HolderIdentity = pointer.String(l.clientID)
if lease.Spec.LeaseTransitions != nil {
lease.Spec.LeaseTransitions = pointer.Int32((*lease.Spec.LeaseTransitions) + 1)
} else {
lease.Spec.LeaseTransitions = pointer.Int32((*lease.Spec.LeaseTransitions) + 1)
}

Here, the code checks if lease.Spec.LeaseTransitions is nil but the if-else branches are identical.

pointer.Int32((*lease.Spec.LeaseTransitions) + 1)

will segfault with a nil lease.Spec.LeaseTransitions

I see a lot of panic()

First I thought: Great, a nice library which helps me to sync two controllers.

Then I looked at the code and see many panic() calls.

Just out of curiosity, why do you use panic() so often?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.