Documentation ¶
Overview ¶
Package concurrency implements concurrency operations on top of etcd such as distributed locks, barriers, and elections.
Index ¶
- Variables
- func NewLocker(s *Session, pfx string) sync.Locker
- func NewSTM(c *v3.Client, apply func(STM) error, so ...stmOption) (*v3.TxnResponse, error)
- func WithAbortContext(ctx context.Context) stmOption
- func WithIsolation(lvl Isolation) stmOption
- func WithPrefetch(keys ...string) stmOption
- type Election
- func (e *Election) Campaign(ctx context.Context, val string) error
- func (e *Election) Key() string
- func (e *Election) Leader(ctx context.Context) (string, error)
- func (e *Election) Observe(ctx context.Context) <-chan v3.GetResponse
- func (e *Election) Proclaim(ctx context.Context, val string) error
- func (e *Election) Resign(ctx context.Context) (err error)
- type Isolation
- type Mutex
- type STM
- type Session
- type SessionOption
Constants ¶
This section is empty.
Variables ¶
var ( ErrElectionNotLeader = errors.New("election: not leader") ErrElectionNoLeader = errors.New("election: no leader") )
Functions ¶
func WithAbortContext ¶
WithAbortContext specifies the context for permanently aborting the transaction.
func WithIsolation ¶
func WithIsolation(lvl Isolation) stmOption
WithIsolation specifies the transaction isolation level.
func WithPrefetch ¶
func WithPrefetch(keys ...string) stmOption
WithPrefetch is a hint to prefetch a list of keys before trying to apply. If an STM transaction will unconditionally fetch a set of keys, prefetching those keys will save the round-trip cost from requesting each key one by one with Get().
Types ¶
type Election ¶
type Election struct {
// contains filtered or unexported fields
}
func NewElection ¶
NewElection returns a new election on a given key prefix.
func (*Election) Campaign ¶
Campaign puts a value as eligible for the election. It blocks until it is elected, an error occurs, or the context is cancelled.
func (*Election) Observe ¶
func (e *Election) Observe(ctx context.Context) <-chan v3.GetResponse
Observe returns a channel that observes all leader proposal values as GetResponse values on the current leader key. The channel closes when the context is cancelled or the underlying watcher is otherwise disrupted.
type Isolation ¶
type Isolation int
Isolation is an enumeration of transactional isolation levels which describes how transactions should interfere and conflict.
const ( // Snapshot is serializable but also checks writes for conflicts. Snapshot Isolation = iota // Serializable reads within the same transactiona attempt return data // from the at the revision of the first read. Serializable // RepeatableReads reads within the same transaction attempt always // return the same data. RepeatableReads // ReadCommitted reads keys from any committed revision. ReadCommitted )
type Mutex ¶
type Mutex struct {
// contains filtered or unexported fields
}
Mutex implements the sync Locker interface with etcd
type STM ¶
type STM interface { // Get returns the value for a key and inserts the key in the txn's read set. // If Get fails, it aborts the transaction with an error, never returning. Get(key ...string) string // Put adds a value for a key to the write set. Put(key, val string, opts ...v3.OpOption) // Rev returns the revision of a key in the read set. Rev(key string) int64 // Del deletes a key. Del(key string) // contains filtered or unexported methods }
STM is an interface for software transactional memory.
type Session ¶
type Session struct {
// contains filtered or unexported fields
}
Session represents a lease kept alive for the lifetime of a client. Fault-tolerant applications may use sessions to reason about liveness.
func NewSession ¶
func NewSession(client *v3.Client, opts ...SessionOption) (*Session, error)
NewSession gets the leased session for a client.
func (*Session) Done ¶
func (s *Session) Done() <-chan struct{}
Done returns a channel that closes when the lease is orphaned, expires, or is otherwise no longer being refreshed.
type SessionOption ¶
type SessionOption func(*sessionOptions)
SessionOption configures Session.
func WithContext ¶
func WithContext(ctx context.Context) SessionOption
WithContext assigns a context to the session instead of defaulting to using the client context. This is useful for canceling NewSession and Close operations immediately without having to close the client. If the context is canceled before Close() completes, the session's lease will be abandoned and left to expire instead of being revoked.
func WithLease ¶
func WithLease(leaseID v3.LeaseID) SessionOption
WithLease specifies the existing leaseID to be used for the session. This is useful in process restart scenario, for example, to reclaim leadership from an election prior to restart.
func WithTTL ¶
func WithTTL(ttl int) SessionOption
WithTTL configures the session's TTL in seconds. If TTL is <= 0, the default 60 seconds TTL will be used.