keep only unconsumed packages
All checks were successful
Build and Publish / BuildAndDeploy (push) Successful in 1m47s
All checks were successful
Build and Publish / BuildAndDeploy (push) Successful in 1m47s
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user