1
0
mirror of https://github.com/go-micro/go-micro.git synced 2024-12-18 08:26:38 +02:00
go-micro/sync/sync.go

61 lines
1.1 KiB
Go
Raw Normal View History

2020-04-11 11:48:32 +02:00
// Package sync is an interface for distributed synchronization
2019-05-31 01:43:23 +02:00
package sync
import (
"context"
"crypto/tls"
2020-04-11 11:37:54 +02:00
"errors"
"time"
"go-micro.dev/v4/logger"
2019-05-31 01:43:23 +02:00
)
2020-04-11 11:37:54 +02:00
var (
ErrLockTimeout = errors.New("lock timeout")
)
2022-09-30 16:27:07 +02:00
// Sync is an interface for distributed synchronization.
2020-04-11 11:37:54 +02:00
type Sync interface {
2022-09-30 16:27:07 +02:00
// Initialize options
2020-04-11 11:37:54 +02:00
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 01:43:23 +02:00
}
2022-09-30 16:27:07 +02:00
// Leader provides leadership election.
2020-04-11 11:37:54 +02:00
type Leader interface {
2020-04-12 12:16:08 +02:00
// resign leadership
Resign() error
// status returns when leadership is lost
Status() chan bool
2019-05-31 01:43:23 +02:00
}
type Options struct {
Context context.Context
Logger logger.Logger
2023-04-26 02:16:34 +02:00
TLSConfig *tls.Config
Prefix string
Nodes []string
2019-05-31 01:43:23 +02:00
}
type Option func(o *Options)
2020-04-11 11:37:54 +02:00
2020-04-12 12:16:08 +02:00
type LeaderOptions struct{}
2020-04-11 11:37:54 +02:00
type LeaderOption func(o *LeaderOptions)
type LockOptions struct {
TTL time.Duration
Wait time.Duration
}
type LockOption func(o *LockOptions)