From 1fe8a910931db08249a610902ec4d862043a8520 Mon Sep 17 00:00:00 2001 From: PabloMK7 Date: Sat, 4 Nov 2023 13:17:05 +0100 Subject: [PATCH 1/3] Add RunAndDelete to MutexMap to fix rc --- mutex_map.go | 15 +++++++++++++++ packet_resend_manager.go | 7 +++---- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/mutex_map.go b/mutex_map.go index 4a91d345..45618043 100644 --- a/mutex_map.go +++ b/mutex_map.go @@ -34,6 +34,21 @@ func (m *MutexMap[K, V]) Delete(key K) { delete(m.real, key) } +// RunAndDelete runs a callback +// and deletes the key afterwards +func (m *MutexMap[K, V]) RunAndDelete(key K, callback func(key K, value V)) { + m.Lock() + defer m.Unlock() + + value, ok := m.real[key] + + if 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() diff --git a/packet_resend_manager.go b/packet_resend_manager.go index 47b7028c..efa89d0a 100644 --- a/packet_resend_manager.go +++ b/packet_resend_manager.go @@ -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 From 537c4d1dfd5fda64754546102eaf7f65f53fe0ea Mon Sep 17 00:00:00 2001 From: PabloMK7 Date: Sat, 4 Nov 2023 13:22:10 +0100 Subject: [PATCH 2/3] Change description --- mutex_map.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/mutex_map.go b/mutex_map.go index 45618043..8d37ef84 100644 --- a/mutex_map.go +++ b/mutex_map.go @@ -34,8 +34,7 @@ func (m *MutexMap[K, V]) Delete(key K) { delete(m.real, key) } -// RunAndDelete runs a callback -// and deletes the key afterwards +// 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() From 7c742784ba837554f87cdaf327772ea00a0c3a76 Mon Sep 17 00:00:00 2001 From: PabloMK7 Date: Sat, 4 Nov 2023 13:24:26 +0100 Subject: [PATCH 3/3] Minor optimization --- mutex_map.go | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/mutex_map.go b/mutex_map.go index 8d37ef84..26284746 100644 --- a/mutex_map.go +++ b/mutex_map.go @@ -39,13 +39,10 @@ func (m *MutexMap[K, V]) RunAndDelete(key K, callback func(key K, value V)) { m.Lock() defer m.Unlock() - value, ok := m.real[key] - - if ok { + if value, ok := m.real[key]; ok { callback(key, value) + delete(m.real, key) } - - delete(m.real, key) } // Size returns the length of the internal map