keep only unconsumed packages
All checks were successful
Build and Publish / BuildAndDeploy (push) Successful in 1m47s

This commit is contained in:
matst80
2024-11-10 11:59:23 +01:00
parent fda6c025b5
commit ee13f516ef

View File

@@ -12,6 +12,7 @@ import (
type PacketWithData struct { type PacketWithData struct {
MessageType uint16 MessageType uint16
Added time.Time Added time.Time
Consumed bool
Data []byte Data []byte
} }
@@ -42,20 +43,16 @@ func NewPacketQueue(connection net.Conn) *PacketQueue {
} }
queue.mu.Lock() queue.mu.Lock()
now := time.Now().Add(-time.Millisecond * 500)
keepIndex := -1 l := make([]PacketWithData, 0, len(queue.Packets))
for i, packet := range queue.Packets {
if packet.Added.After(now) { for _, packet := range queue.Packets {
keepIndex = i if !packet.Consumed {
break l = append(l, packet)
} }
} }
if keepIndex == -1 {
queue.Packets = queue.Packets[:0] queue.Packets = append(l, PacketWithData{
} else {
queue.Packets = queue.Packets[keepIndex:]
}
queue.Packets = append(queue.Packets, PacketWithData{
MessageType: messageType, MessageType: messageType,
Added: ts, Added: ts,
Data: data, Data: data,
@@ -78,6 +75,7 @@ func (p *PacketQueue) Expect(messageType uint16, timeToWait time.Duration) (*Pac
p.mu.RLock() p.mu.RLock()
for _, packet := range p.Packets { for _, packet := range p.Packets {
if packet.MessageType == messageType && packet.Added.After(start) { if packet.MessageType == messageType && packet.Added.After(start) {
packet.Consumed = true
p.mu.RUnlock() p.mu.RUnlock()
return &packet, nil return &packet, nil
} }