Skip to content

Commit

Permalink
Merge pull request #40 from PabloMK7/runanddelete
Browse files Browse the repository at this point in the history
Add RunAndDelete to MutexMap to fix race condition
  • Loading branch information
jonbarrow authored Nov 4, 2023
2 parents 9d6e91c + 7c74278 commit 848280d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
11 changes: 11 additions & 0 deletions mutex_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ func (m *MutexMap[K, V]) Delete(key K) {
delete(m.real, key)
}

// RunAndDelete runs a callback and removes the key afterwards
func (m *MutexMap[K, V]) RunAndDelete(key K, callback func(key K, value V)) {
m.Lock()
defer m.Unlock()

if value, ok := m.real[key]; ok {
callback(key, value)
delete(m.real, key)
}
}

// Size returns the length of the internal map
func (m *MutexMap[K, V]) Size() int {
m.RLock()
Expand Down
7 changes: 3 additions & 4 deletions packet_resend_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,9 @@ func (p *PacketResendManager) Add(packet PacketInterface) {

// Remove removes a packet from pool and stops it's timer
func (p *PacketResendManager) Remove(sequenceID uint16) {
if cached, ok := p.pending.Get(sequenceID); ok {
cached.StopTimeoutTimer()
p.pending.Delete(sequenceID)
}
p.pending.RunAndDelete(sequenceID, func(key uint16, value *PendingPacket){
value.StopTimeoutTimer()
})
}

// Clear removes all packets from pool and stops their timers
Expand Down

0 comments on commit 848280d

Please sign in to comment.