File size: 1,096 Bytes
fea99b3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | package lockr
import (
"errors"
"fmt"
"strings"
xxhash "github.com/zeebo/xxh3"
)
var stringSeparator = ":"
// Key is a unique identifier for a resource and a scope that can be locked
type Key interface {
String() string
Hash64() uint64
}
// NewKey constructs a key for the given scopes.
// Scope is always required and must be non-empty.
func NewKey(scopes ...string) (Key, error) {
if len(scopes) == 0 {
return nil, errors.New("at least one scope is required")
}
for idx, s := range scopes {
if s == "" {
return nil, fmt.Errorf("scope cannot be empty [index=%d]", idx)
}
if strings.Contains(s, stringSeparator) {
return nil, fmt.Errorf("scope cannot contain %q [index=%d]", stringSeparator, idx)
}
}
return &key{scopes: scopes}, nil
}
type key struct {
scopes []string
}
var _ Key = (*key)(nil)
func (k *key) String() string {
return strings.Join(k.scopes, stringSeparator)
}
// Hash64 translates the key string to a 64bit keyspace via hashing it
func (k *key) Hash64() uint64 {
h := xxhash.New()
_, _ = h.WriteString(k.String())
return h.Sum64()
}
|