fix(link): actually bound the server connect with a timeout [CI]
ci/woodpecker/push/web-build Pipeline was successful
ci/woodpecker/push/verify Pipeline was successful
ci/woodpecker/push/dev-image Pipeline was successful

This commit is contained in:
justme committed 2026-10-18 03:30:43 +08:00
1 parent 68c7113adf
commit 1a3feddc9d
2 files changed
+104 -3

No files matched your search

+6 -3
View File
@@ -64,6 +64,9 @@ public sealed class FleetLinkOptions
/// <summary>Optional reason for an orderly client shutdown, so the server can distinguish updates from service stops.</summary>
public Func<string>? ShutdownReason { get; init; }
/// <summary>连上服务器的整个过程(TCP、TLS、升级)最多等多久;卡住就放弃,由外层退避重试。</summary>
public TimeSpan ConnectTimeout { get; init; } = TimeSpan.FromSeconds(20);
public TimeSpan HeartbeatInterval { get; init; } = TimeSpan.FromSeconds(10);
public int MaxFrameBytes { get; init; } = 16 << 20;
@@ -96,7 +99,7 @@ public sealed class FleetLink
private readonly INodeLog _log;
private volatile RpcSession<LinkContext>? _session;
private volatile bool _stopped;
private static readonly TimeSpan s_connectTimeout = TimeSpan.FromSeconds(20);
private static readonly TimeSpan s_tcpTimeout = TimeSpan.FromSeconds(4);
private int _failedConnects;
@@ -179,7 +182,7 @@ public sealed class FleetLink
using var handler = new SocketsHttpHandler { ConnectCallback = (context, token) => ConnectTcpAsync(context, attempt, token) };
using var invoker = new HttpMessageInvoker(handler, disposeHandler: false);
using CancellationTokenSource timeout = CancellationTokenSource.CreateLinkedTokenSource(ct);
timeout.CancelAfter(s_connectTimeout);
timeout.CancelAfter(_options.ConnectTimeout);
try
{
await socket.ConnectAsync(uri, invoker, timeout.Token);
@@ -233,7 +236,7 @@ public sealed class FleetLink
socket.Options.KeepAliveInterval = TimeSpan.FromSeconds(15);
socket.Options.KeepAliveTimeout = TimeSpan.FromSeconds(20); // 对端 20 秒没有回 Pong 就判定连接已死
var builder = new UriBuilder(_options.Server) { Scheme = _options.Server.Scheme == "https" ? "wss" : "ws", Path = RpcPath };
await socket.ConnectAsync(builder.Uri, ct);
await ConnectAsync(socket, builder.Uri, ct);
await using var transport = new SecureTransport(new WsTransport(socket, _options.MaxFrameBytes));
string? failure = await AuthenticateAsync(transport, ct);