summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2026-03-16 04:10:24 +0200
committerPaul Buetow <paul@buetow.org>2026-03-16 04:10:24 +0200
commitc54fb8bceee0341deb973cc06a73c199786832bf (patch)
tree0804e29a9b91fa8513eca6e4228eb6729a2125c1
parent07e285f1bb28c832a78de123392be80fcd0c79b2 (diff)
Use atomic.Int64 for LLM stats counters instead of server-wide mutex
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
-rw-r--r--internal/lsp/handlers_utils.go32
-rw-r--r--internal/lsp/server.go10
2 files changed, 21 insertions, 21 deletions
diff --git a/internal/lsp/handlers_utils.go b/internal/lsp/handlers_utils.go
index 292ff12..b36297c 100644
--- a/internal/lsp/handlers_utils.go
+++ b/internal/lsp/handlers_utils.go
@@ -125,33 +125,33 @@ func chooseSurfaceTemperature(cfg appconfig.App, entry appconfig.SurfaceConfig,
return llmutils.ResolveTemperature(provider, effectiveModel, entry.Temperature, cfg.CodingTemperature)
}
-// small helpers for LLM traffic stats
+// incSentCounters atomically increments request count and sent bytes.
func (s *Server) incSentCounters(n int) {
- s.mu.Lock()
- s.llmReqTotal++
- s.llmSentBytesTotal += int64(n)
- s.mu.Unlock()
+ s.llmReqTotal.Add(1)
+ s.llmSentBytesTotal.Add(int64(n))
}
+// incRecvCounters atomically increments response count and received bytes.
func (s *Server) incRecvCounters(n int) {
- s.mu.Lock()
- s.llmRespTotal++
- s.llmRespBytesTotal += int64(n)
- s.mu.Unlock()
+ s.llmRespTotal.Add(1)
+ s.llmRespBytesTotal.Add(int64(n))
}
+// logLLMStats logs local LLM traffic counters and the global stats snapshot.
+// Counter reads are atomic so no server-wide lock is needed.
func (s *Server) logLLMStats(model string) {
- s.mu.RLock()
+ reqs := s.llmReqTotal.Load()
+ sentTot := s.llmSentBytesTotal.Load()
+ recvTot := s.llmRespBytesTotal.Load()
avgSent := int64(0)
- if s.llmReqTotal > 0 {
- avgSent = s.llmSentBytesTotal / s.llmReqTotal
+ if reqs > 0 {
+ avgSent = sentTot / reqs
}
+ respTotal := s.llmRespTotal.Load()
avgRecv := int64(0)
- if s.llmRespTotal > 0 {
- avgRecv = s.llmRespBytesTotal / s.llmRespTotal
+ if respTotal > 0 {
+ avgRecv = recvTot / respTotal
}
- reqs, sentTot, recvTot := s.llmReqTotal, s.llmSentBytesTotal, s.llmRespBytesTotal
- s.mu.RUnlock()
mins := time.Since(s.startTime).Minutes()
if mins <= 0 {
mins = 0.001
diff --git a/internal/lsp/server.go b/internal/lsp/server.go
index d8fdc92..3af7e1d 100644
--- a/internal/lsp/server.go
+++ b/internal/lsp/server.go
@@ -38,11 +38,11 @@ type Server struct {
cfg appconfig.App
codeActionSubsystem
chatSubsystem
- // LLM request stats
- llmReqTotal int64
- llmSentBytesTotal int64
- llmRespTotal int64
- llmRespBytesTotal int64
+ // LLM request stats — atomic to avoid taking the server-wide mu lock.
+ llmReqTotal atomic.Int64
+ llmSentBytesTotal atomic.Int64
+ llmRespTotal atomic.Int64
+ llmRespBytesTotal atomic.Int64
startTime time.Time
completionSubsystem
configLoadOpts appconfig.LoadOptions