Documentation ¶
Overview ¶
Example ¶
locker, err := etcdlock.NewLocker(etcdlock.LockerOptions{ Address: "127.0.0.1:2379", DialOptions: []grpc.DialOption{grpc.WithInsecure()}, }) if err != nil { log.Fatalln(err) } // Acquire a lock for a specified recource. if _, err = locker.Lock(context.Background(), "resource_key", 5*time.Second); err != nil { log.Fatalln(err) } // This lock will be acquired after 5s, and before that current goroutine // will be blocked. anotherLock, err := locker.Lock(context.Background(), "resource_key", 3*time.Second) if err != nil { log.Fatalln(err) } // Unlock the lock manually. if err := anotherLock.Unlock(context.Background()); err != nil { log.Fatalln(err) }
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var (
ErrEmptyKey = errors.New("empty key")
)
Exposed errors.
Functions ¶
This section is empty.
Types ¶
type Lock ¶
type Lock struct {
// contains filtered or unexported fields
}
Lock is a distributed lock of specified resource which was acquired from etcd v3.
type Locker ¶
type Locker struct {
// contains filtered or unexported fields
}
Locker is the client for acquiring distributed locks from etcd. It should be created from NewLocker() function.
func NewLocker ¶
func NewLocker(options LockerOptions) (*Locker, error)
NewLocker creates a Locker according to the given options.
func (*Locker) Lock ¶
Lock acquires a distributed lock for the specified resource from etcd v3.
Example ¶
locker, err := etcdlock.NewLocker(etcdlock.LockerOptions{ Address: "127.0.0.1:2379", DialOptions: []grpc.DialOption{grpc.WithInsecure()}, }) if err != nil { log.Fatalln(err) } // This lock will be expired in 3 seconds. if _, err := locker.Lock(context.Background(), "resource_key", 3*time.Second); err != nil { log.Fatalln(err) }
Output:
type LockerOptions ¶
type LockerOptions struct { // The address of etcd(v3) server. Address string // Options used for `grpc.Dial`. DialOptions []grpc.DialOption // Prefix of the keys of locks in etcd, by default is "__etcd_lock/" EtcdKeyPrefix string }
LockerOptions is the options for NewLocker() function.
Click to show internal directories.
Click to hide internal directories.