lock

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 8, 2024 License: CC0-1.0 Imports: 6 Imported by: 0

README

lock

Distributed Lock package, implementation

  • local
  • redis
  • etcd
  • zookeeper

Usage

import (
    "github.com/diki-haryadi/govega/lock"
    _ "github.com/diki-haryadi/govega/lock/etcd"
    _ "github.com/diki-haryadi/govega/lock/redis"
    _ "github.com/diki-haryadi/govega/lock/zk"
)

url := "local://"
// url := "redis://localhost:6379/test"
// url := "redis://u1:p1@127.0.0.1:6379,u2:p2@127.0.0.2:6379,u3:p3@127.0.0.3:6379/test"
// url := "etcd://127.0.0.1:2379,127.0.0.2:2379,127.0.0.3:2379/test"
// url := "zk://127.0.0.1:2181,127.0.0.2:2181,127.0.0.3:2181/test"

dlock, _ := lock.New(url)

// Lock and wait
if err := dlock.Lock(ctx, id, 20); err != nil {
    return err
}

// Try lock and return immediately
if err := dlock.TryLock(ctx, id, 20); err != nil {
    continue
}

// Unlock
if err := dlock.Unlock(ctx, id); err != nil {
    return err
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrResourceLocked = errors.New("resource locked")
)

Functions

func Register

func Register(schema string, f InitFunc)

Register register cache implementation

Types

type DLocker

type DLocker interface {
	TryLock(ctx context.Context, id string, ttl int) error
	Lock(ctx context.Context, id string, ttl int) error
	Unlock(ctx context.Context, id string) error
	Close() error
}

DLocker distributed locker interface

func Local

func Local() (DLocker, error)

New create redis locker instance

func New

func New(urlStr string) (DLocker, error)

New create new cache

type InitFunc

type InitFunc func(urls []*url.URL) (DLocker, error)

InitFunc cache init function

type LockManager

type LockManager struct {
	// contains filtered or unexported fields
}

LockManager local lock manager

func (LockManager) Close

func (l LockManager) Close() error

Close close the lock

func (*LockManager) Lock

func (l *LockManager) Lock(ctx context.Context, id string, ttl int) error

Lock try to lock and wait until resource is available to lock

func (*LockManager) TryLock

func (l *LockManager) TryLock(ctx context.Context, id string, ttl int) error

TryLock try to lock, and return immediately if resource already locked

func (*LockManager) Unlock

func (l *LockManager) Unlock(ctx context.Context, id string) error

Unlock unlock resource

Directories

Path Synopsis
pkg

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL