fix: high cpu usage

This commit is contained in:
yusing
2025-01-22 05:44:04 +08:00
parent 3781bb93e1
commit b984386bab
12 changed files with 82 additions and 44 deletions

View File

@@ -19,8 +19,8 @@ type (
io AccessLogIO
buf bytes.Buffer // buffer for non-flushed log
bufMu sync.Mutex // protect buf
bufPool sync.Pool // buffer pool for formatting a single log line
bufMu sync.RWMutex
bufPool sync.Pool // buffer pool for formatting a single log line
flushThreshold int
@@ -123,10 +123,10 @@ func (l *AccessLogger) Flush(force bool) {
return
}
if force || l.buf.Len() >= l.flushThreshold {
l.bufMu.Lock()
l.bufMu.RLock()
l.write(l.buf.Bytes())
l.buf.Reset()
l.bufMu.Unlock()
l.bufMu.RUnlock()
}
}

View File

@@ -19,19 +19,12 @@ import (
const errPagesBasePath = common.ErrorPagesBasePath
var (
setupMu sync.Mutex
setupOnce sync.Once
dirWatcher W.Watcher
fileContentMap = F.NewMapOf[string, []byte]()
)
func setup() {
setupMu.Lock()
defer setupMu.Unlock()
if dirWatcher != nil {
return
}
t := task.RootTask("error_page", false)
dirWatcher = W.NewDirectoryWatcher(t, errPagesBasePath)
loadContent()
@@ -39,7 +32,7 @@ func setup() {
}
func GetStaticFile(filename string) ([]byte, bool) {
setup()
setupOnce.Do(setup)
return fileContentMap.Load(filename)
}

View File

@@ -49,8 +49,6 @@ func (rl *rateLimiter) newLimiter() *rate.Limiter {
}
func (rl *rateLimiter) limit(w http.ResponseWriter, r *http.Request) bool {
rl.mu.Lock()
host, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
rl.AddTracef("unable to parse remote address %s", r.RemoteAddr)
@@ -58,12 +56,12 @@ func (rl *rateLimiter) limit(w http.ResponseWriter, r *http.Request) bool {
return false
}
rl.mu.Lock()
limiter, ok := rl.requestMap[host]
if !ok {
limiter = rl.newLimiter()
rl.requestMap[host] = limiter
}
rl.mu.Unlock()
if limiter.Allow() {