aboutsummaryrefslogtreecommitdiff
path: root/backend/logs.go
diff options
context:
space:
mode:
authorAnhgelus Morhtuuzh <william@herges.fr>2025-10-05 23:21:55 +0200
committerAnhgelus Morhtuuzh <william@herges.fr>2025-10-05 23:21:55 +0200
commitda56ce47d2e275db58e429a3d8ab85290541ca6f (patch)
treeb676385fd216202831c285471874b3ee8a57e112 /backend/logs.go
parent4e5ab025f03004d9cc5ecce53371683766495ac6 (diff)
fix(backend): concurrent write in map
Diffstat (limited to 'backend/logs.go')
-rw-r--r--backend/logs.go9
1 files changed, 6 insertions, 3 deletions
diff --git a/backend/logs.go b/backend/logs.go
index 2b009c1..7a6db3e 100644
--- a/backend/logs.go
+++ b/backend/logs.go
@@ -67,6 +67,7 @@ func LoadLogs(cfg *Config) bool {
func readLogDir(path string, dir []os.DirEntry) error {
var wg sync.WaitGroup
+ var mu sync.Mutex
for _, d := range dir {
p := filepath.Join(path, d.Name())
if d.IsDir() {
@@ -92,7 +93,7 @@ func readLogDir(path string, dir []os.DirEntry) error {
wg.Add(1)
go func(p string, d os.DirEntry) {
defer wg.Done()
- ok = parseLog(dd, slug, strings.TrimSuffix(d.Name(), ".md"))
+ ok = parseLog(dd, &mu, slug, strings.TrimSuffix(d.Name(), ".md"))
if ok {
slog.Debug("log parsed", "path", p)
} else {
@@ -130,7 +131,7 @@ func handleLog(w http.ResponseWriter, r *http.Request) {
if !ok {
d = new(logData)
d.data = new(data)
- if ok = parseLog(d, path, slug); !ok {
+ if ok = parseLog(d, new(sync.Mutex), path, slug); !ok {
notFound(w, r)
return
}
@@ -138,7 +139,7 @@ func handleLog(w http.ResponseWriter, r *http.Request) {
d.handleGeneric(w, r, "log", d)
}
-func parseLog(d *logData, path, slug string) bool {
+func parseLog(d *logData, mu *sync.Mutex, path, slug string) bool {
d.Article = true
d.title = slug
d.Slug = slug
@@ -154,7 +155,9 @@ func parseLog(d *logData, path, slug string) bool {
if !ok {
return false
}
+ mu.Lock()
logs[path] = d
+ mu.Unlock()
return true
}