Skip to content

Commit

Permalink
tune context passing
Browse files Browse the repository at this point in the history
  • Loading branch information
vapopov committed Jul 18, 2024
1 parent a303a8b commit 10042fa
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
3 changes: 2 additions & 1 deletion lib/config/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
package config

import (
"context"
"crypto/x509"
"errors"
"io"
Expand Down Expand Up @@ -798,7 +799,7 @@ func applyLogConfig(loggerConfig Log, cfg *servicecfg.Config) error {
return trace.Wrap(err, "failed to init the log file shared writer")
}
w = logutils.NewWriterFinalizer[*logutils.FileSharedWriter](sharedWriter)
if err := sharedWriter.RunWatcherReopen(); err != nil {
if err := sharedWriter.RunWatcherReopen(context.Background()); err != nil {
return trace.Wrap(err)
}
}
Expand Down
12 changes: 6 additions & 6 deletions lib/utils/log/file_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,19 +106,19 @@ func (s *FileSharedWriter) Reopen() error {
}

// RunWatcherReopen runs a filesystem watcher for rename/remove events to reopen the log.
func (s *FileSharedWriter) RunWatcherReopen() error {
func (s *FileSharedWriter) RunWatcherReopen(ctx context.Context) error {
s.lock.Lock()
defer s.lock.Unlock()
if s.closed {
return trace.Wrap(ErrFileSharedWriterClosed)
}

return s.runWatcherFunc(s.Reopen)
return s.runWatcherFunc(ctx, s.Reopen)
}

// runWatcherFunc spawns goroutine with the watcher loop to consume events of renaming
// or removing the log file to trigger the action function when event appeared.
func (s *FileSharedWriter) runWatcherFunc(action func() error) error {
func (s *FileSharedWriter) runWatcherFunc(ctx context.Context, action func() error) error {
go func() {
for {
select {
Expand All @@ -127,17 +127,17 @@ func (s *FileSharedWriter) runWatcherFunc(action func() error) error {
return
}
if s.logFileName == event.Name && (event.Has(fsnotify.Rename) || event.Has(fsnotify.Remove)) {
slog.DebugContext(context.Background(), "Log file was moved/removed", "file", event.Name)
slog.DebugContext(ctx, "Log file was moved/removed", "file", event.Name)
if err := action(); err != nil {
slog.ErrorContext(context.Background(), "Failed to reopen file", "error", err, "file", event.Name)
slog.ErrorContext(ctx, "Failed to reopen file", "error", err, "file", event.Name)
continue
}
}
case err, ok := <-s.watcher.Errors:
if !ok {
return
}
slog.ErrorContext(context.Background(), "Error received on logger watcher", "error", err)
slog.ErrorContext(ctx, "Error received on logger watcher", "error", err)
}
}
}()
Expand Down
9 changes: 5 additions & 4 deletions lib/utils/log/file_writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package log

import (
"context"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -46,7 +47,7 @@ func TestFileSharedWriterNotify(t *testing.T) {
})

signal := make(chan struct{})
err = logWriter.runWatcherFunc(func() error {
err = logWriter.runWatcherFunc(context.Background(), func() error {
err := logWriter.Reopen()
signal <- struct{}{}
return err
Expand Down Expand Up @@ -100,7 +101,7 @@ func TestFileSharedWriterFinalizer(t *testing.T) {
firstLogWriter, err := NewFileSharedWriter(logFileName, testFileFlag, testFileMode)
require.NoError(t, err, "failed to init the file shared writer")

err = firstLogWriter.runWatcherFunc(func() error {
err = firstLogWriter.runWatcherFunc(context.Background(), func() error {
firstWatcherTriggered = true
return nil
})
Expand All @@ -117,7 +118,7 @@ func TestFileSharedWriterFinalizer(t *testing.T) {
require.NoError(t, err, "failed to init the file shared writer")

signal := make(chan struct{})
err = secondLogWriter.runWatcherFunc(func() error {
err = secondLogWriter.runWatcherFunc(context.Background(), func() error {
err := secondLogWriter.Reopen()
signal <- struct{}{}
return err
Expand All @@ -144,7 +145,7 @@ func TestFileSharedWriterFinalizer(t *testing.T) {

// Check that we receive the error if we are going to try to run watcher
// again for closed one.
err = firstLogWriter.RunWatcherReopen()
err = firstLogWriter.RunWatcherReopen(context.Background())
require.ErrorIs(t, err, ErrFileSharedWriterClosed)

// First file shared writer must be already closed and produce error after
Expand Down

0 comments on commit 10042fa

Please sign in to comment.