etcd3locker: Update formatting of docs; timeout -> ttl substitution (#216)

* Update formatting of docs; timeout -> ttl substitution

* Add link to etcd3 locker in main README.md
This commit is contained in:
Anders Chen 2018-11-11 00:33:06 +01:00 committed by Marius
parent 3549a75e8c
commit a4a6ef5a1c
4 changed files with 58 additions and 45 deletions

View File

@ -219,6 +219,7 @@ useful tools:
* [**gcsstore**](https://godoc.org/github.com/tus/tusd/gcsstore): A storage backend using Google cloud storage
* [**memorylocker**](https://godoc.org/github.com/tus/tusd/memorylocker): An in-memory locker for handling concurrent uploads
* [**consullocker**](https://godoc.org/github.com/tus/tusd/consullocker): A locker using the distributed Consul service
* [**etcd3locker**](https://godoc.org/github.com/tus/tusd/etcd3locker): A locker using the distributed KV etcd3 store
* [**limitedstore**](https://godoc.org/github.com/tus/tusd/limitedstore): A storage wrapper limiting the total used space for uploads
## Running the testsuite

View File

@ -1,4 +1,5 @@
// Tested on etcd 3.1+
// Package etcd3locker provides a locking mechanism using an etcd3 cluster.
// Tested on etcd 3.1/3.2./3.3
package etcd3locker
import (
@ -22,6 +23,7 @@ func newEtcd3Lock(session *concurrency.Session, id string) *etcd3Lock {
}
}
// Acquires a lock from etcd3
func (lock *etcd3Lock) Acquire() error {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
@ -38,10 +40,12 @@ func (lock *etcd3Lock) Acquire() error {
return nil
}
// Releases a lock from etcd3
func (lock *etcd3Lock) Release() error {
return lock.Mutex.Unlock(context.Background())
}
// Closes etcd3 session
func (lock *etcd3Lock) CloseSession() error {
return lock.Session.Close()
}

View File

@ -67,7 +67,7 @@ type Etcd3Locker struct {
locks map[string]*etcd3Lock
mutex sync.Mutex
prefix string
sessionTimeout int
sessionTtl int
}
// New constructs a new locker using the provided client.
@ -85,7 +85,7 @@ func NewWithPrefix(client *etcd3.Client, prefix string) (*Etcd3Locker, error) {
// 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) {
locksMap := map[string]*etcd3Lock{}
return &Etcd3Locker{Client: client, prefix: opts.Prefix(), sessionTimeout: opts.Timeout(), locks: locksMap, mutex: sync.Mutex{}}, nil
return &Etcd3Locker{Client: client, prefix: opts.Prefix(), sessionTtl: opts.Ttl(), locks: locksMap, mutex: sync.Mutex{}}, nil
}
// UseIn adds this locker to the passed composer.
@ -137,7 +137,7 @@ func (locker *Etcd3Locker) UnlockUpload(id string) error {
}
func (locker *Etcd3Locker) createSession() (*concurrency.Session, error) {
return concurrency.NewSession(locker.Client, concurrency.WithTTL(locker.sessionTimeout))
return concurrency.NewSession(locker.Client, concurrency.WithTTL(locker.sessionTtl))
}
func (locker *Etcd3Locker) getId(id string) string {

View File

@ -10,32 +10,38 @@ var (
)
type LockerOptions struct {
timeoutSeconds int
ttl int
prefix string
}
// DefaultLockerOptions() instantiates an instance of LockerOptions
// with default 60 second time to live and an etcd3 prefix of "/tusd"
func DefaultLockerOptions() LockerOptions {
return LockerOptions{
timeoutSeconds: 60,
ttl: 60,
prefix: "/tusd",
}
}
func NewLockerOptions(timeout 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 NewLockerOptions(ttl int, prefix string) LockerOptions {
return LockerOptions{
timeoutSeconds: timeout,
ttl: ttl,
prefix: prefix,
}
}
func (l *LockerOptions) Timeout() int {
if l.timeoutSeconds == 0 {
// Returns the TTL (time to live) of sessions in etcd3
func (l *LockerOptions) Ttl() int {
if l.ttl == 0 {
return DefaultTtl
} else {
return l.timeoutSeconds
return l.ttl
}
}
// Returns the string prefix used to store keys in etcd3
func (l *LockerOptions) Prefix() string {
prefix := l.prefix
if !strings.HasPrefix(prefix, "/") {
@ -49,10 +55,12 @@ func (l *LockerOptions) Prefix() string {
}
}
func (l *LockerOptions) SetTimeout(timeout int) {
l.timeoutSeconds = timeout
// Set etcd3 session TTL (time to live)
func (l *LockerOptions) SetTtl(ttl int) {
l.ttl = ttl
}
// Set string prefix to be used in keys stored into etcd3 by the locker
func (l *LockerOptions) SetPrefix(prefix string) {
l.prefix = prefix
}