From d11f6574a37dc711cdf0bed3730aeb2344d7cc8b Mon Sep 17 00:00:00 2001 From: GoEdgeLab Date: Thu, 23 Nov 2023 17:14:50 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=B8=85=E7=A9=BA=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E7=B4=A2=E5=BC=95Map=E6=97=B6=E4=BA=A7=E7=94=9F?= =?UTF-8?q?=E5=B9=B6=E5=8F=91=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/caches/list_file_hash_map.go | 5 ++++- internal/caches/list_file_hash_map_test.go | 7 +++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/internal/caches/list_file_hash_map.go b/internal/caches/list_file_hash_map.go index 625de3d..5a772bb 100644 --- a/internal/caches/list_file_hash_map.go +++ b/internal/caches/list_file_hash_map.go @@ -133,7 +133,10 @@ func (this *FileListHashMap) Clean() { this.lockers[i].Lock() } - this.m = make([]map[uint64]zero.Zero, HashMapSharding) + // 这里不能简单清空 this.m ,避免导致别的数据无法写入 map 而产生 panic + for i := 0; i < HashMapSharding; i++ { + this.m[i] = map[uint64]zero.Zero{} + } for i := HashMapSharding - 1; i >= 0; i-- { this.lockers[i].Unlock() diff --git a/internal/caches/list_file_hash_map_test.go b/internal/caches/list_file_hash_map_test.go index f9481fa..d2080eb 100644 --- a/internal/caches/list_file_hash_map_test.go +++ b/internal/caches/list_file_hash_map_test.go @@ -125,6 +125,13 @@ func TestFileListHashMap_Delete(t *testing.T) { a.IsTrue(m.Len() == 0) } +func TestFileListHashMap_Clean(t *testing.T) { + var m = caches.NewFileListHashMap() + m.SetIsAvailable(true) + m.Clean() + m.Add("a") +} + func Benchmark_BigInt(b *testing.B) { var hash = stringutil.Md5("123456") b.ResetTimer()