2018-11-10 20:05:44 +00:00
|
|
|
package etcd3locker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
DefaultTtl = 60
|
|
|
|
DefaultPrefix = "/tusd"
|
|
|
|
)
|
|
|
|
|
|
|
|
type LockerOptions struct {
|
2018-11-10 23:33:06 +00:00
|
|
|
ttl int
|
|
|
|
prefix string
|
2018-11-10 20:05:44 +00:00
|
|
|
}
|
|
|
|
|
2018-11-10 23:33:06 +00:00
|
|
|
// DefaultLockerOptions() instantiates an instance of LockerOptions
|
|
|
|
// with default 60 second time to live and an etcd3 prefix of "/tusd"
|
2018-11-10 20:05:44 +00:00
|
|
|
func DefaultLockerOptions() LockerOptions {
|
|
|
|
return LockerOptions{
|
2018-11-10 23:33:06 +00:00
|
|
|
ttl: 60,
|
|
|
|
prefix: "/tusd",
|
2018-11-10 20:05:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-10 23:33:06 +00:00
|
|
|
// 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 {
|
2018-11-10 20:05:44 +00:00
|
|
|
return LockerOptions{
|
2018-11-10 23:33:06 +00:00
|
|
|
ttl: ttl,
|
|
|
|
prefix: prefix,
|
2018-11-10 20:05:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-10 23:33:06 +00:00
|
|
|
// Returns the TTL (time to live) of sessions in etcd3
|
|
|
|
func (l *LockerOptions) Ttl() int {
|
|
|
|
if l.ttl == 0 {
|
2018-11-10 20:05:44 +00:00
|
|
|
return DefaultTtl
|
|
|
|
} else {
|
2018-11-10 23:33:06 +00:00
|
|
|
return l.ttl
|
2018-11-10 20:05:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-10 23:33:06 +00:00
|
|
|
// Returns the string prefix used to store keys in etcd3
|
2018-11-10 20:05:44 +00:00
|
|
|
func (l *LockerOptions) Prefix() string {
|
|
|
|
prefix := l.prefix
|
|
|
|
if !strings.HasPrefix(prefix, "/") {
|
|
|
|
prefix = "/" + prefix
|
|
|
|
}
|
|
|
|
|
|
|
|
if prefix == "" {
|
|
|
|
return DefaultPrefix
|
|
|
|
} else {
|
|
|
|
return prefix
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-10 23:33:06 +00:00
|
|
|
// Set etcd3 session TTL (time to live)
|
|
|
|
func (l *LockerOptions) SetTtl(ttl int) {
|
|
|
|
l.ttl = ttl
|
2018-11-10 20:05:44 +00:00
|
|
|
}
|
|
|
|
|
2018-11-10 23:33:06 +00:00
|
|
|
// Set string prefix to be used in keys stored into etcd3 by the locker
|
2018-11-10 20:05:44 +00:00
|
|
|
func (l *LockerOptions) SetPrefix(prefix string) {
|
|
|
|
l.prefix = prefix
|
|
|
|
}
|