/
options.go
144 lines (125 loc) · 4.02 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package nq
import (
"fmt"
"time"
"github.com/nats-io/nats.go"
)
const (
defaultMaxReconnects = 100
defaultShutdownOnNatsDisconnect = false
defaultKVName = "nq-store"
defaultReconnectWait = time.Second * 10
defaultAuthenticationType = NoAuthenticationOpt
defaultMaxRetry = 0
)
var defaultNatsOptions = make([]nats.Option, 0)
var defaultAuthenticationObject interface{} = nil
const (
// Authentication types
// TODO: error on using multiple of belwo
UPassAuthenticationOpt ClientOptionType = iota
TokenAuthenticationOpt
NoAuthenticationOpt
// General options
ShutdownOnNatsDisconnectOpt
)
type ClientConnectionOption interface {
String() string
Type() ClientOptionType
Value() interface{}
}
// Internal option representations.
type (
shutdownOnNatsDisconnect struct{}
noAuthentication struct{}
uPassAuthentication struct {
Username string
Password string
}
tokenAuthentication struct {
Name string
Token string
}
)
// Shutdown server and workers if connection with nats-server is broken.
// Results in any executing tasks being cancelled, if not finished in time specified by `shutdownTimeout`. Option is useful when workers should be
// `cancellable` at all times.
//
// By default, inactive
func ShutdownOnNatsDisconnect() shutdownOnNatsDisconnect {
return shutdownOnNatsDisconnect{}
}
func (u shutdownOnNatsDisconnect) String() string {
return "nil"
}
func (u shutdownOnNatsDisconnect) Type() ClientOptionType { return ShutdownOnNatsDisconnectOpt }
func (u shutdownOnNatsDisconnect) Value() interface{} { return u }
// Connect to nats-server without any authentication
//
// Default
func NoAuthentcation() noAuthentication {
return noAuthentication{}
}
func (u noAuthentication) String() string {
return "nil"
}
func (u noAuthentication) Type() ClientOptionType { return NoAuthenticationOpt }
func (u noAuthentication) Value() interface{} { return u }
// Connect to nats-server using username:password pair
//
// Read more: https://docs.nats.io/running-a-nats-service/configuration/securing_nats/auth_intro/username_passwordß
func UserPassAuthentcation(username, password string) uPassAuthentication {
return uPassAuthentication{username, password}
}
func (u uPassAuthentication) String() string {
return fmt.Sprintf("Username(%s), Password(%s)", u.Username, u.Password)
}
func (u uPassAuthentication) Type() ClientOptionType { return UPassAuthenticationOpt }
func (u uPassAuthentication) Value() interface{} { return u }
// Connect to nats-server using token authentication
//
// Read more: https://docs.nats.io/running-a-nats-service/configuration/securing_nats/auth_intro/tokens
func TokenAuthentication(username, password string) tokenAuthentication {
return tokenAuthentication{username, password}
}
func (u tokenAuthentication) String() string {
return fmt.Sprintf("Name(%s), Token(%s)", u.Name, u.Token)
}
func (u tokenAuthentication) Type() ClientOptionType { return TokenAuthenticationOpt }
func (u tokenAuthentication) Value() interface{} { return u }
// Merges user defined options with default options
// Also validates user provided options
func withDefaultClientOptions(opts ...ClientConnectionOption) (ClientOption, error) {
res := ClientOption{
Timeout: time.Duration(time.Minute),
AuthenticationType: defaultAuthenticationType,
AuthenticationObject: defaultAuthenticationObject,
NatsOption: defaultNatsOptions,
ShutdownOnNatsDisconnect: defaultShutdownOnNatsDisconnect,
}
for _, opt := range opts {
switch opt := opt.(type) {
case shutdownOnNatsDisconnect:
{
res.ShutdownOnNatsDisconnect = true
}
case noAuthentication:
{
// skip
continue
}
case uPassAuthentication:
{
res.NatsOption = append(res.NatsOption, nats.UserInfo(opt.Username, opt.Password))
}
case tokenAuthentication:
{
res.NatsOption = append(res.NatsOption, nats.Name(opt.Name), nats.Token(opt.Token))
}
default:
{ // unexpected option
}
}
}
return res, nil
}