From 1d22bcfed921688f001b7ac5e0f139047af4cb22 Mon Sep 17 00:00:00 2001 From: yusing Date: Tue, 29 Apr 2025 07:33:51 +0800 Subject: [PATCH] fix(access_log): file size calculation --- internal/logging/accesslog/back_scanner.go | 13 ++----------- internal/logging/accesslog/file_logger.go | 8 ++++++++ internal/logging/accesslog/rotate.go | 12 +++++++++--- 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/internal/logging/accesslog/back_scanner.go b/internal/logging/accesslog/back_scanner.go index 2e93ac2..9b1eb61 100644 --- a/internal/logging/accesslog/back_scanner.go +++ b/internal/logging/accesslog/back_scanner.go @@ -26,12 +26,8 @@ type BackScanner struct { // NewBackScanner creates a new Scanner to read the file backward. // chunkSize determines the size of each read chunk from the end of the file. -func NewBackScanner(file ReaderAtSeeker, chunkSize int) *BackScanner { - size, err := file.Seek(0, io.SeekEnd) - if err != nil { - return &BackScanner{err: err} - } - return newBackScanner(file, size, make([]byte, chunkSize)) +func NewBackScanner(file ReaderAtSeeker, fileSize int64, chunkSize int) *BackScanner { + return newBackScanner(file, fileSize, make([]byte, chunkSize)) } func newBackScanner(file ReaderAtSeeker, fileSize int64, buf []byte) *BackScanner { @@ -111,11 +107,6 @@ func (s *BackScanner) Bytes() []byte { return s.line } -// FileSize returns the size of the file. -func (s *BackScanner) FileSize() int64 { - return s.size -} - // Err returns the first non-EOF error encountered by the scanner. func (s *BackScanner) Err() error { return s.err diff --git a/internal/logging/accesslog/file_logger.go b/internal/logging/accesslog/file_logger.go index f887271..7a85662 100644 --- a/internal/logging/accesslog/file_logger.go +++ b/internal/logging/accesslog/file_logger.go @@ -72,6 +72,14 @@ func (f *File) Seek(offset int64, whence int) (int64, error) { return f.f.Seek(offset, whence) } +func (f *File) Size() (int64, error) { + stat, err := f.f.Stat() + if err != nil { + return 0, err + } + return stat.Size(), nil +} + func (f *File) Truncate(size int64) error { return f.f.Truncate(size) } diff --git a/internal/logging/accesslog/rotate.go b/internal/logging/accesslog/rotate.go index b004472..e447f13 100644 --- a/internal/logging/accesslog/rotate.go +++ b/internal/logging/accesslog/rotate.go @@ -16,6 +16,7 @@ type supportRotate interface { io.ReaderAt io.WriterAt Truncate(size int64) error + Size() (int64, error) } type RotateResult struct { @@ -96,9 +97,14 @@ func rotateLogFileByPolicy(file supportRotate, config *Retention) (result *Rotat return nil, nil // should not happen } - s := NewBackScanner(file, defaultChunkSize) + fileSize, err := file.Size() + if err != nil { + return nil, err + } + + s := NewBackScanner(file, fileSize, defaultChunkSize) result = &RotateResult{ - OriginalSize: s.FileSize(), + OriginalSize: fileSize, } // nothing to rotate, return the nothing @@ -201,7 +207,7 @@ func rotateLogFileByPolicy(file supportRotate, config *Retention) (result *Rotat // // Invalid lines will not be detected and included in the result. func rotateLogFileBySize(file supportRotate, config *Retention) (result *RotateResult, err error) { - filesize, err := file.Seek(0, io.SeekEnd) + filesize, err := file.Size() if err != nil { return nil, err }