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

93 lines
2.3 KiB
Go

package server
import (
"encoding/json"
"net/http"
"path/filepath"
"strconv"
"skillshare/internal/hub"
"skillshare/internal/search"
)
func (s *Server) handleSearch(w http.ResponseWriter, r *http.Request) {
// Snapshot config under RLock, then release before I/O.
s.mu.RLock()
source := s.cfg.Source
projectRoot := s.projectRoot
s.mu.RUnlock()
query := r.URL.Query().Get("q")
hubParam := r.URL.Query().Get("hub")
limit := 0 // default: no limit for hub search
if l := r.URL.Query().Get("limit"); l != "" {
if parsed, err := strconv.Atoi(l); err == nil {
limit = parsed
}
}
var results []search.SearchResult
var err error
switch {
case hubParam == "@builtin":
results, err = searchBuiltinIndex(source, projectRoot, query, limit)
case hubParam != "":
results, err = search.SearchFromIndexURL(query, limit, hubParam)
default:
// GitHub search always needs a limit
if limit <= 0 {
limit = 20
}
results, err = search.Search(query, limit)
}
if err != nil {
writeError(w, http.StatusInternalServerError, err.Error())
return
}
type resultItem struct {
Name string `json:"name"`
Description string `json:"description"`
Source string `json:"source"`
Skill string `json:"skill,omitempty"`
Stars int `json:"stars"`
Owner string `json:"owner"`
Repo string `json:"repo"`
Tags []string `json:"tags,omitempty"`
}
items := make([]resultItem, 0, len(results))
for _, r := range results {
items = append(items, resultItem{
Name: r.Name,
Description: r.Description,
Source: r.Source,
Skill: r.Skill,
Stars: r.Stars,
Owner: r.Owner,
Repo: r.Repo,
Tags: r.Tags,
})
}
writeJSON(w, map[string]any{"results": items})
}
// searchBuiltinIndex builds the hub index from local skills and searches it in-memory.
func searchBuiltinIndex(source, projectRoot, query string, limit int) ([]search.SearchResult, error) {
sourcePath := source
if projectRoot != "" {
sourcePath = filepath.Join(projectRoot, ".skillshare", "skills")
}
idx, err := hub.BuildIndex(sourcePath, false, false)
if err != nil {
return nil, err
}
data, err := json.Marshal(idx)
if err != nil {
return nil, err
}
return search.SearchFromIndexJSON(query, limit, data)
}