Files
skillshare/internal/server/oplog_helper.go
T
Willie 5307b3276e fix(server): use snapshot pattern to eliminate map races and narrow lock scope
Addresses P1 and P2 from code review on the RWMutex commit:

P1 — Stream handlers now snapshot config under RLock before streaming:
- handleDiffStream: clone Targets map to prevent concurrent map panic
- handleCheckStream, handleAuditStream: snapshot source/projectRoot
- handleUpdateStream: snapshot source (per-item Lock already correct)

P2 — All read handlers now use narrow snapshot instead of defer RUnlock:
- RLock → copy needed fields to locals → RUnlock → do work with locals
- Prevents slow I/O (git fetch, version check, audit scan) from blocking
  the auto-reload middleware's exclusive Lock on concurrent requests

Other changes:
- computeTargetDiff takes source parameter (no more s.cfg.Source access)
- Add cloneTargets() helper for safe map copying
- Add writeOpsLogTo/writeAuditLogTo for lock-free oplog writes
2026-03-17 04:48:42 +08:00

43 lines
1.1 KiB
Go

package server
import (
"time"
"skillshare/internal/oplog"
)
func (s *Server) writeOpsLog(cmd, status string, start time.Time, args map[string]any, msg string) {
e := oplog.NewEntry(cmd, status, time.Since(start))
if len(args) > 0 {
e.Args = args
}
if msg != "" {
e.Message = msg
}
oplog.Write(s.configPath(), oplog.OpsFile, e) //nolint:errcheck
}
func (s *Server) writeAuditLog(status string, start time.Time, args map[string]any, msg string) {
e := oplog.NewEntry("audit", status, time.Since(start))
if len(args) > 0 {
e.Args = args
}
if msg != "" {
e.Message = msg
}
oplog.Write(s.configPath(), oplog.AuditFile, e) //nolint:errcheck
}
// writeAuditLogTo writes an audit log entry using the given config path (no lock needed).
// Used by handlers that snapshot cfgPath under RLock and write the log after releasing.
func writeAuditLogTo(cfgPath, status string, start time.Time, args map[string]any, msg string) {
e := oplog.NewEntry("audit", status, time.Since(start))
if len(args) > 0 {
e.Args = args
}
if msg != "" {
e.Message = msg
}
oplog.Write(cfgPath, oplog.AuditFile, e) //nolint:errcheck
}