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