-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpillow.go
More file actions
294 lines (251 loc) · 7.3 KB
/
Copy pathpillow.go
File metadata and controls
294 lines (251 loc) · 7.3 KB
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package pillow
import (
"context"
"errors"
"net"
"strings"
"time"
"github.com/nats-io/nats-server/v2/server"
"github.com/nats-io/nats.go"
)
// ErrStartupTimedOut indicates that the embedded nats server exceeded its startup
// timeout duration.
var ErrStartupTimedOut = errors.New("embedded nats server startup timed out")
type options struct {
server *server.Server
withSystemUser bool
systemUserUsername string
systemUserPassword string
// Time to wait for embedded NATS to start
timeout time.Duration
// The returned client will communicate in-process with the NATS server if enabled
inProcessClient bool
// Enable NATS Logger
enableLogging bool
natsSeverOptions *server.Options
}
type Option func(*options) error
type Server struct {
// Reference to the underlying nats-server server.Server
NATSServer *server.Server
opts *options
}
// Duration to wait for embedded NATS to start. Defaults to 5 seconds if omitted
func WithTimeout(t time.Duration) Option {
return func(o *options) error {
o.timeout = t
return nil
}
}
// Duration to wait for embedded NATS to start. Defaults to 5 seconds if omitted
func WithSystemUser(enable bool, username, password string) Option {
return func(o *options) error {
o.withSystemUser = enable
o.systemUserUsername = username
o.systemUserPassword = password
return nil
}
}
// If enabled the clients returned from *Server.NATSClient() will
// communicate with the embedded server over the network instead of in-process.
func WithoutInProcessClient(enable bool) Option {
return func(o *options) error {
o.inProcessClient = !enable
return nil
}
}
// Enable NATS internal logging
func WithLogging(enable bool) Option {
return func(o *options) error {
o.enableLogging = enable
return nil
}
}
// Apply your own NATs Server Options. Note, this will override
// any existing options, so it's recommended to place first
// in the list of options in Run().
func WithNATSServerOptions(natsServerOpts *server.Options) Option {
return func(o *options) error {
o.natsSeverOptions = natsServerOpts
return nil
}
}
// Applies all passed options and starts the NATS server, returning a server struct
// and error if there was a problem starting the server.
//
// All configuration functions start with "With". Example WithLogging(true)
func Run(opts ...Option) (*Server, error) {
// Set default options, then override with their configured options
options := &options{
timeout: time.Second * 5,
inProcessClient: true,
enableLogging: false,
natsSeverOptions: &server.Options{
NoSigs: true,
},
}
for _, o := range opts {
err := o(options)
if err != nil {
return nil, err
}
}
// Disable this force when this is fixed: https://github.com/nats-io/nats-server/issues/6358
options.natsSeverOptions.NoSigs = true
if options.withSystemUser {
sysAcc := server.NewAccount(server.DEFAULT_SYSTEM_ACCOUNT)
options.natsSeverOptions.Accounts = []*server.Account{sysAcc}
options.natsSeverOptions.SystemAccount = server.DEFAULT_SYSTEM_ACCOUNT
options.natsSeverOptions.Users = []*server.User{{
Username: options.systemUserUsername,
Password: options.systemUserPassword,
Account: sysAcc,
}}
}
ns, err := server.NewServer(options.natsSeverOptions)
if err != nil {
return nil, err
}
if options.enableLogging {
ns.ConfigureLogger()
}
ns.Start()
if !ns.ReadyForConnections(options.timeout) {
return nil, ErrStartupTimedOut
}
options.server = ns
return &Server{
NATSServer: ns,
opts: options,
}, nil
}
// Returns a new nats client.
//
// Client will communicate in-process unless the embedded server was started with
// WithoutInProcessClient(true)
func (s *Server) NATSClient() (*nats.Conn, error) {
clientsOpts := []nats.Option{}
if s.opts.inProcessClient {
clientsOpts = append(clientsOpts, nats.InProcessServer(s.NATSServer))
}
return nats.Connect(s.NATSServer.ClientURL(), clientsOpts...)
}
func (s *Server) Shutdown(ctx context.Context) error {
done := make(chan any)
if s.NATSServer == nil {
return nil
}
go func() {
// Re-enable this form of handling when this issue is fixed: https://github.com/nats-io/nats-server/issues/6358
//
// if s.opts.NATSSeverOptions.NoSigs {
// s.NATSServer.Shutdown()
// } else if s.NATSServer.Running() {
// s.NATSServer.Shutdown()
// }
s.NATSServer.Shutdown()
s.NATSServer.WaitForShutdown()
close(done)
}()
select {
case <-done:
return nil
case <-ctx.Done():
return ctx.Err()
}
}
// ##############################################################
// Below are random helper functions, don't care to split out yet
// ##############################################################
func unorderedEqual[T comparable](first, second []T) bool {
if len(first) != len(second) {
return false
}
exists := make(map[T]bool)
for _, value := range first {
exists[value] = true
}
for _, value := range second {
if !exists[value] {
return false
}
}
return true
}
// unorderedEqualFunc reports whether two unordered slices are equal using an equality
// and an identity function function on each pair of elements. If the lengths
// are different unorderedEqualFunc returns false. Otherwise, the elements entered into a
// map and iterated over until one of the elements dosen't match the element with a matching
// identifier, or if a matching identifier element can't be found.
// func unorderedEqualFunc[S1 ~[]E1, S2 ~[]E2, E1, E2 any, T comparable](
// s1 S1,
// s2 S2,
// id func(any) T,
// eq func(E1, E2) bool,
// ) bool {
// // slices.EqualFunc()
// if len(s1) != len(s2) {
// return false
// }
// idMap := make(map[T]E1)
// for _, val1 := range s1 {
// idMap[id(val1)] = val1
// }
// for _, val2 := range s2 {
// val1, ok := idMap[id(val2)]
// if !ok {
// return false
// }
// if !eq(val1, val2) {
// return false
// }
// }
// return true
// }
// All IPs must be of the same type, ie IPv4 or IPv6
func intersectIPs(a, b []net.IP) []net.IP {
set := make([]net.IP, 0)
hash := make(map[string]struct{})
for _, v := range a {
hash[v.String()] = struct{}{}
}
for _, v := range b {
if _, ok := hash[v.String()]; ok {
set = append(set, v)
}
}
return set
}
// FindAndRemoveSubstring searches for the first string in the array that contains the substring.
// If found, it removes the substring from that string and returns the modified string and true.
// If not found, it returns an empty string and false.
func findAndRemoveSubstring(arr []string, sub string) (string, bool) {
for _, str := range arr {
if strings.Contains(str, sub) {
return strings.Replace(str, sub, "", 1), true
}
}
return "", false
}
// var ErrJetStreamStartedSolo = errors.New("JetStream started as standalone as cluster failed")
// func startJetStream(s *server.Server, o *server.Options) error {
// opts := o.Clone()
// cfg := server.JetStreamConfig{
// MaxMemory: opts.JetStreamMaxMemory,
// MaxStore: opts.JetStreamMaxStore,
// StoreDir: opts.StoreDir,
// SyncInterval: opts.SyncInterval,
// SyncAlways: opts.SyncAlways,
// Domain: opts.JetStreamDomain,
// CompressOK: true,
// UniqueTag: opts.JetStreamUniqueTag,
// }
// err := s.EnableJetStream(&cfg)
// if err != nil {
// if s.JetStreamEnabled() && !s.JetStreamIsClustered() {
// return ErrJetStreamStartedSolo
// }
// return s.DisableJetStream()
// }
// return nil
// }