2018-11-10 20:05:44 +00:00
|
|
|
// Package etcd3locker provides a locking mechanism using an etcd3 cluster
|
|
|
|
//
|
|
|
|
// To initialize a locker, a pre-existing connected etcd3 client must be present
|
|
|
|
//
|
2018-11-10 23:33:06 +00:00
|
|
|
// client, err := clientv3.New(clientv3.Config{
|
|
|
|
// Endpoints: []string{harness.Endpoint},
|
|
|
|
// DialTimeout: 5 * time.Second,
|
|
|
|
// })
|
2018-11-10 20:05:44 +00:00
|
|
|
//
|
2018-11-10 23:33:06 +00:00
|
|
|
// For the most basic locker (e.g. non-shared etcd3 cluster / use default TTLs),
|
|
|
|
// a locker can be instantiated like the following:
|
2018-11-10 20:05:44 +00:00
|
|
|
//
|
2018-11-10 23:33:06 +00:00
|
|
|
// locker, err := etcd3locker.New(client)
|
|
|
|
// if err != nil {
|
|
|
|
// return nil, fmt.Errorf("Failed to create etcd locker: %v", err.Error())
|
|
|
|
// }
|
2018-11-10 20:05:44 +00:00
|
|
|
//
|
2018-11-10 23:33:06 +00:00
|
|
|
// The locker will need to be included in composer that is used by tusd:
|
2018-11-10 20:05:44 +00:00
|
|
|
//
|
2019-06-11 16:23:20 +00:00
|
|
|
// composer := handler.NewStoreComposer()
|
2018-11-10 23:33:06 +00:00
|
|
|
// locker.UseIn(composer)
|
2018-11-10 20:05:44 +00:00
|
|
|
//
|
2018-11-10 23:33:06 +00:00
|
|
|
// For a shared etcd3 cluster, you may want to modify the prefix that etcd3locker uses:
|
2018-11-10 20:05:44 +00:00
|
|
|
//
|
2018-11-10 23:33:06 +00:00
|
|
|
// locker, err := etcd3locker.NewWithPrefix(client, "my-prefix")
|
|
|
|
// if err != nil {
|
|
|
|
// return nil, fmt.Errorf("Failed to create etcd locker: %v", err.Error())
|
|
|
|
// }
|
2018-11-10 20:05:44 +00:00
|
|
|
//
|
|
|
|
//
|
2018-11-10 23:33:06 +00:00
|
|
|
// For full control over all options, an etcd3.LockerOptions may be passed into
|
|
|
|
// etcd3.NewWithLockerOptions like the following example:
|
2018-11-10 20:05:44 +00:00
|
|
|
//
|
2018-11-10 23:33:06 +00:00
|
|
|
// 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())
|
|
|
|
// }
|
2018-11-10 20:05:44 +00:00
|
|
|
//
|
|
|
|
// Tested on etcd 3.1/3.2/3.3
|
|
|
|
//
|
|
|
|
package etcd3locker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"time"
|
|
|
|
|
2019-06-16 15:06:17 +00:00
|
|
|
etcd3 "github.com/coreos/etcd/clientv3"
|
|
|
|
"github.com/coreos/etcd/clientv3/concurrency"
|
2019-09-11 10:03:39 +00:00
|
|
|
"github.com/tus/tusd/pkg/handler"
|
2018-11-10 20:05:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrLockNotHeld = errors.New("Lock not held")
|
|
|
|
GrantTimeout = 1500 * time.Millisecond
|
|
|
|
)
|
|
|
|
|
|
|
|
type Etcd3Locker struct {
|
|
|
|
// etcd3 client session
|
|
|
|
Client *etcd3.Client
|
|
|
|
|
2018-11-10 23:33:06 +00:00
|
|
|
prefix string
|
|
|
|
sessionTtl int
|
2018-11-10 20:05:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// New constructs a new locker using the provided client.
|
|
|
|
func New(client *etcd3.Client) (*Etcd3Locker, error) {
|
|
|
|
return NewWithLockerOptions(client, DefaultLockerOptions())
|
|
|
|
}
|
|
|
|
|
|
|
|
// This method may be used if a different prefix is required for multi-tenant etcd clusters
|
|
|
|
func NewWithPrefix(client *etcd3.Client, prefix string) (*Etcd3Locker, error) {
|
|
|
|
lockerOptions := DefaultLockerOptions()
|
|
|
|
lockerOptions.SetPrefix(prefix)
|
|
|
|
return NewWithLockerOptions(client, lockerOptions)
|
|
|
|
}
|
|
|
|
|
|
|
|
// This method may be used if we want control over both prefix/session TTLs. This is used for testing in particular.
|
|
|
|
func NewWithLockerOptions(client *etcd3.Client, opts LockerOptions) (*Etcd3Locker, error) {
|
2019-09-11 10:03:39 +00:00
|
|
|
return &Etcd3Locker{Client: client, prefix: opts.Prefix(), sessionTtl: opts.Ttl()}, nil
|
2018-11-10 20:05:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UseIn adds this locker to the passed composer.
|
2019-06-11 16:23:20 +00:00
|
|
|
func (locker *Etcd3Locker) UseIn(composer *handler.StoreComposer) {
|
2019-09-12 10:37:43 +00:00
|
|
|
composer.UseLocker(locker)
|
2018-11-10 20:05:44 +00:00
|
|
|
}
|
|
|
|
|
2019-09-11 10:03:39 +00:00
|
|
|
func (locker *Etcd3Locker) NewLock(id string) (handler.Lock, error) {
|
2018-11-10 20:05:44 +00:00
|
|
|
session, err := locker.createSession()
|
|
|
|
if err != nil {
|
2019-09-11 10:03:39 +00:00
|
|
|
return nil, err
|
2018-11-10 20:05:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
lock := newEtcd3Lock(session, locker.getId(id))
|
|
|
|
|
2019-09-11 10:03:39 +00:00
|
|
|
return lock, nil
|
2018-11-10 20:05:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (locker *Etcd3Locker) createSession() (*concurrency.Session, error) {
|
2018-11-10 23:33:06 +00:00
|
|
|
return concurrency.NewSession(locker.Client, concurrency.WithTTL(locker.sessionTtl))
|
2018-11-10 20:05:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (locker *Etcd3Locker) getId(id string) string {
|
|
|
|
return locker.prefix + id
|
|
|
|
}
|