lock

package
v0.0.0-...-012d1c6 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2021 License: Apache-2.0 Imports: 5 Imported by: 1

Documentation

Index

Constants

View Source
const (
	DefaultTTL = time.Minute
	TimeoutMax = -1
)

Variables

View Source
var ErrNotLock = errors.New("not lock")

Functions

func SetDefault

func SetDefault(l Locking)

Types

type Locker

type Locker interface {
	Lock() error
	Unlock()
}

Locker 类似于sync.Locker,但是Lock会返回错误

func Lock

func Lock(key string, opts ...Option) (Locker, error)

New and Lock l, err := Lock("test_key")

if err != nil {
  return
}

defer l.Unlock()

func New

func New(key string, opts ...Option) (Locker, error)

New 使用默认的locking创建一个locker

type Locking

type Locking interface {
	Name() string
	Acquire(key string, opts *Options) (Locker, error)
}

Locking 分布式锁系统,distributed locking system 可以基于redis实现,也可以基于consul,etcd等实现 可选参数: TTL:通常不需要设置,用于指示业务过期时间,防止服务器宕机永远无法释放锁 Timeout:用于指示Lock等待超时时间,0立即返回,-1表示永久等待,直到获取到锁

注意:因为目标是分布式锁,为了防止永久死锁,必须要设置一个过期,插件库来保证自动续期 TODO:如何抽象乐观锁?

github.com/go-redsync/redsync

func GetDefault

func GetDefault() Locking

type Option

type Option func(o *Options)

func Blocking

func Blocking() Option

wait until get lock

func TTL

func TTL(t time.Duration) Option

TTL set the lock ttl

func Timeout

func Timeout(t time.Duration) Option

Timeout set the lock wait timeout

type Options

type Options struct {
	TTL     time.Duration // 表示宕机后最长TTL时间后可以重新获得锁,<=0则使用默认值
	Timeout time.Duration // Lock等待超时时间,0则不等待立即返回,TimeoutMax则表示永不超时,直到获取到锁
}

func (*Options) Build

func (o *Options) Build(opts ...Option)

Jump to

Keyboard shortcuts

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