From b5687ecf98b999ad535e242c5d25cc5be328af77 Mon Sep 17 00:00:00 2001
From: zhangzengfei <zhangzengfei@smartai.com>
Date: 星期五, 13 九月 2024 11:17:00 +0800
Subject: [PATCH] add rlock in shardmap walk

---
 cache/shardmap/shardmap.go |   18 +++++++++---------
 1 files changed, 9 insertions(+), 9 deletions(-)

diff --git a/cache/shardmap/shardmap.go b/cache/shardmap/shardmap.go
index 96aaded..4e3ec24 100644
--- a/cache/shardmap/shardmap.go
+++ b/cache/shardmap/shardmap.go
@@ -93,25 +93,27 @@
 func (s *ShardMap) Walk(wf wfOp, sourceFea []float32, baseScore float32) (targets []*protomsg.SdkCompareEach) {
 	var wg sync.WaitGroup
 	var lock sync.Mutex
-	//fmt.Printf("Walk shards len %d\n", len(s.shards))
 
 	for _, si := range s.shards {
-		var tempsi shardItem = *si
+		var tempsi *shardItem = si // 淇濇寔瀵瑰師濮� shardItem 鐨勬寚閽堝紩鐢�
 
+		// 璺宠繃绌哄垎鐗�
 		if len(tempsi.data) == 0 {
 			continue
 		}
 
 		wg.Add(1)
-
 		go func(st *shardItem, fn wfOp, srcFeat []float32, baseSec float32) {
 			defer wg.Done()
+			st.RLock() // 閿佸畾璇诲彇
+			defer st.RUnlock() // 纭繚璇诲彇瀹屾瘯鍚庤В閿�
+
 			for _, feature := range st.data {
+				// 璇诲彇鎿嶄綔鍦ㄩ攣鍐呰繘琛岋紝闃叉骞跺彂鍐茬獊
 				if item, ok := feature.(*db.FeatureCacheBase); ok {
-					score := float32(0)
-					score = fn(srcFeat, item.FaceFeature)
+					score := fn(srcFeat, item.FaceFeature)
 					if score > 0 && score >= baseScore {
-						lock.Lock()
+						lock.Lock() // 淇濇姢鐩爣鍒囩墖鐨勫啓鍏�
 						targets = append(targets, &protomsg.SdkCompareEach{
 							Id:           item.Id,
 							CompareScore: score,
@@ -121,13 +123,11 @@
 					}
 				}
 			}
-
-		}(&tempsi, wf, sourceFea, baseScore)
+		}(tempsi, wf, sourceFea, baseScore)
 	}
 
 	wg.Wait()
 	return targets
-
 }
 
 // print all

--
Gitblit v1.8.0