From 03da51db126c74779464d60b96f5d9d117f57c37 Mon Sep 17 00:00:00 2001 From: George King Date: Mon, 16 Sep 2024 03:27:34 +0800 Subject: [PATCH 1/4] refactor: replace logger with default logger --- filestorage.go | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/filestorage.go b/filestorage.go index d3df9cf7..efa43a13 100644 --- a/filestorage.go +++ b/filestorage.go @@ -21,7 +21,6 @@ import ( "fmt" "io" "io/fs" - "log" "os" "path" "path/filepath" @@ -213,7 +212,7 @@ func (s *FileStorage) Lock(ctx context.Context, name string) error { // the previous acquirer either crashed or had some sort of failure that // caused them to be unable to fully acquire or retain the lock, therefore // we should treat it as if the lockfile did not exist - log.Printf("[INFO][%s] %s: Empty lockfile (%v) - likely previous process crashed or storage medium failure; treating as stale", s, filename, err2) + Default.Logger.Sugar().Infof("[INFO][%s] %s: Empty lockfile (%v) - likely previous process crashed or storage medium failure; treating as stale", s, filename, err2) } } else if err2 != nil { return fmt.Errorf("decoding lockfile contents: %w", err2) @@ -235,8 +234,7 @@ func (s *FileStorage) Lock(ctx context.Context, name string) error { // either have potential to cause infinite loops, as in caddyserver/caddy#4448, // or must give up on perfect mutual exclusivity; however, these cases are rare, // so we prefer the simpler solution that avoids infinite loops) - log.Printf("[INFO][%s] Lock for '%s' is stale (created: %s, last update: %s); removing then retrying: %s", - s, name, meta.Created, meta.Updated, filename) + Default.Logger.Sugar().Infof("[%s] Lock for '%s' is stale (created: %s, last update: %s); removing then retrying: %s", s, name, meta.Created, meta.Updated, filename) if err = os.Remove(filename); err != nil { // hopefully we can replace the lock file quickly! if !errors.Is(err, fs.ErrNotExist) { return fmt.Errorf("unable to delete stale lockfile; deadlocked: %w", err) @@ -311,7 +309,7 @@ func keepLockfileFresh(filename string) { if err := recover(); err != nil { buf := make([]byte, stackTraceBufferSize) buf = buf[:runtime.Stack(buf, false)] - log.Printf("panic: active locking: %v\n%s", err, buf) + Default.Logger.Sugar().Panicf("active locking: %v\n%s", err, buf) } }() @@ -319,7 +317,7 @@ func keepLockfileFresh(filename string) { time.Sleep(lockFreshnessInterval) done, err := updateLockfileFreshness(filename) if err != nil { - log.Printf("[ERROR] Keeping lock file fresh: %v - terminating lock maintenance (lockfile: %s)", err, filename) + Default.Logger.Sugar().Errorf("Keeping lock file fresh: %v - terminating lock maintenance (lockfile: %s)", err, filename) return } if done { From b36d209e2b7b925b7781c86e55cf24966279ecc9 Mon Sep 17 00:00:00 2001 From: George King Date: Mon, 16 Sep 2024 03:33:06 +0800 Subject: [PATCH 2/4] typo: fix typo --- filestorage.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/filestorage.go b/filestorage.go index efa43a13..8e5f7e56 100644 --- a/filestorage.go +++ b/filestorage.go @@ -212,7 +212,7 @@ func (s *FileStorage) Lock(ctx context.Context, name string) error { // the previous acquirer either crashed or had some sort of failure that // caused them to be unable to fully acquire or retain the lock, therefore // we should treat it as if the lockfile did not exist - Default.Logger.Sugar().Infof("[INFO][%s] %s: Empty lockfile (%v) - likely previous process crashed or storage medium failure; treating as stale", s, filename, err2) + Default.Logger.Sugar().Infof("[%s] %s: Empty lockfile (%v) - likely previous process crashed or storage medium failure; treating as stale", s, filename, err2) } } else if err2 != nil { return fmt.Errorf("decoding lockfile contents: %w", err2) From 44d7140dbae88627a4aa07d0539624492bf9f2c1 Mon Sep 17 00:00:00 2001 From: George Date: Tue, 17 Sep 2024 07:59:08 +0800 Subject: [PATCH 3/4] fix: avoid panic --- filestorage.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/filestorage.go b/filestorage.go index 8e5f7e56..c099c6a2 100644 --- a/filestorage.go +++ b/filestorage.go @@ -309,7 +309,7 @@ func keepLockfileFresh(filename string) { if err := recover(); err != nil { buf := make([]byte, stackTraceBufferSize) buf = buf[:runtime.Stack(buf, false)] - Default.Logger.Sugar().Panicf("active locking: %v\n%s", err, buf) + Default.Logger.Sugar().Errorf("active locking: %v\n%s", err, buf) } }() From 78ab6f15f5e8babcbd6bc40e498e9a23550a02a0 Mon Sep 17 00:00:00 2001 From: George Date: Fri, 20 Sep 2024 02:17:28 +0800 Subject: [PATCH 4/4] Update filestorage.go Replace Default.Logger with defaultLogger --- filestorage.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/filestorage.go b/filestorage.go index c099c6a2..78ba2393 100644 --- a/filestorage.go +++ b/filestorage.go @@ -212,7 +212,7 @@ func (s *FileStorage) Lock(ctx context.Context, name string) error { // the previous acquirer either crashed or had some sort of failure that // caused them to be unable to fully acquire or retain the lock, therefore // we should treat it as if the lockfile did not exist - Default.Logger.Sugar().Infof("[%s] %s: Empty lockfile (%v) - likely previous process crashed or storage medium failure; treating as stale", s, filename, err2) + defaultLogger.Sugar().Infof("[%s] %s: Empty lockfile (%v) - likely previous process crashed or storage medium failure; treating as stale", s, filename, err2) } } else if err2 != nil { return fmt.Errorf("decoding lockfile contents: %w", err2) @@ -234,7 +234,7 @@ func (s *FileStorage) Lock(ctx context.Context, name string) error { // either have potential to cause infinite loops, as in caddyserver/caddy#4448, // or must give up on perfect mutual exclusivity; however, these cases are rare, // so we prefer the simpler solution that avoids infinite loops) - Default.Logger.Sugar().Infof("[%s] Lock for '%s' is stale (created: %s, last update: %s); removing then retrying: %s", s, name, meta.Created, meta.Updated, filename) + defaultLogger.Sugar().Infof("[%s] Lock for '%s' is stale (created: %s, last update: %s); removing then retrying: %s", s, name, meta.Created, meta.Updated, filename) if err = os.Remove(filename); err != nil { // hopefully we can replace the lock file quickly! if !errors.Is(err, fs.ErrNotExist) { return fmt.Errorf("unable to delete stale lockfile; deadlocked: %w", err) @@ -309,7 +309,7 @@ func keepLockfileFresh(filename string) { if err := recover(); err != nil { buf := make([]byte, stackTraceBufferSize) buf = buf[:runtime.Stack(buf, false)] - Default.Logger.Sugar().Errorf("active locking: %v\n%s", err, buf) + defaultLogger.Sugar().Errorf("active locking: %v\n%s", err, buf) } }() @@ -317,7 +317,7 @@ func keepLockfileFresh(filename string) { time.Sleep(lockFreshnessInterval) done, err := updateLockfileFreshness(filename) if err != nil { - Default.Logger.Sugar().Errorf("Keeping lock file fresh: %v - terminating lock maintenance (lockfile: %s)", err, filename) + defaultLogger.Sugar().Errorf("Keeping lock file fresh: %v - terminating lock maintenance (lockfile: %s)", err, filename) return } if done {