-
Notifications
You must be signed in to change notification settings - Fork 366
/
guerrilla.go
574 lines (525 loc) · 15.9 KB
/
guerrilla.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
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
package guerrilla
import (
"errors"
"fmt"
"os"
"sync"
"sync/atomic"
"github.com/flashmob/go-guerrilla/backends"
"github.com/flashmob/go-guerrilla/log"
)
const (
// all configured servers were just been created
daemonStateNew = iota
// ... been started and running
daemonStateStarted
// ... been stopped
daemonStateStopped
)
type Errors []error
// implement the Error interface
func (e Errors) Error() string {
if len(e) == 1 {
return e[0].Error()
}
// multiple errors
msg := ""
for _, err := range e {
msg += "\n" + err.Error()
}
return msg
}
type Guerrilla interface {
Start() error
Shutdown()
Subscribe(topic Event, fn interface{}) error
Publish(topic Event, args ...interface{})
Unsubscribe(topic Event, handler interface{}) error
SetLogger(log.Logger)
}
type guerrilla struct {
Config AppConfig
servers map[string]*server
// guard controls access to g.servers
guard sync.Mutex
state int8
EventHandler
logStore
backendStore
}
type logStore struct {
atomic.Value
}
type backendStore struct {
atomic.Value
}
type daemonEvent func(c *AppConfig)
type serverEvent func(sc *ServerConfig)
// Get loads the log.logger in an atomic operation. Returns a stderr logger if not able to load
func (ls *logStore) mainlog() log.Logger {
if v, ok := ls.Load().(log.Logger); ok {
return v
}
l, _ := log.GetLogger(log.OutputStderr.String(), log.InfoLevel.String())
return l
}
// setMainlog stores the log value in an atomic operation
func (ls *logStore) setMainlog(log log.Logger) {
ls.Store(log)
}
// Returns a new instance of Guerrilla with the given config, not yet running. Backend started.
func New(ac *AppConfig, b backends.Backend, l log.Logger) (Guerrilla, error) {
g := &guerrilla{
Config: *ac, // take a local copy
servers: make(map[string]*server, len(ac.Servers)),
}
g.backendStore.Store(b)
g.setMainlog(l)
if ac.LogLevel != "" {
if h, ok := l.(*log.HookedLogger); ok {
if h, err := log.GetLogger(h.GetLogDest(), ac.LogLevel); err == nil {
g.setMainlog(h)
}
}
}
// Write the process id (pid) to a file
// we should still be able to continue even if we can't write the pid, error will be logged by writePid()
_ = g.writePid()
g.state = daemonStateNew
err := g.makeServers()
if err != nil {
return g, err
}
// start backend for processing email
err = g.backend().Start()
if err != nil {
return g, err
}
// subscribe for any events that may come in while running
g.subscribeEvents()
return g, err
}
// Instantiate servers
func (g *guerrilla) makeServers() error {
g.mainlog().Debug("making servers")
var errs Errors
for _, sc := range g.Config.Servers {
if _, ok := g.servers[sc.ListenInterface]; ok {
// server already instantiated
continue
}
if err := sc.Validate(); err != nil {
g.mainlog().WithError(errs).Errorf("Failed to create server [%s]", sc.ListenInterface)
errs = append(errs, err)
continue
} else {
sc := sc // pin!
server, err := newServer(&sc, g.backend(), g.mainlog())
if err != nil {
g.mainlog().WithError(err).Errorf("Failed to create server [%s]", sc.ListenInterface)
errs = append(errs, err)
}
if server != nil {
g.servers[sc.ListenInterface] = server
server.setAllowedHosts(g.Config.AllowedHosts)
}
}
}
if len(g.servers) == 0 {
errs = append(errs, errors.New("there are no servers that can start, please check your config"))
}
if len(errs) == 0 {
return nil
}
return errs
}
// findServer finds a server by iface (interface), retuning the server or err
func (g *guerrilla) findServer(iface string) (*server, error) {
g.guard.Lock()
defer g.guard.Unlock()
if server, ok := g.servers[iface]; ok {
return server, nil
}
return nil, errors.New("server not found in g.servers")
}
// removeServer removes a server from the list of servers
func (g *guerrilla) removeServer(iface string) {
g.guard.Lock()
defer g.guard.Unlock()
delete(g.servers, iface)
}
// setConfig sets the app config
func (g *guerrilla) setConfig(c *AppConfig) {
g.guard.Lock()
defer g.guard.Unlock()
g.Config = *c
}
// setServerConfig config updates the server's config, which will update for the next connected client
func (g *guerrilla) setServerConfig(sc *ServerConfig) {
g.guard.Lock()
defer g.guard.Unlock()
if _, ok := g.servers[sc.ListenInterface]; ok {
g.servers[sc.ListenInterface].setConfig(sc)
}
}
// mapServers calls a callback on each server in g.servers map
// It locks the g.servers map before mapping
func (g *guerrilla) mapServers(callback func(*server)) map[string]*server {
defer g.guard.Unlock()
g.guard.Lock()
for _, server := range g.servers {
callback(server)
}
return g.servers
}
// subscribeEvents subscribes event handlers for configuration change events
func (g *guerrilla) subscribeEvents() {
events := map[Event]interface{}{}
// main config changed
events[EventConfigNewConfig] = daemonEvent(func(c *AppConfig) {
g.setConfig(c)
})
// allowed_hosts changed, set for all servers
events[EventConfigAllowedHosts] = daemonEvent(func(c *AppConfig) {
g.mapServers(func(server *server) {
server.setAllowedHosts(c.AllowedHosts)
})
g.mainlog().Infof("allowed_hosts config changed, a new list was set")
})
// the main log file changed
events[EventConfigLogFile] = daemonEvent(func(c *AppConfig) {
var err error
var l log.Logger
if l, err = log.GetLogger(c.LogFile, c.LogLevel); err == nil {
g.setMainlog(l)
g.mapServers(func(server *server) {
// it will change server's logger when the next client gets accepted
server.mainlogStore.Store(l)
})
g.mainlog().Infof("main log for new clients changed to [%s]", c.LogFile)
} else {
g.mainlog().WithError(err).Errorf("main logging change failed [%s]", c.LogFile)
}
})
// re-open the main log file (file not changed)
events[EventConfigLogReopen] = daemonEvent(func(c *AppConfig) {
err := g.mainlog().Reopen()
if err != nil {
g.mainlog().WithError(err).Errorf("main log file [%s] failed to re-open", c.LogFile)
return
}
g.mainlog().Infof("re-opened main log file [%s]", c.LogFile)
})
// when log level changes, apply to mainlog and server logs
events[EventConfigLogLevel] = daemonEvent(func(c *AppConfig) {
l, err := log.GetLogger(g.mainlog().GetLogDest(), c.LogLevel)
if err == nil {
g.logStore.Store(l)
g.mapServers(func(server *server) {
server.logStore.Store(l)
})
g.mainlog().Infof("log level changed to [%s]", c.LogLevel)
}
})
// write out our pid whenever the file name changes in the config
events[EventConfigPidFile] = daemonEvent(func(ac *AppConfig) {
_ = g.writePid()
})
// server config was updated
events[EventConfigServerConfig] = serverEvent(func(sc *ServerConfig) {
g.setServerConfig(sc)
g.mainlog().Infof("server %s config change event, a new config has been saved", sc.ListenInterface)
})
// add a new server to the config & start
events[EventConfigServerNew] = serverEvent(func(sc *ServerConfig) {
g.mainlog().Debugf("event fired [%s] %s", EventConfigServerNew, sc.ListenInterface)
if _, err := g.findServer(sc.ListenInterface); err != nil {
// not found, lets add it
//
if err := g.makeServers(); err != nil {
g.mainlog().WithError(err).Errorf("cannot add server [%s]", sc.ListenInterface)
return
}
g.mainlog().Infof("New server added [%s]", sc.ListenInterface)
if g.state == daemonStateStarted {
err := g.Start()
if err != nil {
g.mainlog().WithError(err).Info("Event server_change:new_server returned errors when starting")
}
}
} else {
g.mainlog().Debugf("new event, but server already fund")
}
})
// start a server that already exists in the config and has been enabled
events[EventConfigServerStart] = serverEvent(func(sc *ServerConfig) {
if server, err := g.findServer(sc.ListenInterface); err == nil {
if server.state == ServerStateStopped || server.state == ServerStateNew {
g.mainlog().Infof("Starting server [%s]", server.listenInterface)
err := g.Start()
if err != nil {
g.mainlog().WithError(err).Info("Event server_change:start_server returned errors when starting")
}
}
}
})
// stop running a server
events[EventConfigServerStop] = serverEvent(func(sc *ServerConfig) {
if server, err := g.findServer(sc.ListenInterface); err == nil {
if server.state == ServerStateRunning {
server.Shutdown()
g.mainlog().Infof("Server [%s] stopped.", sc.ListenInterface)
}
}
})
// server was removed from config
events[EventConfigServerRemove] = serverEvent(func(sc *ServerConfig) {
if server, err := g.findServer(sc.ListenInterface); err == nil {
server.Shutdown()
g.removeServer(sc.ListenInterface)
g.mainlog().Infof("Server [%s] removed from config, stopped it.", sc.ListenInterface)
}
})
// TLS changes
events[EventConfigServerTLSConfig] = serverEvent(func(sc *ServerConfig) {
if server, err := g.findServer(sc.ListenInterface); err == nil {
if err := server.configureTLS(); err == nil {
g.mainlog().Infof("Server [%s] new TLS configuration loaded", sc.ListenInterface)
} else {
g.mainlog().WithError(err).Errorf("Server [%s] failed to load the new TLS configuration", sc.ListenInterface)
}
}
})
// when server's timeout change.
events[EventConfigServerTimeout] = serverEvent(func(sc *ServerConfig) {
g.mapServers(func(server *server) {
server.setTimeout(sc.Timeout)
})
})
// when server's max clients change.
events[EventConfigServerMaxClients] = serverEvent(func(sc *ServerConfig) {
g.mapServers(func(server *server) {
// TODO resize the pool somehow
})
})
// when a server's log file changes
events[EventConfigServerLogFile] = serverEvent(func(sc *ServerConfig) {
if server, err := g.findServer(sc.ListenInterface); err == nil {
var err error
var l log.Logger
level := g.mainlog().GetLevel()
if l, err = log.GetLogger(sc.LogFile, level); err == nil {
g.setMainlog(l)
backends.Svc.SetMainlog(l)
// it will change to the new logger on the next accepted client
server.logStore.Store(l)
g.mainlog().Infof("Server [%s] changed, new clients will log to: [%s]",
sc.ListenInterface,
sc.LogFile,
)
} else {
g.mainlog().WithError(err).Errorf(
"Server [%s] log change failed to: [%s]",
sc.ListenInterface,
sc.LogFile,
)
}
}
})
// when the daemon caught a sighup, event for individual server
events[EventConfigServerLogReopen] = serverEvent(func(sc *ServerConfig) {
if server, err := g.findServer(sc.ListenInterface); err == nil {
if err = server.log().Reopen(); err != nil {
g.mainlog().WithError(err).Errorf("server [%s] log file [%s] failed to re-open", sc.ListenInterface, sc.LogFile)
return
}
g.mainlog().Infof("Server [%s] re-opened log file [%s]", sc.ListenInterface, sc.LogFile)
}
})
// when the backend changes
events[EventConfigBackendConfig] = daemonEvent(func(appConfig *AppConfig) {
logger, _ := log.GetLogger(appConfig.LogFile, appConfig.LogLevel)
// shutdown the backend first.
var err error
if err = g.backend().Shutdown(); err != nil {
logger.WithError(err).Warn("Backend failed to shutdown")
return
}
// init a new backend, Revert to old backend config if it fails
if newBackend, newErr := backends.New(appConfig.BackendConfig, logger); newErr != nil {
logger.WithError(newErr).Error("Error while loading the backend")
err = g.backend().Reinitialize()
if err != nil {
logger.WithError(err).Fatal("failed to revert to old backend config")
return
}
err = g.backend().Start()
if err != nil {
logger.WithError(err).Fatal("failed to start backend with old config")
return
}
logger.Info("reverted to old backend config")
} else {
// swap to the bew backend (assuming old backend was shutdown so it can be safely swapped)
if err := newBackend.Start(); err != nil {
logger.WithError(err).Error("backend could not start")
}
logger.Info("new backend started")
g.storeBackend(newBackend)
}
})
var err error
for topic, fn := range events {
switch f := fn.(type) {
case daemonEvent:
err = g.Subscribe(topic, f)
case serverEvent:
err = g.Subscribe(topic, f)
}
if err != nil {
g.mainlog().WithError(err).Errorf("failed to subscribe on topic [%s]", topic)
break
}
}
}
func (g *guerrilla) storeBackend(b backends.Backend) {
g.backendStore.Store(b)
g.mapServers(func(server *server) {
server.setBackend(b)
})
}
func (g *guerrilla) backend() backends.Backend {
if b, ok := g.backendStore.Load().(backends.Backend); ok {
return b
}
return nil
}
// Entry point for the application. Starts all servers.
func (g *guerrilla) Start() error {
var startErrors Errors
g.guard.Lock()
defer func() {
g.state = daemonStateStarted
g.guard.Unlock()
}()
if len(g.servers) == 0 {
return append(startErrors, errors.New("no servers to start, please check the config"))
}
if g.state == daemonStateStopped {
// when a backend is shutdown, we need to re-initialize before it can be started again
if err := g.backend().Reinitialize(); err != nil {
startErrors = append(startErrors, err)
}
if err := g.backend().Start(); err != nil {
startErrors = append(startErrors, err)
}
}
// channel for reading errors
errs := make(chan error, len(g.servers))
var startWG sync.WaitGroup
// start servers, send any errors back to errs channel
for ListenInterface := range g.servers {
if !g.servers[ListenInterface].isEnabled() {
// not enabled
continue
}
if g.servers[ListenInterface].state != ServerStateNew &&
g.servers[ListenInterface].state != ServerStateStopped {
continue
}
startWG.Add(1)
go func(s *server) {
g.mainlog().Infof("Starting: %s", s.listenInterface)
if err := s.Start(&startWG); err != nil {
errs <- err
}
}(g.servers[ListenInterface])
}
// wait for all servers to start (or fail)
startWG.Wait()
// close, then read any errors
close(errs)
for err := range errs {
if err != nil {
startErrors = append(startErrors, err)
}
}
if len(startErrors) > 0 {
return startErrors
}
return nil
}
func (g *guerrilla) Shutdown() {
// shut down the servers first
g.mapServers(func(s *server) {
if s.state == ServerStateRunning {
s.Shutdown()
g.mainlog().Infof("shutdown completed for [%s]", s.listenInterface)
}
})
g.guard.Lock()
defer func() {
g.state = daemonStateStopped
defer g.guard.Unlock()
}()
if err := g.backend().Shutdown(); err != nil {
g.mainlog().WithError(err).Warn("Backend failed to shutdown")
} else {
g.mainlog().Infof("Backend shutdown completed")
}
}
// SetLogger sets the logger for the app and propagates it to sub-packages (eg.
func (g *guerrilla) SetLogger(l log.Logger) {
g.setMainlog(l)
backends.Svc.SetMainlog(l)
}
// writePid writes the pid (process id) to the file specified in the config.
// Won't write anything if no file specified
func (g *guerrilla) writePid() (err error) {
var f *os.File
defer func() {
if f != nil {
if closeErr := f.Close(); closeErr != nil {
err = closeErr
}
}
if err != nil {
g.mainlog().WithError(err).Errorf("error while writing pidFile (%s)", g.Config.PidFile)
}
}()
if len(g.Config.PidFile) > 0 {
if f, err = os.Create(g.Config.PidFile); err != nil {
return err
}
pid := os.Getpid()
if _, err := f.WriteString(fmt.Sprintf("%d", pid)); err != nil {
return err
}
if err = f.Sync(); err != nil {
return err
}
g.mainlog().Infof("pid_file (%s) written with pid:%v", g.Config.PidFile, pid)
}
return nil
}
// CheckFileLimit checks the number of files we can open (works on OS'es that support the ulimit command)
func CheckFileLimit(c *AppConfig) (bool, int, uint64) {
fileLimit, err := getFileLimit()
maxClients := 0
if err != nil {
// since we can't get the limit, return true to indicate the check passed
return true, maxClients, fileLimit
}
if c.Servers == nil {
// no servers have been configured, assuming default
maxClients = defaultMaxClients
} else {
for _, s := range c.Servers {
maxClients += s.MaxClients
}
}
if uint64(maxClients) > fileLimit {
return false, maxClients, fileLimit
}
return true, maxClients, fileLimit
}