2020-04-11 10:48:32 +01:00
|
|
|
// Package sync is an interface for distributed synchronization
|
2019-05-31 00:43:23 +01:00
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
2020-04-11 10:37:54 +01:00
|
|
|
"errors"
|
|
|
|
"time"
|
2019-05-31 00:43:23 +01:00
|
|
|
)
|
|
|
|
|
2020-04-11 10:37:54 +01:00
|
|
|
var (
|
|
|
|
ErrLockTimeout = errors.New("lock timeout")
|
|
|
|
)
|
|
|
|
|
2020-04-11 10:48:32 +01:00
|
|
|
// Sync is an interface for distributed synchronization
|
2020-04-11 10:37:54 +01:00
|
|
|
type Sync interface {
|
|
|
|
// Initialise options
|
|
|
|
Init(...Option) error
|
|
|
|
// Return the options
|
|
|
|
Options() Options
|
|
|
|
// Elect a leader
|
|
|
|
Leader(id string, opts ...LeaderOption) (Leader, error)
|
|
|
|
// Lock acquires a lock
|
|
|
|
Lock(id string, opts ...LockOption) error
|
|
|
|
// Unlock releases a lock
|
|
|
|
Unlock(id string) error
|
|
|
|
// Sync implementation
|
|
|
|
String() string
|
2019-05-31 00:43:23 +01:00
|
|
|
}
|
|
|
|
|
2020-04-11 10:37:54 +01:00
|
|
|
// Leader provides leadership election
|
|
|
|
type Leader interface {
|
2020-04-12 11:16:08 +01:00
|
|
|
// resign leadership
|
|
|
|
Resign() error
|
|
|
|
// status returns when leadership is lost
|
|
|
|
Status() chan bool
|
2019-05-31 00:43:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type Options struct {
|
2020-04-11 10:37:54 +01:00
|
|
|
Nodes []string
|
|
|
|
Prefix string
|
2019-05-31 00:43:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type Option func(o *Options)
|
2020-04-11 10:37:54 +01:00
|
|
|
|
2020-04-12 11:16:08 +01:00
|
|
|
type LeaderOptions struct{}
|
2020-04-11 10:37:54 +01:00
|
|
|
|
|
|
|
type LeaderOption func(o *LeaderOptions)
|
|
|
|
|
|
|
|
type LockOptions struct {
|
|
|
|
TTL time.Duration
|
|
|
|
Wait time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
type LockOption func(o *LockOptions)
|