-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathclient_options.go
209 lines (155 loc) · 5.18 KB
/
client_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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package gorabbit
import (
"time"
"github.com/Netflix/go-env"
)
// ClientOptions holds all necessary properties to launch a successful connection with an MQTTClient.
type ClientOptions struct {
// Host is the RabbitMQ server host name.
Host string
// Port is the RabbitMQ server port number.
Port uint
// Username is the RabbitMQ server allowed username.
Username string
// Password is the RabbitMQ server allowed password.
Password string
// Vhost is used for CloudAMQP connections to set the specific vhost.
Vhost string
// UseTLS defines whether we use amqp or amqps protocol.
UseTLS bool
// ConnectionName is the client connection name passed on to the RabbitMQ server.
ConnectionName string
// KeepAlive will determine whether the re-connection and retry mechanisms should be triggered.
KeepAlive bool
// RetryDelay will define the delay for the re-connection and retry mechanism.
RetryDelay time.Duration
// MaxRetry will define the number of retries when an amqpMessage could not be processed.
MaxRetry uint
// PublishingCacheTTL defines the time to live for each publishing cache item.
PublishingCacheTTL time.Duration
// PublishingCacheSize defines the max length of the publishing cache.
PublishingCacheSize uint64
// Mode will specify whether logs are enabled or not.
Mode string
// Marshaller defines the content type used for messages and how they're marshalled (default: JSON).
Marshaller Marshaller
}
// DefaultClientOptions will return a ClientOptions with default values.
func DefaultClientOptions() *ClientOptions {
return &ClientOptions{
Host: defaultHost,
Port: defaultPort,
Username: defaultUsername,
Password: defaultPassword,
Vhost: defaultVhost,
UseTLS: defaultUseTLS,
KeepAlive: defaultKeepAlive,
RetryDelay: defaultRetryDelay,
MaxRetry: defaultMaxRetry,
PublishingCacheTTL: defaultPublishingCacheTTL,
PublishingCacheSize: defaultPublishingCacheSize,
Mode: defaultMode,
Marshaller: defaultMarshaller,
}
}
// NewClientOptions is the exported builder for a ClientOptions and will offer setter methods for an easy construction.
// Any non-assigned field will be set to default through DefaultClientOptions.
func NewClientOptions() *ClientOptions {
return DefaultClientOptions()
}
// NewClientOptionsFromEnv will generate a ClientOptions from environment variables. Empty values will be taken as default
// through the DefaultClientOptions.
func NewClientOptionsFromEnv() *ClientOptions {
defaultOpts := DefaultClientOptions()
fromEnv := new(RabbitMQEnvs)
_, err := env.UnmarshalFromEnviron(fromEnv)
if err != nil {
return defaultOpts
}
if fromEnv.Host != "" {
defaultOpts.Host = fromEnv.Host
}
if fromEnv.Port > 0 {
defaultOpts.Port = fromEnv.Port
}
if fromEnv.Username != "" {
defaultOpts.Username = fromEnv.Username
}
if fromEnv.Password != "" {
defaultOpts.Password = fromEnv.Password
}
if fromEnv.Vhost != "" {
defaultOpts.Vhost = fromEnv.Vhost
}
defaultOpts.UseTLS = fromEnv.UseTLS
defaultOpts.ConnectionName = fromEnv.ConnectionName
return defaultOpts
}
// SetHost will assign the Host.
func (c *ClientOptions) SetHost(host string) *ClientOptions {
c.Host = host
return c
}
// SetPort will assign the Port.
func (c *ClientOptions) SetPort(port uint) *ClientOptions {
c.Port = port
return c
}
// SetCredentials will assign the Username and Password.
func (c *ClientOptions) SetCredentials(username, password string) *ClientOptions {
c.Username = username
c.Password = password
return c
}
// SetVhost will assign the Vhost.
func (c *ClientOptions) SetVhost(vhost string) *ClientOptions {
c.Vhost = vhost
return c
}
// SetUseTLS will assign the UseTLS status.
func (c *ClientOptions) SetUseTLS(use bool) *ClientOptions {
c.UseTLS = use
return c
}
// SetConnectionName will assign the ConnectionName.
func (c *ClientOptions) SetConnectionName(connectionName string) *ClientOptions {
c.ConnectionName = connectionName
return c
}
// SetKeepAlive will assign the KeepAlive status.
func (c *ClientOptions) SetKeepAlive(keepAlive bool) *ClientOptions {
c.KeepAlive = keepAlive
return c
}
// SetRetryDelay will assign the retry delay.
func (c *ClientOptions) SetRetryDelay(delay time.Duration) *ClientOptions {
c.RetryDelay = delay
return c
}
// SetMaxRetry will assign the max retry count.
func (c *ClientOptions) SetMaxRetry(retry uint) *ClientOptions {
c.MaxRetry = retry
return c
}
// SetPublishingCacheTTL will assign the publishing cache item TTL.
func (c *ClientOptions) SetPublishingCacheTTL(ttl time.Duration) *ClientOptions {
c.PublishingCacheTTL = ttl
return c
}
// SetPublishingCacheSize will assign the publishing cache max length.
func (c *ClientOptions) SetPublishingCacheSize(size uint64) *ClientOptions {
c.PublishingCacheSize = size
return c
}
// SetMode will assign the Mode if valid.
func (c *ClientOptions) SetMode(mode string) *ClientOptions {
if isValidMode(mode) {
c.Mode = mode
}
return c
}
// SetMarshaller will assign the Marshaller.
func (c *ClientOptions) SetMarshaller(marshaller Marshaller) *ClientOptions {
c.Marshaller = marshaller
return c
}