-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoptions.go
97 lines (84 loc) · 1.87 KB
/
options.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package natsmutex
import (
"fmt"
"time"
"github.com/google/uuid"
"github.com/nats-io/nats.go"
)
// Option is a functional option type for configuring Mutex.
type Option func(*Mutex) error
// WithUrl creates NATS connection using provided url.
func WithUrl(url string) Option {
return func(m *Mutex) error {
nc, err := nats.Connect(url)
if err != nil {
return fmt.Errorf("failed to connect to NATS server: %w", err)
}
m.nc = nc
return nil
}
}
// WithConn injects an existing NATS connection.
func WithConn(nc *nats.Conn) Option {
return func(m *Mutex) error {
m.nc = nc
return nil
}
}
// WithJetStream sets a custom JetStream context.
func WithJetStream(js nats.JetStreamContext) Option {
return func(m *Mutex) error {
m.js = js
return nil
}
}
// WithKeyValue sets a custom KeyValue store.
func WithKeyValue(kv nats.KeyValue) Option {
return func(m *Mutex) error {
m.kv = kv
return nil
}
}
// WithBucket sets a custom bucket name.
func WithBucket(bucket string) Option {
return func(m *Mutex) error {
m.bucket = bucket
return nil
}
}
// WithResourceID sets a custom key for the lock.
func WithResourceID(id string) Option {
return func(m *Mutex) error {
m.so = getSingleton(id)
return nil
}
}
// WithOwner sets a custom owner ID.
func WithOwner(owner uuid.UUID) Option {
return func(m *Mutex) error {
m.owner = owner
m.checkOwner = true
return nil
}
}
// WithOwnershipCheck enables or disables ownership check.
func WithOwnershipCheck(check bool) Option {
return func(m *Mutex) error {
m.checkOwner = check
return nil
}
}
// WithTTL sets a custom TTL for the lock.
func WithTTL(ttl time.Duration) Option {
return func(m *Mutex) error {
m.ttl = ttl
return nil
}
}
// WithBackoff sets a custom backoff duration.
func WithBackoff(backoff time.Duration) Option {
return func(m *Mutex) error {
m.backoff = backoff
return nil
}
}