lock

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2021 License: MIT Imports: 9 Imported by: 1

README

nlock

A simple but powerful distributed lock

Get Started

Download
go get github.com/inuggets/nlock
Usage
Redis lock
import lock github.com/inuggets/nlock

redisPool := lock.NewRedisPool("localhost:6379", "PASSWORD")
key := "test"
l := lock.NewRedisLock(redisPool, 15) // ttl is 15s
if l.Lock(key) {
    defer l.Unlock(key)
    if l.HasLock(key) { // You can check if has lock
        ...
    }
    
    // Do something
    ...
}

Lock with waiting time
l := lock.NewRedisLock(redisPool, 15) // ttl is 15s
if lock.LockWait(key, 3) { // Wait up to 3s if key is locked but others
    defer l.Unlock(key)
}

Methods
- Lock(key string) bool                             // lock
- LockWait(key string, seconds int64) bool          // lock with waiting time
- Locked(key string) bool                           // check if locked
- HasLock(key string) bool                          // check if has/own lock
- Unlock(key string) bool                           // unlock
MySQL lock
import lock github.com/inuggets/nlock

db, _ := lock.NewMysqlClient("localhost:3306", "DB", "USER", "PASSWORD")
l := lock.NewMysqlLock(db, 15)
key := "test"
l := lock.NewRedisLock(redisPool, 15) // ttl is 15s
if l.Lock(key) {
    defer l.Unlock(key)
}

License

nlock is under the MIT license. See the LICENSE for detail.

Documentation

Index

Constants

View Source
const (
	Prefix = "nlock:"
)

Variables

This section is empty.

Functions

func ID

func ID() string

func NewMysqlClient

func NewMysqlClient(host, database, username, password string) (*gorm.DB, error)

func NewRedisPool

func NewRedisPool(url, password string) *redis.Pool

Types

type Lock

type Lock interface {
	Lock(key string) bool
	LockWait(key string, seconds int64) bool
	Locked(key string) bool
	HasLock(key string) bool
	Unlock(key string) bool
}

func NewMysqlLock

func NewMysqlLock(db *gorm.DB, ttl int, tableName ...string) Lock

func NewRedisLock

func NewRedisLock(pool *redis.Pool, ttl int) Lock

type MysqlLock

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

func (*MysqlLock) HasLock

func (l *MysqlLock) HasLock(key string) bool

func (*MysqlLock) Lock

func (l *MysqlLock) Lock(key string) bool

func (*MysqlLock) LockWait

func (l *MysqlLock) LockWait(key string, seconds int64) bool

func (*MysqlLock) Locked

func (l *MysqlLock) Locked(key string) bool

func (*MysqlLock) Unlock

func (l *MysqlLock) Unlock(key string) bool

type RedisLock

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

func (*RedisLock) HasLock

func (l *RedisLock) HasLock(key string) bool

func (*RedisLock) Lock

func (l *RedisLock) Lock(key string) bool

func (*RedisLock) LockWait

func (l *RedisLock) LockWait(key string, seconds int64) bool

func (*RedisLock) Locked

func (l *RedisLock) Locked(key string) bool

func (*RedisLock) Unlock

func (l *RedisLock) Unlock(key string) bool

Jump to

Keyboard shortcuts

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