@@ -52,6 +52,8 @@ func newIndexer(ctx context.Context, defaultLRUSize int) *indexer {
5252// Add adds a list of prefix hashes to the cache, tied to the server.
5353func (i * indexer ) Add (hashes []BlockHash , pod Server ) {
5454 i .mu .Lock ()
55+ defer i .mu .Unlock ()
56+
5557 // Check if the LRU pod exist
5658 lruForPod , exists := i .podToLRU [pod .ServerID ]
5759 if ! exists {
@@ -64,15 +66,12 @@ func (i *indexer) Add(hashes []BlockHash, pod Server) {
6466 lruForPod = newLRU
6567 }
6668
67- i .mu .Unlock ()
68-
6969 // Add to LRU (may evict)
7070 for _ , hash := range hashes {
7171 lruForPod .Add (hash , struct {}{})
7272 }
7373
74- // Update hashToPods once under lock
75- i .mu .Lock ()
74+ // Update hashToPods
7675 for _ , hash := range hashes {
7776 podIDs := i .hashToPods [hash ]
7877 if podIDs == nil {
@@ -81,8 +80,6 @@ func (i *indexer) Add(hashes []BlockHash, pod Server) {
8180 podIDs [pod .ServerID ] = struct {}{}
8281 i .hashToPods [hash ] = podIDs
8382 }
84-
85- i .mu .Unlock ()
8683}
8784
8885// Get returns a set of servers that have the given prefix hash cached.
@@ -103,8 +100,6 @@ func (i *indexer) Get(hash BlockHash) podSet {
103100// makeEvictionFn returns a per-pod LRU eviction callback that removes the pod from hashToPods on eviction.
104101func (i * indexer ) makeEvictionFn (pod ServerID ) func (BlockHash , struct {}) {
105102 return func (hash BlockHash , _ struct {}) {
106- i .mu .Lock ()
107- defer i .mu .Unlock ()
108103 // Remove the pod from the hash→pods map
109104 if podSet , ok := i .hashToPods [hash ]; ok {
110105 delete (podSet , pod )
@@ -156,10 +151,10 @@ func (i *indexer) reportLRUSize(ctx context.Context, interval time.Duration) {
156151
157152// RemovePod removes a pod and its associated entries from the indexer.
158153func (i * indexer ) RemovePod (pod ServerID ) {
159- i .mu .RLock ()
160- lruCache , exists := i .podToLRU [pod ]
161- i .mu .RUnlock ()
154+ i .mu .Lock ()
155+ defer i .mu .Unlock ()
162156
157+ lruCache , exists := i .podToLRU [pod ]
163158 if ! exists {
164159 return
165160 }
@@ -169,9 +164,7 @@ func (i *indexer) RemovePod(pod ServerID) {
169164 lruCache .Remove (hash )
170165 }
171166
172- i .mu .Lock ()
173167 delete (i .podToLRU , pod )
174- i .mu .Unlock ()
175168}
176169
177170// Pods returns the list of all pods currently tracked in the indexer.
0 commit comments