diff options
| author | Paul Buetow <paul@buetow.org> | 2026-03-16 04:10:24 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-03-16 04:10:24 +0200 |
| commit | c54fb8bceee0341deb973cc06a73c199786832bf (patch) | |
| tree | 0804e29a9b91fa8513eca6e4228eb6729a2125c1 /internal/lsp/handlers_utils.go | |
| parent | 07e285f1bb28c832a78de123392be80fcd0c79b2 (diff) | |
Use atomic.Int64 for LLM stats counters instead of server-wide mutex
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Diffstat (limited to 'internal/lsp/handlers_utils.go')
| -rw-r--r-- | internal/lsp/handlers_utils.go | 32 |
1 files changed, 16 insertions, 16 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 |
