Add hot config reload

This commit is contained in:
simon
2019-06-01 15:00:50 -04:00
parent cdc3017948
commit 4f7b5b7d98
6 changed files with 65 additions and 19 deletions

View File

@@ -3,6 +3,7 @@ package main
import (
"encoding/json"
"fmt"
"github.com/sirupsen/logrus"
"golang.org/x/time/rate"
"io/ioutil"
"os"
@@ -98,6 +99,32 @@ func applyConfig(proxy *Proxy) {
}
}
func (b *Balancer) reloadConfig() {
b.proxyMutex.Lock()
loadConfig()
if b.proxies != nil {
b.proxies = b.proxies[:0]
}
for _, proxyConf := range config.Proxies {
proxy, err := NewProxy(proxyConf.Name, proxyConf.Url)
handleErr(err)
b.proxies = append(b.proxies, proxy)
applyConfig(proxy)
logrus.WithFields(logrus.Fields{
"name": proxy.Name,
"url": proxy.Url,
}).Info("Proxy")
}
b.proxyMutex.Unlock()
logrus.Info("Reloaded config")
}
func handleErr(err error) {
if err != nil {
panic(err)