Skip to content

Commit

Permalink
feat(htpasswd): rewrite htpasswd watcher not to store context
Browse files Browse the repository at this point in the history
Signed-off-by: Vladimir Ermakov <[email protected]>
  • Loading branch information
vooon committed Feb 8, 2025
1 parent d73710d commit 2f38114
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 48 deletions.
8 changes: 0 additions & 8 deletions pkg/api/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,10 +301,6 @@ func (c *Controller) Init() error {
}
}

if err := c.HTPasswdWatcher.Start(); err != nil {
return err
}

return nil
}

Expand Down Expand Up @@ -463,10 +459,6 @@ func (c *Controller) Shutdown() {
ctx := context.Background()
_ = c.Server.Shutdown(ctx)
}

if c.HTPasswdWatcher != nil {
_ = c.HTPasswdWatcher.Stop()
}
}

// Will stop scheduler and wait for all tasks to finish their work.
Expand Down
73 changes: 33 additions & 40 deletions pkg/api/htpasswd.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,11 @@ type HTPasswdWatcher struct {
htp *HTPasswd
filePath string
watcher *fsnotify.Watcher
ctx context.Context //nolint: containedctx
ccancel context.CancelFunc
cancel context.CancelFunc
log log.Logger
}

// NewHTPasswdWatcher create and start watcher

Check failure on line 112 in pkg/api/htpasswd.go

View workflow job for this annotation

GitHub Actions / lint

Comment should end in a period (godot)
func NewHTPasswdWatcher(htp *HTPasswd, filePath string) (*HTPasswdWatcher, error) {
watcher, err := fsnotify.NewWatcher()
if err != nil {
Expand All @@ -130,11 +130,38 @@ func NewHTPasswdWatcher(htp *HTPasswd, filePath string) (*HTPasswdWatcher, error
htp: htp,
filePath: filePath,
watcher: watcher,
ctx: ctx,
ccancel: cancel,
cancel: cancel,
log: htp.log,
}

go func() {
defer ret.watcher.Close() //nolint: errcheck

for {
select {
case ev := <-ret.watcher.Events:
if ev.Op != fsnotify.Write {
continue
}

ret.log.Info().Str("htpasswd-file", ret.filePath).Msg("htpasswd file changed, trying to reload config")

err := ret.htp.Reload(ret.filePath)
if err != nil {
ret.log.Warn().Err(err).Str("htpasswd-file", ret.filePath).Msg("failed to reload file")
}

case err := <-ret.watcher.Errors:
ret.log.Panic().Err(err).Str("htpasswd-file", ret.filePath).Msg("fsnotfy error while watching config")

case <-ctx.Done():
ret.log.Debug().Msg("htpasswd watcher terminating...")

return
}
}
}()

return ret, nil
}

Expand Down Expand Up @@ -164,42 +191,8 @@ func (s *HTPasswdWatcher) ChangeFile(filePath string) error {
return s.htp.Reload(filePath)
}

// Start watcher. Can only be run once.
func (s *HTPasswdWatcher) Start() error {
go func() {
defer s.watcher.Close() //nolint: errcheck

for {
select {
case ev := <-s.watcher.Events:
if ev.Op != fsnotify.Write {
continue
}

s.log.Info().Str("htpasswd-file", s.filePath).Msg("htpasswd file changed, trying to reload config")

err := s.htp.Reload(s.filePath)
if err != nil {
s.log.Warn().Err(err).Str("htpasswd-file", s.filePath).Msg("failed to reload file")
}

case err := <-s.watcher.Errors:
s.log.Panic().Err(err).Str("htpasswd-file", s.filePath).Msg("fsnotfy error while watching config")

case <-s.ctx.Done():
s.log.Debug().Msg("htpasswd watcher terminating...")

return
}
}
}()

return nil
}

// Stop watcher. Can only be run once.
func (s *HTPasswdWatcher) Stop() error {
s.ccancel()
func (s *HTPasswdWatcher) Close() error {
s.cancel()

return nil
}

0 comments on commit 2f38114

Please sign in to comment.