mirror of
https://github.com/yusing/godoxy.git
synced 2025-05-20 12:42:34 +02:00
fix access logger write on closed file after config reload
This commit is contained in:
parent
b3c47e759f
commit
f906e04581
2 changed files with 43 additions and 9 deletions
|
@ -129,7 +129,6 @@ func (l *AccessLogger) Flush(force bool) {
|
||||||
l.write(l.buf.Bytes())
|
l.write(l.buf.Bytes())
|
||||||
l.buf.Reset()
|
l.buf.Reset()
|
||||||
l.bufMu.Unlock()
|
l.bufMu.Unlock()
|
||||||
logger.Debug().Msg("access log flushed to " + l.io.Name())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -170,5 +169,7 @@ func (l *AccessLogger) write(data []byte) {
|
||||||
l.io.Unlock()
|
l.io.Unlock()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
l.handleErr(err)
|
l.handleErr(err)
|
||||||
|
} else {
|
||||||
|
logger.Debug().Msg("access log flushed to " + l.io.Name())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,36 +3,69 @@ package accesslog
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"path"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/yusing/go-proxy/internal/task"
|
"github.com/yusing/go-proxy/internal/task"
|
||||||
|
"github.com/yusing/go-proxy/internal/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
type File struct {
|
type File struct {
|
||||||
*os.File
|
*os.File
|
||||||
sync.Mutex
|
sync.Mutex
|
||||||
|
|
||||||
|
// os.File.Name() may not equal to key of `openedFiles`.
|
||||||
|
// Store it for later delete from `openedFiles`.
|
||||||
|
path string
|
||||||
|
|
||||||
|
refCount *utils.RefCount
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
openedFiles = make(map[string]AccessLogIO)
|
openedFiles = make(map[string]*File)
|
||||||
openedFilesMu sync.Mutex
|
openedFilesMu sync.Mutex
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewFileAccessLogger(parent task.Parent, cfg *Config) (*AccessLogger, error) {
|
func NewFileAccessLogger(parent task.Parent, cfg *Config) (*AccessLogger, error) {
|
||||||
openedFilesMu.Lock()
|
openedFilesMu.Lock()
|
||||||
|
|
||||||
var io AccessLogIO
|
var file *File
|
||||||
if opened, ok := openedFiles[cfg.Path]; ok {
|
path := path.Clean(cfg.Path)
|
||||||
io = opened
|
if opened, ok := openedFiles[path]; ok {
|
||||||
|
opened.refCount.Add()
|
||||||
|
file = opened
|
||||||
} else {
|
} else {
|
||||||
f, err := os.OpenFile(cfg.Path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644)
|
f, err := os.OpenFile(cfg.Path, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0o644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
openedFilesMu.Unlock()
|
||||||
return nil, fmt.Errorf("access log open error: %w", err)
|
return nil, fmt.Errorf("access log open error: %w", err)
|
||||||
}
|
}
|
||||||
io = &File{File: f}
|
file = &File{File: f, path: path, refCount: utils.NewRefCounter()}
|
||||||
openedFiles[cfg.Path] = io
|
openedFiles[path] = file
|
||||||
|
go file.closeOnZero()
|
||||||
}
|
}
|
||||||
|
|
||||||
openedFilesMu.Unlock()
|
openedFilesMu.Unlock()
|
||||||
return NewAccessLogger(parent, io, cfg), nil
|
return NewAccessLogger(parent, file, cfg), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *File) Close() error {
|
||||||
|
f.refCount.Sub()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *File) closeOnZero() {
|
||||||
|
defer logger.Debug().
|
||||||
|
Str("path", f.path).
|
||||||
|
Msg("access log closed")
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-f.refCount.Zero():
|
||||||
|
openedFilesMu.Lock()
|
||||||
|
delete(openedFiles, f.path)
|
||||||
|
openedFilesMu.Unlock()
|
||||||
|
f.File.Close()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue