fix: Lock block and system log out error

This commit is contained in:
Roi Feng
2025-04-28 21:37:49 -04:00
parent c1efa9a0ca
commit c0524c56e7
4 changed files with 14 additions and 15 deletions

View File

@ -2,7 +2,7 @@ port: "25555"
web_path: "/torii"
rule_path: "/www/server_torii/config/rules"
error_page: "/www/server_torii/config/error_page"
log_path: "/www/server_torii/log/access.log"
log_path: "/www/server_torii/log/"
node_name: "Server Torii"
connecting_host_headers:
- "Torii-Real-Host"

View File

@ -1,5 +1,7 @@
package dataType
import "server_torii/internal/utils"
type UserRequest struct {
RemoteIP string
Uri string
@ -34,4 +36,5 @@ type HTTPFloodRule struct {
type SharedMemory struct {
HTTPFloodSpeedLimitCounter *Counter
HTTPFloodSameURILimitCounter *Counter
Logger *utils.LogxManager
}

View File

@ -14,9 +14,8 @@ import (
type LogxManager struct {
basePath string
access *os.File
loggers map[string]*zap.Logger
mu sync.Mutex
mu sync.RWMutex
}
func NewManager(base string) *LogxManager {
@ -25,24 +24,18 @@ func NewManager(base string) *LogxManager {
if err := os.MkdirAll(m.basePath, 0744); err != nil {
log.Printf("failed to create base log dir %s: %v", m.basePath, err)
}
accessPath := filepath.Join(m.basePath, "access.log")
f, err := os.OpenFile(accessPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
panic(fmt.Sprintf("failed to open access.log: %v", err))
}
m.access = f
log.SetOutput(f)
return m
}
func (m *LogxManager) getLogger(host string) *zap.Logger {
m.mu.Lock()
defer m.mu.Unlock()
m.mu.RLock()
if lg, ok := m.loggers[host]; ok {
m.mu.RUnlock()
return lg
}
m.mu.RUnlock()
m.mu.Lock()
defer m.mu.Unlock()
dir := filepath.Join(m.basePath, host)
if err := os.MkdirAll(dir, 0744); err != nil {
log.Printf("failed to create log dir %s: %v", dir, err)

View File

@ -5,6 +5,7 @@ import (
"log"
"os"
"os/signal"
"path/filepath"
"runtime"
"server_torii/internal/config"
"server_torii/internal/dataType"
@ -38,7 +39,8 @@ func main() {
log.Printf("Ready to start server on port %s", cfg.Port)
//set log file
logFile, err := os.OpenFile(cfg.LogPath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
defaultLogPath := filepath.Join(cfg.LogPath + "server_torii.log")
logFile, err := os.OpenFile(defaultLogPath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Fatalf("Failed to open log file: %v", err)
}
@ -54,6 +56,7 @@ func main() {
sharedMem := &dataType.SharedMemory{
HTTPFloodSpeedLimitCounter: dataType.NewCounter(max(runtime.NumCPU()*8, 16), utils.FindMaxRateTime(ruleSet.HTTPFloodRule.HTTPFloodSpeedLimit)),
HTTPFloodSameURILimitCounter: dataType.NewCounter(max(runtime.NumCPU()*8, 16), utils.FindMaxRateTime(ruleSet.HTTPFloodRule.HTTPFloodSameURILimit)),
Logger: utils.NewManager(cfg.LogPath),
}
//GC