-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathoptions.go
96 lines (82 loc) · 2.73 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
package setddblock
import (
"context"
"time"
)
// Options are for changing the behavior of DynamoDB Locker and are changed by the function passed to the New () function.
// See the WithXXX options for more information.
type Options struct {
NoPanic bool
Logger Logger
Delay bool
Endpoint string
Region string
LeaseDuration time.Duration
ctx context.Context
}
//Default values
var (
DefaultLeaseDuration = 10 * time.Second
)
func newOptions() *Options {
return &Options{
Logger: voidLogger{},
LeaseDuration: DefaultLeaseDuration,
Delay: true,
ctx: context.Background(),
}
}
// WithNoPanic changes the behavior so that it does not panic if an error occurs in the Lock () and Unlock () functions.
// Check the LastErr () function to see if an error has occurred when WithNoPanic is specified.
func WithNoPanic() func(opts *Options) {
return func(opts *Options) {
opts.NoPanic = true
}
}
// WithDelay will delay the acquisition of the lock if it fails to acquire the lock. This is similar to the N option of setlock.
//The default is delay enalbed(true). Specify false if you want to exit immediately if Lock acquisition fails.
func WithDelay(delay bool) func(opts *Options) {
return func(opts *Options) {
opts.Delay = delay
}
}
// Logger is a Logging interface used inside DynamoDB Locker
type Logger interface {
Print(v ...interface{})
Printf(format string, v ...interface{})
Println(v ...interface{})
}
type voidLogger struct{}
func (voidLogger) Print(v ...interface{}) {}
func (voidLogger) Printf(format string, v ...interface{}) {}
func (voidLogger) Println(v ...interface{}) {}
// WithLogger is a setting to enable the log output of DynamoDB Locker. By default, Logger that does not output anywhere is specified.
func WithLogger(logger Logger) func(opts *Options) {
return func(opts *Options) {
opts.Logger = logger
}
}
// WithEndpoint is an endpoint specification option for Local development. Please enter the URL of DynamoDB Local etc.
func WithEndpoint(endpoint string) func(opts *Options) {
return func(opts *Options) {
opts.Endpoint = endpoint
}
}
// WithRegion specifies the AWS Region. Default AWS_DEFAULT_REGION env
func WithRegion(region string) func(opts *Options) {
return func(opts *Options) {
opts.Region = region
}
}
// WithLeaseDuration affects the heartbeat interval and TTL after Lock acquisition. The default is 10 seconds
func WithLeaseDuration(d time.Duration) func(opts *Options) {
return func(opts *Options) {
opts.LeaseDuration = d
}
}
// WithContext specifies the Context used by Lock() and Unlock().
func WithContext(ctx context.Context) func(opts *Options) {
return func(opts *Options) {
opts.ctx = ctx
}
}