Commit 5e0e08d5 authored by ginuerzh's avatar ginuerzh

live reloading for base config

parent 194b651d
......@@ -122,8 +122,8 @@ func (m *domainMatcher) String() string {
// It contains a list of matchers.
type Bypass struct {
matchers []Matcher
reversed bool
period time.Duration // the period for live reloading
reversed bool
mux sync.RWMutex
}
......
......@@ -7,6 +7,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net/url"
"os"
......@@ -51,35 +52,60 @@ func loadCA(caFile string) (cp *x509.CertPool, err error) {
return
}
func loadConfigureFile(configureFile string) error {
if configureFile == "" {
return nil
}
content, err := ioutil.ReadFile(configureFile)
type baseConfig struct {
route
Routes []route
ReloadPeriod string
Debug bool
}
func parseBaseConfig(s string) (*baseConfig, error) {
file, err := os.Open(s)
if err != nil {
return err
return nil, err
}
var cfg struct {
route
Routes []route
defer file.Close()
if err := json.NewDecoder(file).Decode(baseCfg); err != nil {
return nil, err
}
if err := json.Unmarshal(content, &cfg); err != nil {
return baseCfg, nil
}
func (cfg *baseConfig) IsValid() bool {
return len(cfg.route.ServeNodes) > 0
}
func (cfg *baseConfig) Reload(r io.Reader) error {
c := baseConfig{}
if err := json.NewDecoder(r).Decode(&c); err != nil {
return err
}
if len(cfg.route.ServeNodes) > 0 {
routes = append(routes, cfg.route)
cfg.route.Close()
for _, r := range cfg.Routes {
r.Close()
}
*cfg = c
gost.Debug = cfg.Debug
if err := cfg.route.serve(); err != nil {
return err
}
for _, route := range cfg.Routes {
if len(route.ServeNodes) > 0 {
routes = append(routes, route)
if err := route.serve(); err != nil {
return err
}
}
gost.Debug = cfg.Debug
return nil
}
func (cfg *baseConfig) Period() time.Duration {
d, _ := time.ParseDuration(cfg.ReloadPeriod)
return d
}
type stringList []string
func (l *stringList) String() string {
......@@ -240,3 +266,10 @@ func parseResolver(cfg string) gost.Resolver {
return resolver
}
func parseHosts(s string) *gost.Hosts {
hosts := gost.NewHosts()
go gost.PeriodReload(hosts, s)
return hosts
}
This diff is collapsed.
......@@ -13,6 +13,11 @@ import (
"github.com/ginuerzh/gost"
)
const (
defaultMaxFails = 1
defaultFailTimeout = 30 * time.Second
)
type peerConfig struct {
Strategy string `json:"strategy"`
MaxFails int `json:"max_fails"`
......@@ -39,10 +44,10 @@ func parsePeerConfig(cfg string, group *gost.NodeGroup, baseNodes []gost.Node) *
func (cfg *peerConfig) Validate() {
if cfg.MaxFails <= 0 {
cfg.MaxFails = 1
cfg.MaxFails = defaultMaxFails
}
if cfg.FailTimeout <= 0 {
cfg.FailTimeout = 30 // seconds
cfg.FailTimeout = defaultFailTimeout // seconds
}
}
......@@ -53,20 +58,22 @@ func (cfg *peerConfig) Reload(r io.Reader) error {
cfg.Validate()
group := cfg.group
strategy := cfg.Strategy
if len(cfg.baseNodes) > 0 {
// overwrite the strategry in the peer config if `strategy` param exists.
if s := cfg.baseNodes[0].Get("strategy"); s != "" {
strategy = s
/*
strategy := cfg.Strategy
if len(cfg.baseNodes) > 0 {
// overwrite the strategry in the peer config if `strategy` param exists.
if s := cfg.baseNodes[0].Get("strategy"); s != "" {
strategy = s
}
}
}
*/
group.SetSelector(
nil,
gost.WithFilter(&gost.FailFilter{
MaxFails: cfg.MaxFails,
FailTimeout: time.Duration(cfg.FailTimeout) * time.Second,
FailTimeout: cfg.FailTimeout,
}),
gost.WithStrategy(parseStrategy(strategy)),
gost.WithStrategy(parseStrategy(cfg.Strategy)),
)
gNodes := cfg.baseNodes
......
This diff is collapsed.
......@@ -16,8 +16,11 @@ type Reloader interface {
// PeriodReload reloads the config periodically according to the period of the reloader.
func PeriodReload(r Reloader, configFile string) error {
var lastMod time.Time
if configFile == "" {
return nil
}
var lastMod time.Time
for {
f, err := os.Open(configFile)
if err != nil {
......@@ -32,7 +35,9 @@ func PeriodReload(r Reloader, configFile string) error {
mt := finfo.ModTime()
if !mt.Equal(lastMod) {
log.Log("[reload]", configFile)
r.Reload(f)
if err := r.Reload(f); err != nil {
log.Logf("[reload] %s: %s", configFile, err)
}
lastMod = mt
}
f.Close()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment