Skip to content

Commit 7647bdb

Browse files
committed
perf(quic): move connection migration check from hot path to 5s timer
1 parent 19504e9 commit 7647bdb

3 files changed

Lines changed: 26 additions & 3 deletions

File tree

src/Servus.Akka/Transport/Quic/Client/QuicTransportStateMachine.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ namespace Servus.Akka.Transport.Quic.Client;
77
public sealed class QuicTransportStateMachine
88
{
99
private const string ConnectTimerKey = "connect-timeout";
10+
private const string MigrationCheckTimerKey = "migration-check";
11+
private static readonly TimeSpan MigrationCheckInterval = TimeSpan.FromSeconds(5);
1012

1113
private readonly ITransportOperations _ops;
1214
private readonly IActorRef _connectionManager;
@@ -51,7 +53,6 @@ internal void Dispatch(IQuicTransportEvent evt)
5153
case InboundData e:
5254
if (e.Gen == _connectionGen)
5355
{
54-
CheckForConnectionMigration();
5556
_ops.OnPushInbound(new MultiplexedData(e.Buffer, e.StreamId));
5657
}
5758
else
@@ -139,6 +140,13 @@ public void HandleDownstreamFinish()
139140

140141
public void OnTimer(string? timerKey)
141142
{
143+
if (timerKey == MigrationCheckTimerKey)
144+
{
145+
CheckForConnectionMigration();
146+
_ops.OnScheduleTimer(MigrationCheckTimerKey, MigrationCheckInterval);
147+
return;
148+
}
149+
142150
if (timerKey != ConnectTimerKey || _pendingConnect is null)
143151
{
144152
return;
@@ -153,6 +161,7 @@ public void OnTimer(string? timerKey)
153161
public void PostStop()
154162
{
155163
_ops.OnCancelTimer(ConnectTimerKey);
164+
_ops.OnCancelTimer(MigrationCheckTimerKey);
156165
CleanupTransport();
157166
}
158167

@@ -238,6 +247,7 @@ private void OnConnectionLeaseAcquired(QuicConnectionLease lease)
238247
_connectionLease = lease;
239248
_connectionHandle = lease.Handle;
240249
_lastLocalEndPoint = _connectionHandle.LocalEndPoint();
250+
_ops.OnScheduleTimer(MigrationCheckTimerKey, MigrationCheckInterval);
241251

242252
_pumpManager = new QuicPumpManager(_self);
243253
_pumpManager.StartAcceptLoop(_connectionHandle);

src/Servus.Akka/Transport/Quic/Listener/QuicServerConnectionStage.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ private void OnReceive((IActorRef sender, object message) args)
7474
}
7575
}
7676

77-
protected override void OnTimer(object timerKey) { }
77+
protected override void OnTimer(object timerKey) => _sm.OnTimer(timerKey as string);
7878

7979
public override void PostStop() => _sm.PostStop();
8080

src/Servus.Akka/Transport/Quic/Listener/QuicServerStateMachine.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ namespace Servus.Akka.Transport.Quic.Listener;
55

66
internal sealed class QuicServerStateMachine
77
{
8+
private const string MigrationCheckTimerKey = "migration-check";
9+
private static readonly TimeSpan MigrationCheckInterval = TimeSpan.FromSeconds(5);
10+
811
private readonly ITransportOperations _ops;
912
private readonly IActorRef _self;
1013
private readonly QuicConnectionHandle _connectionHandle;
@@ -33,6 +36,7 @@ public void Start()
3336
{
3437
_connectionGen++;
3538
_lastLocalEndPoint = _connectionHandle.LocalEndPoint();
39+
_ops.OnScheduleTimer(MigrationCheckTimerKey, MigrationCheckInterval);
3640

3741
_pumpManager = new QuicPumpManager(_self);
3842
_pumpManager.StartAcceptLoop(_connectionHandle);
@@ -47,7 +51,6 @@ internal void Dispatch(IQuicTransportEvent evt)
4751
case InboundData e:
4852
if (e.Gen == _connectionGen)
4953
{
50-
CheckForConnectionMigration();
5154
_ops.OnPushInbound(new MultiplexedData(e.Buffer, e.StreamId));
5255
}
5356
else
@@ -117,8 +120,18 @@ public void HandleDownstreamFinish()
117120
Cleanup();
118121
}
119122

123+
public void OnTimer(string? timerKey)
124+
{
125+
if (timerKey == MigrationCheckTimerKey)
126+
{
127+
CheckForConnectionMigration();
128+
_ops.OnScheduleTimer(MigrationCheckTimerKey, MigrationCheckInterval);
129+
}
130+
}
131+
120132
public void PostStop()
121133
{
134+
_ops.OnCancelTimer(MigrationCheckTimerKey);
122135
Cleanup();
123136
}
124137

0 commit comments

Comments
 (0)