Commit 5e0e08d5 authored by ginuerzh's avatar ginuerzh

live reloading for base config

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