Chen-BUPT commented on code in PR #878: URL: https://github.com/apache/dubbo-go-pixiu/pull/878#discussion_r2777078238
########## pkg/filter/ai/kvcache/load_monitor.go: ########## @@ -0,0 +1,74 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package kvcache + +import ( + "runtime" + "sync" + "time" +) + +type LoadMonitor struct { + window time.Duration + last time.Time + count int64 + rate float64 + mutex sync.Mutex +} + +func NewLoadMonitor() *LoadMonitor { + return &LoadMonitor{ + window: time.Second, + last: time.Now(), + } +} + +func (lm *LoadMonitor) RecordRequest() { + if lm == nil { + return + } + lm.mutex.Lock() + lm.count++ + lm.mutex.Unlock() +} + +func (lm *LoadMonitor) Snapshot() LoadMetrics { + if lm == nil { + return LoadMetrics{} + } + lm.mutex.Lock() + defer lm.mutex.Unlock() + now := time.Now() + elapsed := now.Sub(lm.last) + if elapsed >= lm.window && elapsed > 0 { + lm.rate = float64(lm.count) / elapsed.Seconds() + lm.count = 0 + lm.last = now + } + var ms runtime.MemStats + runtime.ReadMemStats(&ms) + memUsage := 0.0 + if ms.Sys > 0 { + memUsage = float64(ms.Alloc) / float64(ms.Sys) + } + return LoadMetrics{ + CPUUsage: 0, + MemoryUsage: memUsage, + RequestRate: lm.rate, + } Review Comment: Fixed. Reworked cache deletion to use LoadAndDelete and only decrement cacheSize when deletion actually removed an existing key. Changes: - Added deleteCache(key) helper using sync.Map.LoadAndDelete - Replaced delete paths in TTL expiry, type-mismatch cleanup, InvalidateCache, and evictOne with deleteCache This prevents concurrent double-decrement on the same key and keeps cacheSize consistent for max-size eviction. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
