Files
skillshare/internal/server/handler_sync_matrix.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

111 lines
2.8 KiB
Go

package server
import (
"encoding/json"
"net/http"
ssync "skillshare/internal/sync"
)
type syncMatrixEntry struct {
Skill string `json:"skill"`
Target string `json:"target"`
Status string `json:"status"`
Reason string `json:"reason"`
}
func (s *Server) handleSyncMatrix(w http.ResponseWriter, r *http.Request) {
// Snapshot config under RLock, then release before I/O.
s.mu.RLock()
source := s.cfg.Source
targets := s.cloneTargets()
s.mu.RUnlock()
skills, err := ssync.DiscoverSourceSkills(source)
if err != nil {
writeError(w, http.StatusInternalServerError, "failed to discover skills: "+err.Error())
return
}
targetFilter := r.URL.Query().Get("target")
var entries []syncMatrixEntry
for name, target := range targets {
if targetFilter != "" && name != targetFilter {
continue
}
if target.Mode == "symlink" {
for _, skill := range skills {
entries = append(entries, syncMatrixEntry{
Skill: skill.FlatName,
Target: name,
Status: "na",
Reason: "symlink mode — filters not applicable",
})
}
continue
}
for _, skill := range skills {
status, reason := ssync.ClassifySkillForTarget(skill.FlatName, skill.Targets, name, target.Include, target.Exclude)
entries = append(entries, syncMatrixEntry{
Skill: skill.FlatName,
Target: name,
Status: status,
Reason: reason,
})
}
}
writeJSON(w, map[string]any{"entries": entries})
}
func (s *Server) handleSyncMatrixPreview(w http.ResponseWriter, r *http.Request) {
// Snapshot config under RLock, then release before I/O.
s.mu.RLock()
source := s.cfg.Source
s.mu.RUnlock()
var body struct {
Target string `json:"target"`
Include []string `json:"include"`
Exclude []string `json:"exclude"`
}
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
writeError(w, http.StatusBadRequest, "invalid JSON: "+err.Error())
return
}
if body.Target == "" {
writeError(w, http.StatusBadRequest, "target is required")
return
}
// Validate patterns before discovering skills
if _, err := ssync.FilterSkills(nil, body.Include, nil); err != nil {
writeError(w, http.StatusBadRequest, err.Error())
return
}
if _, err := ssync.FilterSkills(nil, nil, body.Exclude); err != nil {
writeError(w, http.StatusBadRequest, err.Error())
return
}
skills, err := ssync.DiscoverSourceSkills(source)
if err != nil {
writeError(w, http.StatusInternalServerError, "failed to discover skills: "+err.Error())
return
}
var entries []syncMatrixEntry
for _, skill := range skills {
status, reason := ssync.ClassifySkillForTarget(skill.FlatName, skill.Targets, body.Target, body.Include, body.Exclude)
entries = append(entries, syncMatrixEntry{
Skill: skill.FlatName,
Target: body.Target,
Status: status,
Reason: reason,
})
}
writeJSON(w, map[string]any{"entries": entries})
}