move reload to github.com/go-gost/reload

This commit is contained in:
ginuerzh 2020-03-25 21:06:58 +08:00
parent fd0d28e815
commit de4a896004
6 changed files with 13 additions and 72 deletions

View File

@ -13,6 +13,7 @@ import (
"strings"
"github.com/ginuerzh/gost"
"github.com/go-gost/reload"
)
var (
@ -130,7 +131,7 @@ func parseAuthenticator(s string) (gost.Authenticator, error) {
au := gost.NewLocalAuthenticator(nil)
au.Reload(f)
go gost.PeriodReload(au, s)
go reload.PeriodReload(au, s)
return au, nil
}
@ -200,7 +201,7 @@ func parseBypass(s string) *gost.Bypass {
bp := gost.NewBypass(reversed)
bp.Reload(f)
go gost.PeriodReload(bp, s)
go reload.PeriodReload(bp, s)
return bp
}
@ -257,7 +258,7 @@ func parseResolver(cfg string) gost.Resolver {
resolver := gost.NewResolver(0)
resolver.Reload(f)
go gost.PeriodReload(resolver, cfg)
go reload.PeriodReload(resolver, cfg)
return resolver
}
@ -272,7 +273,7 @@ func parseHosts(s string) *gost.Hosts {
hosts := gost.NewHosts()
hosts.Reload(f)
go gost.PeriodReload(hosts, s)
go reload.PeriodReload(hosts, s)
return hosts
}

View File

@ -12,6 +12,7 @@ import (
"github.com/ginuerzh/gost"
"github.com/go-gost/log"
"github.com/go-gost/reload"
)
type stringList []string
@ -76,7 +77,7 @@ func (r *route) parseChain() (*gost.Chain, error) {
peerCfg.Reload(f)
f.Close()
go gost.PeriodReload(peerCfg, cfg)
go reload.PeriodReload(peerCfg, cfg)
}
chain.AddNodeGroup(ngroup)

1
go.mod
View File

@ -18,6 +18,7 @@ require (
github.com/go-gost/bpool v1.0.0
github.com/go-gost/log v1.0.0
github.com/go-gost/relay v0.1.0
github.com/go-gost/reload v1.0.0
github.com/gobwas/glob v0.2.3
github.com/golang/mock v1.2.0 // indirect
github.com/google/gopacket v1.1.17 // indirect

2
go.sum
View File

@ -35,6 +35,8 @@ github.com/go-gost/log v1.0.0 h1:maSjjMvQqLSQYb0Ta5nJTdlRI+aiLMt6WIBYVxajJgs=
github.com/go-gost/log v1.0.0/go.mod h1:FCOaaJQ7moHTlLxYk7dsFewlS68U9A1GG3OR+yXkF6s=
github.com/go-gost/relay v0.1.0 h1:UOf2YwAzzaUjY5mdpMuLfSw0vz62iIFYk7oJQkuhlGw=
github.com/go-gost/relay v0.1.0/go.mod h1:YFCpddLOFE3NlIkeDWRdEs8gL/GFsqXdtaf8SV5v4YQ=
github.com/go-gost/reload v1.0.0 h1:dcC3YwHXLlvow53UKH8gKhyguX0atkBV+/y7fHIGpCU=
github.com/go-gost/reload v1.0.0/go.mod h1:IVKerwSiS/YATAMeKRnmv3NhnIwFeLfXRTAyMsySnUM=
github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y=
github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8=
github.com/golang/mock v1.2.0 h1:28o5sBqPkBsMGnC6b4MvE2TzSr5/AT4c/1fLqVGIwlk=

View File

@ -1,65 +0,0 @@
package gost
import (
"io"
"os"
"time"
"github.com/go-gost/log"
)
// Reloader is the interface for objects that support live reloading.
type Reloader interface {
Reload(r io.Reader) error
Period() time.Duration
}
// Stoppable is the interface that indicates a Reloader can be stopped.
type Stoppable interface {
Stop()
Stopped() bool
}
// PeriodReload reloads the config configFile periodically according to the period of the Reloader r.
func PeriodReload(r Reloader, configFile string) error {
if r == nil || configFile == "" {
return nil
}
var lastMod time.Time
for {
if r.Period() < 0 {
log.Log("[reload] stopped:", configFile)
return nil
}
f, err := os.Open(configFile)
if err != nil {
return err
}
mt := lastMod
if finfo, err := f.Stat(); err == nil {
mt = finfo.ModTime()
}
if !lastMod.IsZero() && !mt.Equal(lastMod) {
log.Log("[reload]", configFile)
if err := r.Reload(f); err != nil {
log.Logf("[reload] %s: %s", configFile, err)
}
}
f.Close()
lastMod = mt
period := r.Period()
if period == 0 {
log.Log("[reload] disabled:", configFile)
return nil
}
if period < time.Second {
period = time.Second
}
<-time.After(period)
}
}

View File

@ -17,6 +17,7 @@ import (
"time"
"github.com/go-gost/log"
"github.com/go-gost/reload"
"github.com/miekg/dns"
)
@ -180,8 +181,8 @@ type Resolver interface {
// ReloadResolver is resolover that support live reloading.
type ReloadResolver interface {
Resolver
Reloader
Stoppable
reload.Reloader
reload.Stoppable
}
type resolver struct {