Skip to content

Commit f9e778b

Browse files
authored
execute m.Unlock in defer in store.go (#425)
1 parent be783ee commit f9e778b

File tree

1 file changed

+3
-7
lines changed

1 file changed

+3
-7
lines changed

store.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -184,13 +184,12 @@ func (m *lockedMap[V]) Set(i *Item[V]) {
184184

185185
func (m *lockedMap[V]) Del(key, conflict uint64) (uint64, V) {
186186
m.Lock()
187+
defer m.Unlock()
187188
item, ok := m.data[key]
188189
if !ok {
189-
m.Unlock()
190190
return 0, zeroValue[V]()
191191
}
192192
if conflict != 0 && (conflict != item.conflict) {
193-
m.Unlock()
194193
return 0, zeroValue[V]()
195194
}
196195

@@ -199,19 +198,17 @@ func (m *lockedMap[V]) Del(key, conflict uint64) (uint64, V) {
199198
}
200199

201200
delete(m.data, key)
202-
m.Unlock()
203201
return item.conflict, item.value
204202
}
205203

206204
func (m *lockedMap[V]) Update(newItem *Item[V]) (V, bool) {
207205
m.Lock()
206+
defer m.Unlock()
208207
item, ok := m.data[newItem.Key]
209208
if !ok {
210-
m.Unlock()
211209
return zeroValue[V](), false
212210
}
213211
if newItem.Conflict != 0 && (newItem.Conflict != item.conflict) {
214-
m.Unlock()
215212
return zeroValue[V](), false
216213
}
217214

@@ -223,12 +220,12 @@ func (m *lockedMap[V]) Update(newItem *Item[V]) (V, bool) {
223220
expiration: newItem.Expiration,
224221
}
225222

226-
m.Unlock()
227223
return item.value, true
228224
}
229225

230226
func (m *lockedMap[V]) Clear(onEvict func(item *Item[V])) {
231227
m.Lock()
228+
defer m.Unlock()
232229
i := &Item[V]{}
233230
if onEvict != nil {
234231
for _, si := range m.data {
@@ -239,5 +236,4 @@ func (m *lockedMap[V]) Clear(onEvict func(item *Item[V])) {
239236
}
240237
}
241238
m.data = make(map[uint64]storeItem[V])
242-
m.Unlock()
243239
}

0 commit comments

Comments
 (0)