File
Blob: tun/client/reload.go
| 1 | package client |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "time" |
| 6 | |
| 7 | "go.uber.org/zap" |
| 8 | ) |
| 9 | |
| 10 | func (c *Client) reloadOnSignal(ctx context.Context) { |
| 11 | defer c.closeWg.Done() |
| 12 | |
| 13 | for { |
| 14 | select { |
| 15 | case <-c.closeCh: |
| 16 | return |
| 17 | case <-ctx.Done(): |
| 18 | return |
| 19 | case <-c.ReloadSignal: |
| 20 | c.Logger.Info("Received SIGHUP, reloading config") |
| 21 | if result := c.doReload(ctx); result.Error != "" { |
| 22 | c.Logger.Warn("Configuration reload did not fully succeed", zap.String("error", result.Error)) |
| 23 | } |
| 24 | } |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | func (c *Client) doReload(ctx context.Context) SyncResult { |
| 29 | c.syncMu.Lock() |
| 30 | defer c.syncMu.Unlock() |
| 31 | onReload := func(prev, curr []Tunnel) { |
| 32 | diff := diffTunnels(prev, curr) |
| 33 | c.closeOutdatedProxies(diff...) |
| 34 | c.Configuration.buildRouter(diff...) |
| 35 | } |
| 36 | c.configMu.Lock() |
| 37 | if err := c.Configuration.reloadFile(onReload); err != nil { |
| 38 | c.Logger.Error("Error reloading config file", zap.Error(err)) |
| 39 | c.configMu.Unlock() |
| 40 | now := time.Now() |
| 41 | return SyncResult{Error: err.Error(), AttemptedAt: &now, Tunnels: []TunnelSyncResult{}} |
| 42 | } |
| 43 | c.configMu.Unlock() |
| 44 | return c.syncConfigTunnels(ctx, true) |
| 45 | } |
| 46 | |
| 47 | func (c *Client) UpdateApex(apex string) { |
| 48 | c.configMu.Lock() |
| 49 | c.Configuration.Apex = apex |
| 50 | if err := c.Configuration.writeFile(); err != nil { |
| 51 | c.Logger.Error("Error saving to config file", zap.Error(err)) |
| 52 | } |
| 53 | c.configMu.Unlock() |
| 54 | } |
| 55 | |
| 56 | func (c *Client) GetCurrentConfig() *Config { |
| 57 | c.configMu.RLock() |
| 58 | defer c.configMu.RUnlock() |
| 59 | return c.Configuration.clone() |
| 60 | } |