1
0
mirror of https://github.com/gofiber/fiber.git synced 2025-02-21 19:53:19 +00:00

🚀 use atomic.Value instead of sync.Mutex

To improve the performance and precision
This commit is contained in:
kiyon 2020-07-29 14:42:17 +08:00
parent 616ebec453
commit 76c67ca05a

View File

@ -7,7 +7,7 @@ import (
"os"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
fiber "github.com/gofiber/fiber"
@ -207,20 +207,18 @@ func logger(config LoggerConfig) fiber.Handler {
}
}
// Middleware settings
var mutex sync.RWMutex
var tmpl loggerTemplate
tmpl.new(config.Format, "${", "}")
timestamp := nowTimeString(config.timeZoneLocation, config.TimeFormat)
// Update date/time every second in a separate go routine
var timestamp atomic.Value
timestamp.Store(nowTimeString(config.timeZoneLocation, config.TimeFormat))
// Update date/time every millisecond in a separate go routine
if strings.Contains(config.Format, "${time}") {
go func() {
for {
mutex.Lock()
timestamp = nowTimeString(config.timeZoneLocation, config.TimeFormat)
mutex.Unlock()
time.Sleep(500 * time.Millisecond)
time.Sleep(time.Millisecond)
timestamp.Store(nowTimeString(config.timeZoneLocation, config.TimeFormat))
}
}()
}
@ -243,9 +241,7 @@ func logger(config LoggerConfig) fiber.Handler {
_, err := tmpl.executeFunc(buf, func(w io.Writer, tag string) (int, error) {
switch tag {
case LoggerTagTime:
mutex.RLock()
defer mutex.RUnlock()
return buf.WriteString(timestamp)
return buf.WriteString(timestamp.Load().(string))
case LoggerTagReferer:
return buf.WriteString(c.Get(fiber.HeaderReferer))
case LoggerTagProtocol: