/
storage.go
62 lines (48 loc) · 1.17 KB
/
storage.go
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
58
59
60
61
62
package storage
import (
"context"
"time"
)
type Storage interface {
MutexTryLock(context.Context) (bool, error)
MutexUnlock(context.Context) error
DictionaryPut(ctx context.Context, k, v []byte) error
DictionaryGet(ctx context.Context, k []byte) ([]byte, error)
DictionaryRemove(ctx context.Context, k []byte) error
}
const (
Stoa = "stoa"
Etcd = "etcd"
)
const (
DefaultType = Stoa
DefaultTTL = 5000 * time.Millisecond
)
type options struct {
t string
ttl time.Duration
bootstrap string
}
type Option func(*options)
func defaultOptions() *options {
return &options{
t: DefaultType,
ttl: DefaultTTL,
}
}
func WithBootstrap(v string) Option { return func(o *options) { o.bootstrap = v } }
func WithTTL(v time.Duration) Option { return func(o *options) { o.ttl = v } }
func WithType(v string) Option { return func(o *options) { o.t = v } }
func New(ctx context.Context, opts ...Option) (Storage, error) {
cfg := defaultOptions()
for _, o := range opts {
o(cfg)
}
switch cfg.t {
case Stoa:
return newStoa(ctx, cfg.bootstrap, cfg.ttl)
case Etcd:
return newEtcd(ctx, cfg.bootstrap, cfg.ttl)
}
panic("unknown storage type")
}