change locks
All checks were successful
Build and Publish / BuildAndDeploy (push) Successful in 4m18s
All checks were successful
Build and Publish / BuildAndDeploy (push) Successful in 4m18s
This commit is contained in:
@@ -197,13 +197,14 @@ func NewPacketQueue(connection net.Conn) *PacketQueue {
|
|||||||
go func(queueTimer *time.Ticker) {
|
go func(queueTimer *time.Ticker) {
|
||||||
for {
|
for {
|
||||||
<-queueTimer.C
|
<-queueTimer.C
|
||||||
queue.mu.RLock()
|
queue.mu.Lock()
|
||||||
for i, packet := range queue.Packets {
|
for i, packet := range queue.Packets {
|
||||||
if time.Since(packet.Added) > time.Second*5 {
|
if time.Since(packet.Added) < time.Second*3 {
|
||||||
queue.Packets = append(queue.Packets[:i], queue.Packets[i+1:]...)
|
queue.Packets = queue.Packets[i:]
|
||||||
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
queue.mu.RUnlock()
|
queue.mu.Unlock()
|
||||||
}
|
}
|
||||||
}(time.NewTicker(time.Second))
|
}(time.NewTicker(time.Second))
|
||||||
return queue
|
return queue
|
||||||
@@ -211,22 +212,19 @@ func NewPacketQueue(connection net.Conn) *PacketQueue {
|
|||||||
|
|
||||||
func (p *PacketQueue) Expect(messageType uint16, timeToWait time.Duration) (*PacketWithData, error) {
|
func (p *PacketQueue) Expect(messageType uint16, timeToWait time.Duration) (*PacketWithData, error) {
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
if time.Since(start) > timeToWait {
|
if time.Since(start) > timeToWait {
|
||||||
return nil, fmt.Errorf("timeout waiting for message type %d", messageType)
|
return nil, fmt.Errorf("timeout waiting for message type %d", messageType)
|
||||||
}
|
}
|
||||||
for i, packet := range p.Packets {
|
p.mu.RLock()
|
||||||
|
for _, packet := range p.Packets {
|
||||||
if packet.MessageType == messageType && packet.Added.After(start) {
|
if packet.MessageType == messageType && packet.Added.After(start) {
|
||||||
toReturn := PacketWithData{
|
p.mu.RUnlock()
|
||||||
MessageType: packet.MessageType,
|
return &packet, nil
|
||||||
Data: packet.Data,
|
|
||||||
}
|
|
||||||
p.mu.Lock()
|
|
||||||
p.Packets = append(p.Packets[:i], p.Packets[i+1:]...)
|
|
||||||
p.mu.Unlock()
|
|
||||||
return &toReturn, nil
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
p.mu.RUnlock()
|
||||||
time.Sleep(time.Millisecond * 5)
|
time.Sleep(time.Millisecond * 5)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user