fix: Fixes throttle receive packet issues (#1513)

This commit is contained in:
Kamron Batman 2023-09-25 18:35:59 -07:00 • committed by GitHub
parent 5d78941ddf
commit 0f41a3d1b1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -53,6 +53,8 @@ public partial class NetState : IComparable<NetState>
private static readonly Queue<NetState> _flushPending = new(2048); private static readonly Queue<NetState> _flushPending = new(2048);
private static readonly Queue<NetState> _flushedPartials = new(256); private static readonly Queue<NetState> _flushedPartials = new(256);
private static readonly Queue<NetState> _disposed = new(256); private static readonly Queue<NetState> _disposed = new(256);
private static readonly Queue<NetState> _throttled = new(256);
private static readonly Queue<NetState> _throttledPending = new(256);
public static NetStateCreatedCallback CreatedCallback { get; set; } public static NetStateCreatedCallback CreatedCallback { get; set; }
@ -562,14 +564,17 @@ public partial class NetState : IComparable<NetState>
} }
} }
public void HandleReceive() public void HandleReceive(bool throttled = false)
{ {
if (!_running) if (!_running)
{ {
return; return;
} }
ReceiveData(); if (!throttled)
{
ReceiveData();
}
var reader = RecvPipe.Reader; var reader = RecvPipe.Reader;
@ -712,7 +717,20 @@ public partial class NetState : IComparable<NetState>
{ {
reader.Advance((uint)packetLength); reader.Advance((uint)packetLength);
} }
else if (_parserState is ParserState.AwaitingPartialPacket or ParserState.Throttled) else if (_parserState is ParserState.Throttled)
{
if (!throttled)
{
_throttled.Enqueue(this);
}
else
{
_throttledPending.Enqueue(this);
}
break;
}
else if (_parserState is ParserState.AwaitingPartialPacket)
{ {
break; break;
} }
@ -937,6 +955,21 @@ public partial class NetState : IComparable<NetState>
public static void Slice() public static void Slice()
{ {
while (_throttled.Count > 0)
{
var ns = _throttled.Dequeue();
if (ns.Running)
{
ns.HandleReceive(true);
}
}
// This is enqueued by HandleReceive if already throttled and still throttled
while (_throttledPending.Count > 0)
{
_throttled.Enqueue(_throttledPending.Dequeue());
}
int count = _pollGroup.Poll(_polledStates); int count = _pollGroup.Poll(_polledStates);
if (count > 0) if (count > 0)