-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
121 lines (96 loc) · 2.26 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package lhw
import (
"errors"
"net/http"
"time"
"github.com/loghole/lhw/transport"
)
const (
DefaultQueueCap = 1000
DefaultPingInterval = time.Second
DefaultRequestTimeout = 2 * time.Second
)
var (
ErrBadQueueCapacity = errors.New("queue capacity invalid")
ErrBadRequestTimeout = errors.New("request timeout invalid")
ErrBadPingInterval = errors.New("ping interval invalid")
ErrSuccessCodes = errors.New("success codes empty")
)
type Option func(option *Options) error
func WithQueueCap(capacity int) Option {
return func(options *Options) error {
if capacity <= 0 {
return ErrBadQueueCapacity
}
options.QueueCap = capacity
return nil
}
}
func WithLogger(logger Logger) Option {
return func(options *Options) error {
options.Logger = logger
return nil
}
}
func WithInsecure() Option {
return func(options *Options) error {
options.Insecure = true
return nil
}
}
func WithRequestTimeout(timeout time.Duration) Option {
return func(options *Options) error {
if timeout <= 0 {
return ErrBadRequestTimeout
}
options.RequestTimeout = timeout
return nil
}
}
func WithPingInterval(interval time.Duration) Option {
return func(options *Options) error {
if interval <= 0 {
return ErrBadPingInterval
}
options.PingInterval = interval
return nil
}
}
func WithSuccessCodes(codes ...int) Option {
return func(options *Options) error {
if len(codes) == 0 {
return ErrSuccessCodes
}
options.SuccessCodes = codes
return nil
}
}
type Options struct {
// Writer settings
QueueCap int
Logger Logger
Servers []string
Insecure bool
RequestTimeout time.Duration
PingInterval time.Duration
SuccessCodes []int
}
// GetDefaultOptions returns default configuration options for the client.
func GetDefaultOptions() *Options {
return &Options{
QueueCap: DefaultQueueCap,
Insecure: false,
RequestTimeout: DefaultRequestTimeout,
PingInterval: DefaultPingInterval,
SuccessCodes: []int{http.StatusOK, http.StatusCreated},
}
}
func (o *Options) transportConfig() transport.Config {
return transport.Config{
Servers: o.Servers,
Insecure: o.Insecure,
RequestTimeout: o.RequestTimeout,
PingInterval: o.PingInterval,
SuccessCodes: o.SuccessCodes,
}
}