Alanxtl commented on code in PR #849: URL: https://github.com/apache/dubbo-go-pixiu/pull/849#discussion_r2650093830
########## pkg/filter/network/grpcproxy/filter/proxy/descriptor_cache.go: ########## @@ -0,0 +1,367 @@ +/* + * 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 proxy + +import ( + "container/list" + "sync" + "sync/atomic" + "time" +) + +import ( + "google.golang.org/protobuf/reflect/protoreflect" +) + +import ( + "github.com/apache/dubbo-go-pixiu/pkg/logger" +) + +// Constants for cache configuration +const ( + defaultMaxCacheSize = 1000 // Maximum number of cached descriptors + minCacheSize = 100 // Minimum cache size to prevent thrashing +) + +// cacheEntry represents a cached method descriptor with metadata +type cacheEntry struct { + key string // Cache key for O(1) reverse lookup during eviction + descriptor protoreflect.MethodDescriptor + expireAt time.Time + versionHash string // Hash of the descriptor for version detection + element *list.Element // Pointer to LRU list element +} + +// CacheStats holds cache statistics for monitoring (returned by GetStats) +type CacheStats struct { + Hits int64 + Misses int64 + Evictions int64 + Size int + MaxSize int + TTL time.Duration + HitRatio float64 +} + +// DescriptorCache provides TTL-based caching with LRU eviction for gRPC method descriptors +type DescriptorCache struct { + entries sync.Map // key -> *cacheEntry + lruList *list.List // LRU eviction list + lruMutex sync.Mutex // Protects lruList + ttl time.Duration + stopCh chan struct{} + closeOnce sync.Once + maxSize atomic.Int32 // Maximum cache size (atomic for lock-free reads) + activeCount atomic.Int32 // O(1) size tracking instead of O(n) iteration + // Atomic statistics for lock-free updates + hits atomic.Int64 + misses atomic.Int64 + evictions atomic.Int64 +} + +// NewDescriptorCache creates a new descriptor cache with the specified TTL +func NewDescriptorCache(ttl time.Duration) *DescriptorCache { + return NewDescriptorCacheWithSize(ttl, defaultMaxCacheSize) +} + +// NewDescriptorCacheWithSize creates a new descriptor cache with custom size limit +func NewDescriptorCacheWithSize(ttl time.Duration, maxSize int) *DescriptorCache { + if maxSize < minCacheSize { + maxSize = minCacheSize + logger.Infof("Cache size adjusted to minimum: %d", maxSize) Review Comment: add a logger prefix ########## pkg/filter/network/grpcproxy/filter/proxy/descriptor_cache.go: ########## @@ -0,0 +1,367 @@ +/* + * 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 proxy + +import ( + "container/list" + "sync" + "sync/atomic" + "time" +) + +import ( + "google.golang.org/protobuf/reflect/protoreflect" +) + +import ( + "github.com/apache/dubbo-go-pixiu/pkg/logger" +) + +// Constants for cache configuration +const ( + defaultMaxCacheSize = 1000 // Maximum number of cached descriptors + minCacheSize = 100 // Minimum cache size to prevent thrashing +) + +// cacheEntry represents a cached method descriptor with metadata +type cacheEntry struct { + key string // Cache key for O(1) reverse lookup during eviction + descriptor protoreflect.MethodDescriptor + expireAt time.Time + versionHash string // Hash of the descriptor for version detection + element *list.Element // Pointer to LRU list element +} + +// CacheStats holds cache statistics for monitoring (returned by GetStats) +type CacheStats struct { + Hits int64 + Misses int64 + Evictions int64 + Size int + MaxSize int + TTL time.Duration + HitRatio float64 +} + +// DescriptorCache provides TTL-based caching with LRU eviction for gRPC method descriptors +type DescriptorCache struct { + entries sync.Map // key -> *cacheEntry + lruList *list.List // LRU eviction list + lruMutex sync.Mutex // Protects lruList Review Comment: I suggest use gost's lru https://github.com/dubbogo/gost/blob/master/container/gxlru/lru_cache.go -- 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]
