etcd3locker

package
v1.0.8 Latest Latest
Warning

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

Go to latest
Published: Jun 21, 2019 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package etcd3locker provides a locking mechanism using an etcd3 cluster. Tested on etcd 3.1/3.2./3.3

Package etcd3locker provides a locking mechanism using an etcd3 cluster

To initialize a locker, a pre-existing connected etcd3 client must be present

client, err := clientv3.New(clientv3.Config{
	Endpoints:   []string{harness.Endpoint},
	DialTimeout: 5 * time.Second,
})

For the most basic locker (e.g. non-shared etcd3 cluster / use default TTLs), a locker can be instantiated like the following:

locker, err := etcd3locker.New(client)
if err != nil {
	return nil, fmt.Errorf("Failed to create etcd locker: %v", err.Error())
}

The locker will need to be included in composer that is used by tusd:

composer := tusd.NewStoreComposer()
locker.UseIn(composer)

For a shared etcd3 cluster, you may want to modify the prefix that etcd3locker uses:

locker, err := etcd3locker.NewWithPrefix(client, "my-prefix")
if err != nil {
	return nil, fmt.Errorf("Failed to create etcd locker: %v", err.Error())
}

For full control over all options, an etcd3.LockerOptions may be passed into etcd3.NewWithLockerOptions like the following example:

ttl := 15 // seconds
options := etcd3locker.NewLockerOptions(ttl, "my-prefix")
locker, err := etcd3locker.NewWithLockerOptions(client, options)
if err != nil {
	return nil, fmt.Errorf("Failed to create etcd locker: %v", err.Error())
}

Tested on etcd 3.1/3.2/3.3

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrLockNotHeld = errors.New("Lock not held")
	GrantTimeout   = 1500 * time.Millisecond
)
View Source
var (
	DefaultTtl    = 60
	DefaultPrefix = "/tusd"
)

Functions

This section is empty.

Types

type Etcd3Locker

type Etcd3Locker struct {
	// etcd3 client session
	Client *etcd3.Client
	// contains filtered or unexported fields
}

func New

func New(client *etcd3.Client) (*Etcd3Locker, error)

New constructs a new locker using the provided client.

func NewWithLockerOptions

func NewWithLockerOptions(client *etcd3.Client, opts LockerOptions) (*Etcd3Locker, error)

This method may be used if we want control over both prefix/session TTLs. This is used for testing in particular.

func NewWithPrefix

func NewWithPrefix(client *etcd3.Client, prefix string) (*Etcd3Locker, error)

This method may be used if a different prefix is required for multi-tenant etcd clusters

func (*Etcd3Locker) LockUpload

func (locker *Etcd3Locker) LockUpload(id string) error

LockUpload tries to obtain the exclusive lock.

func (*Etcd3Locker) UnlockUpload

func (locker *Etcd3Locker) UnlockUpload(id string) error

UnlockUpload releases a lock.

func (*Etcd3Locker) UseIn

func (locker *Etcd3Locker) UseIn(composer *tusd.StoreComposer)

UseIn adds this locker to the passed composer.

type LockerOptions

type LockerOptions struct {
	// contains filtered or unexported fields
}

func DefaultLockerOptions

func DefaultLockerOptions() LockerOptions

DefaultLockerOptions() instantiates an instance of LockerOptions with default 60 second time to live and an etcd3 prefix of "/tusd"

func NewLockerOptions

func NewLockerOptions(ttl int, prefix string) LockerOptions

NewLockerOptions instantiates an instance of LockerOptions with a provided TTL (time to live) and string prefix for keys to be stored in etcd3

func (*LockerOptions) Prefix

func (l *LockerOptions) Prefix() string

Returns the string prefix used to store keys in etcd3

func (*LockerOptions) SetPrefix

func (l *LockerOptions) SetPrefix(prefix string)

Set string prefix to be used in keys stored into etcd3 by the locker

func (*LockerOptions) SetTtl

func (l *LockerOptions) SetTtl(ttl int)

Set etcd3 session TTL (time to live)

func (*LockerOptions) Ttl

func (l *LockerOptions) Ttl() int

Returns the TTL (time to live) of sessions in etcd3

Jump to

Keyboard shortcuts

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