fix: 修复memory cache并发问题 - #889
Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 718d3e387e
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| mem.mu.RLock() | ||
| ret, ok := mem.data[key] | ||
| mem.mu.RUnlock() |
There was a problem hiding this comment.
Keep expiry decisions under the read lock
When Get reads a soon-to-expire entry, drops the lock here, and then another goroutine Sets a fresh value for the same key before the old expiry, this goroutine can resume after the old expiry and return nil even though the current entry is valid; IsExist has the same pattern. Keep the expiry check/return decision covered by the read lock, only rechecking under the write lock when deleting an expired entry.
Useful? React with 👍 / 👎.
memory的cache实现中Get 和 IsExist 读操作没有加锁,且存在死锁风险(deleteKey 内部加锁,但调用方可能已持锁)。
可用 sync.RWMutex 替换 sync.Mutex 修复并发读写问题,已完成相关修复并通过单元测试。
Fixes #888