fix: resolve data race in storagev2 uplog #175
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Requested by @zhangzqs
修复问题
修复了storagev2中uplog功能存在的数据竞争问题。
问题分析
根据提供的data race报告,问题出现在
internal/uplog/request_uplog.go中:竞争位置:
json.Marshal(uplog)时读取ConnectElapsedTime等timing字段httptrace.ClientTrace回调中写入这些字段根本原因: 多个goroutine同时访问
RequestUplog结构体的timing字段(uint64类型),造成并发读写竞争解决方案
atomic.StoreUint64atomicSnapshot方法,在JSON序列化前使用atomic.LoadUint64创建线程安全的结构体副本DNSElapsedTimeConnectElapsedTimeTLSConnectElapsedTimeRequestElapsedTimeWaitElapsedTimeResponseElapsedTimeTotalElapsedTime测试
兼容性
此修复完全向后兼容,不会影响现有的API或行为,只是确保了线程安全。
Fixes #174