Skip to content

Commit

Permalink
Fix watch handling on stat errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Schneider authored Apr 13, 2021
2 parents 61b2a30 + 12527b9 commit 280a785
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
21 changes: 17 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func realmain(arguments []string) int {
errCh <- execCmd.Execute(args, confFile, logger)
}()

reloadCh := watchConfigFile(confFile.Filename, logger)
reloadCh := watchConfigFile(confFile.Filename, logger, flags.FileWatchRetries, flags.FileWatchRetryDelay)
for {
select {
case err = <-errCh:
Expand All @@ -151,7 +151,10 @@ func realmain(arguments []string) int {
return 1
}
return 0
case <-reloadCh:
case _, more := <-reloadCh:
if !more {
return 1
}
errRetries = 0 // reset
logger.Info("reloading couper configuration")

Expand Down Expand Up @@ -211,17 +214,26 @@ func newLogger(format string, pretty bool) *logrus.Entry {
return logger.WithField("type", logConf.TypeFieldKey).WithFields(fields)
}

func watchConfigFile(name string, logger logrus.FieldLogger) <-chan struct{} {
func watchConfigFile(name string, logger logrus.FieldLogger, maxRetries int, retryDelay time.Duration) <-chan struct{} {
reloadCh := make(chan struct{})
go func() {
ticker := time.NewTicker(time.Second / 4)
defer ticker.Stop()
var lastChange time.Time
var errors int
for {
<-ticker.C
fileInfo, fileErr := os.Stat(name)
if fileErr != nil {
logger.WithFields(fields).Error(fileErr)
errors++
if errors >= maxRetries {
logger.Errorf("giving up after %d retries: %v", errors, fileErr)
close(reloadCh)
return
} else {
logger.WithFields(fields).Error(fileErr)
}
time.Sleep(retryDelay)
continue
}

Expand All @@ -234,6 +246,7 @@ func watchConfigFile(name string, logger logrus.FieldLogger) <-chan struct{} {
reloadCh <- struct{}{}
}
lastChange = fileInfo.ModTime()
errors = 0
}
}()
return reloadCh
Expand Down
17 changes: 13 additions & 4 deletions server/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,12 @@ func (s *HTTPServer) Listen() error {
go s.listenForCtx()

go func() {
if err := s.srv.Serve(ln); err != nil {
s.log.Errorf("%s: %v", ln.Addr().String(), err.Error())
if serveErr := s.srv.Serve(ln); serveErr != nil {
if serveErr == http.ErrServerClosed {
s.log.Infof("%v: %s", serveErr, ln.Addr().String())
} else {
s.log.Errorf("%s: %v", ln.Addr().String(), serveErr)
}
}
}()
return nil
Expand All @@ -161,8 +165,13 @@ func (s *HTTPServer) listenForCtx() {
close(s.shutdownCh)

time.Sleep(s.timings.ShutdownDelay)
ctx, cancel := context.WithTimeout(context.Background(), s.timings.ShutdownTimeout)
defer cancel()
ctx := context.Background()
if s.timings.ShutdownTimeout > 0 {
c, cancel := context.WithTimeout(ctx, s.timings.ShutdownTimeout)
defer cancel()
ctx = c
}

if err := s.srv.Shutdown(ctx); err != nil {
s.log.WithFields(logFields).Error(err)
}
Expand Down

0 comments on commit 280a785

Please sign in to comment.