tlock

package module
v0.0.0-...-449f6a4 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2015 License: MIT Imports: 14 Imported by: 0

README

TLock

A simple tiny centralized lock service, still in development.

Although there are some existing key lock service, like Zookeeper, Etcd, or even Redis, they don't fit my need. E.g, I want to lock multi keys at same time, not one by one. And I want to support hierachical path lock.

If you have some good recommendations, please tell me!

Key Lock

We can lock multi keys using tlock at same time, a simple example:

// shell1

// lock key a, b and c at same time, lock timeout is 30s
// if lock ok, return a lockid for later unlock
// you must do query escape in the real scenario,:-)
POST http://localhost/lock?names=a,b,c&type=key&timeout=30

// do something then unlock
DELETE http://localhost/lock?id=lockid

// shell2
POST http://localhost/lock?names=a,b,c&type=key&timeout=30

return lockid 

DELETE http://localhost/lock?id=lockid

Path Lock

A path lock is for hierachical lock, like a file system lock.

E.g, if we lock path "a/b/c", other can not operate its ancestor like ("a", "a/b") or descendant like ("a/b/c/d", "a/b/c/e"), but can operate its brother like ("a/b/d").

A simple example:

// shell1

// lock path a/b/c, a/b/d at same time, lock timeout is 30s
// if lock ok, return a lockid for later unlock
// you must do query escape in the real scenario,:-)
POST http://localhost/lock?names=a/b/c,a/b/d&type=path&timeout=30

// do something then unlock
DELETE http://localhost/lock?id=lockid

// shell2
POST http://localhost/lock?names=a/b/c,a/b/d&type=path&timeout=30
DELETE http://localhost/lock?id=lockid

RESP Support

tlock supports Redis Serialiazation Protocol(RESP), so you can use any redis client to communicate with tlock, a simple example:

# shell1 redis-cli
redis>LOCK abc TYPE key TIMEOUT 10
redis>lockid
// do something
redis>UNLOCK lockid
redis>OK

# shell2 redis-cli 
redis>LOCK abc TYPE key TIMEOUT 10
// will hang up until shell1 unlock 
redis>lockid
// do something
redis>UNLOCK lockid
redis>OK

You can also use a RESP client for tlock:

import "github.com/siddontang/tlock"

client := NewRESPClient(addr)
locker, _ := client.GetLocker("key", "abc")
locker.Lock()
locker.Unlock()

Documentation

Index

Constants

View Source
const (
	KeyLockType  = "key"
	PathLockType = "path"
)

Variables

View Source
var InfiniteTimeout = 30 * 24 * 3600 * time.Second

Functions

func LockTimeout

func LockTimeout(m sync.Locker, timeout time.Duration) bool

func LockWithTimer

func LockWithTimer(m sync.Locker, timer *time.Timer) bool

Types

type App

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

func NewApp

func NewApp() *App

func (*App) Close

func (a *App) Close()

func (*App) HTTPAddr

func (a *App) HTTPAddr() net.Addr

func (*App) Lock

func (a *App) Lock(tp string, names []string) (uint64, error)

Lock and returns a lock id, you must use this id to unlock

func (*App) LockTimeout

func (a *App) LockTimeout(tp string, timeout time.Duration, names []string) (uint64, error)

Lock with timeout and returns a lock id, you must use this id to unlock

func (*App) RESPAddr

func (a *App) RESPAddr() net.Addr

func (*App) StartHTTP

func (a *App) StartHTTP(addr string) error

func (*App) StartRESP

func (a *App) StartRESP(addr string) error

func (*App) Unlock

func (a *App) Unlock(id uint64) error

type Client

type Client interface {
	GetLocker(tp string, names ...string) (ClientLocker, error)
}

type ClientLocker

type ClientLocker interface {
	Lock() error
	// timeout is seconds
	LockTimeout(timeout int) error
	Unlock() error
}

type KeyLockerGroup

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

func NewKeyLockerGroup

func NewKeyLockerGroup() *KeyLockerGroup

func (*KeyLockerGroup) Lock

func (g *KeyLockerGroup) Lock(keys ...string)

func (*KeyLockerGroup) LockTimeout

func (g *KeyLockerGroup) LockTimeout(timeout time.Duration, keys ...string) bool

func (*KeyLockerGroup) Unlock

func (g *KeyLockerGroup) Unlock(keys ...string)

type LockerGroup

type LockerGroup interface {
	Lock(args ...string)
	LockTimeout(timeout time.Duration, args ...string) bool
	Unlock(args ...string)
}

type PathLockerGroup

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

func NewPathLockerGroup

func NewPathLockerGroup() *PathLockerGroup

func (*PathLockerGroup) Lock

func (g *PathLockerGroup) Lock(paths ...string)

func (*PathLockerGroup) LockTimeout

func (g *PathLockerGroup) LockTimeout(timeout time.Duration, paths ...string) bool

func (*PathLockerGroup) Unlock

func (g *PathLockerGroup) Unlock(paths ...string)

type RESPClient

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

func NewRESPClient

func NewRESPClient(addr string) *RESPClient

func (*RESPClient) Close

func (c *RESPClient) Close()

func (*RESPClient) GetLocker

func (c *RESPClient) GetLocker(tp string, names ...string) (ClientLocker, error)

Directories

Path Synopsis
cmd
tlock command

Jump to

Keyboard shortcuts

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