"backend/vscode:/vscode.git/clone" did not exist on "a8779a2c054c457c2e8bb9c97fb467b256fc04fb"
Commit 3457bcbf authored by erio's avatar erio
Browse files

fix(channel): 修复 invalidateCache 存入 typed nil 导致 loadCache panic

invalidateCache 存入 (*channelCache)(nil),类型断言 ok=true 但
指针为 nil,后续 cached.loadedAt 导致 nil pointer dereference。
在 loadCache 双重检查处增加 cached != nil 防御。
parent eb385457
...@@ -137,7 +137,7 @@ func NewChannelService(repo ChannelRepository, authCacheInvalidator APIKeyAuthCa ...@@ -137,7 +137,7 @@ func NewChannelService(repo ChannelRepository, authCacheInvalidator APIKeyAuthCa
// loadCache 加载或返回缓存的渠道数据 // loadCache 加载或返回缓存的渠道数据
func (s *ChannelService) loadCache(ctx context.Context) (*channelCache, error) { func (s *ChannelService) loadCache(ctx context.Context) (*channelCache, error) {
if cached, ok := s.cache.Load().(*channelCache); ok { if cached, ok := s.cache.Load().(*channelCache); ok && cached != nil {
if time.Since(cached.loadedAt) < channelCacheTTL { if time.Since(cached.loadedAt) < channelCacheTTL {
return cached, nil return cached, nil
} }
...@@ -145,7 +145,7 @@ func (s *ChannelService) loadCache(ctx context.Context) (*channelCache, error) { ...@@ -145,7 +145,7 @@ func (s *ChannelService) loadCache(ctx context.Context) (*channelCache, error) {
result, err, _ := s.cacheSF.Do("channel_cache", func() (any, error) { result, err, _ := s.cacheSF.Do("channel_cache", func() (any, error) {
// 双重检查 // 双重检查
if cached, ok := s.cache.Load().(*channelCache); ok { if cached, ok := s.cache.Load().(*channelCache); ok && cached != nil {
if time.Since(cached.loadedAt) < channelCacheTTL { if time.Since(cached.loadedAt) < channelCacheTTL {
return cached, nil return cached, nil
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment