Skip to content

Commit

Permalink
Refactor relay config reloading logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Ehco1996 committed Jan 18, 2024
1 parent 50087c7 commit 85b3c88
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
38 changes: 18 additions & 20 deletions cmd/ehco/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,8 @@ func startRelayServers(ctx context.Context, cfg *config.Config) error {
}
go startOneRelay(r, relayM, errCH)
}
if ConfigPath != "" {
if cfg.PATH != "" && cfg.ReloadInterval > 0 {
cmdLogger.Infof("Start to watch relay config %s ", ConfigPath)
go watchAndReloadRelayConfig(ctx, cfg, relayM, errCH)
}

Expand All @@ -272,17 +273,16 @@ func startRelayServers(ctx context.Context, cfg *config.Config) error {
}
}

func watchAndReloadRelayConfig(ctx context.Context, curCfg *config.Config, relayM *sync.Map, errCh chan error) {
cmdLogger.Infof("Start to watch relay config %s ", ConfigPath)
func watchAndReloadRelayConfig(ctx context.Context, cfg *config.Config, relayM *sync.Map, errCh chan error) {
reladRelayF := func() error {
newCfg, err := loadConfig()
if err != nil {
// load new config
if err := cfg.LoadConfig(); err != nil {
cmdLogger.Errorf("Reloading Realy Conf meet error: %s ", err)
return err
}
var newRelayAddrList []string
for idx := range newCfg.RelayConfigs {
r, err := relay.NewRelay(newCfg.RelayConfigs[idx])
for idx := range cfg.RelayConfigs {
r, err := relay.NewRelay(cfg.RelayConfigs[idx])
if err != nil {
cmdLogger.Errorf("reload new relay failed err=%s", err.Error())
return err
Expand Down Expand Up @@ -333,20 +333,18 @@ func watchAndReloadRelayConfig(ctx context.Context, curCfg *config.Config, relay
}()

// ticker to reload config
if curCfg.ReloadInterval > 0 {
ticker := time.NewTicker(time.Second * time.Duration(curCfg.ReloadInterval))
defer ticker.Stop()
go func() {
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
reloadCH <- struct{}{}
}
ticker := time.NewTicker(time.Second * time.Duration(cfg.ReloadInterval))
defer ticker.Stop()
go func() {
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
reloadCH <- struct{}{}
}
}()
}
}
}()

for {
select {
Expand Down
7 changes: 7 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ type Config struct {
SyncTrafficEndPoint string `json:"sync_traffic_endpoint"`

L *zap.SugaredLogger

lastLoadTime time.Time
}

func NewConfig(path string) *Config {
Expand All @@ -88,6 +90,11 @@ func (c *Config) NeedSyncUserFromServer() bool {
}

func (c *Config) LoadConfig() error {
if c.ReloadInterval > 0 && time.Since(c.lastLoadTime).Seconds() < float64(c.ReloadInterval) {
c.L.Debugf("Skip Load Config, last load time: %s", c.lastLoadTime)
return nil
}
c.lastLoadTime = time.Now()
if c.NeedSyncUserFromServer() {
if err := c.readFromHttp(); err != nil {
return err
Expand Down

0 comments on commit 85b3c88

Please sign in to comment.