Skip to content

Commit 9bcb53d

Browse files
feat: add operation name and ID to error messages of timed out WebSocket opeartions
1 parent 15f14c5 commit 9bcb53d

2 files changed

Lines changed: 13 additions & 4 deletions

File tree

Runtime/Communication/Lobby/ResultUtils.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@ namespace Elympics.Lobby
99
{
1010
internal static class ResultUtils
1111
{
12-
public static async UniTask<TResult> WaitForResult<TResult, TAction>(TimeSpan timeout, Func<UniTaskCompletionSource<TResult>, TAction> handlerFactory, Action<TAction>? onBeforeWait = null, Action<TAction>? onAfterWait = null, CancellationToken ct = default)
12+
public static async UniTask<TResult> WaitForResult<TResult, TAction>(
13+
TimeSpan timeout,
14+
Func<UniTaskCompletionSource<TResult>, TAction> handlerFactory,
15+
string operationName,
16+
Action<TAction>? onBeforeWait = null,
17+
Action<TAction>? onAfterWait = null,
18+
CancellationToken ct = default)
1319
where TAction : Delegate
1420
{
1521
var tcs = new UniTaskCompletionSource<TResult>();
@@ -32,7 +38,7 @@ public static async UniTask<TResult> WaitForResult<TResult, TAction>(TimeSpan ti
3238
}
3339

3440
ct.ThrowIfCancellationRequested();
35-
throw new LobbyOperationException($"Operation {nameof(WaitForResult)} timed out");
41+
throw new LobbyOperationException($"{nameof(WaitForResult)} timed out for operation: {operationName}.");
3642
}
3743

3844
public static async UniTask<T> WithTimeout<T>(this UniTaskCompletionSource<T> tcs, TimeSpan timeout, CancellationToken ct = default)

Runtime/Communication/Lobby/WebSocketSession.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public async UniTask<OperationResultDto> ExecuteOperation(LobbyOperation message
121121
private async UniTask<OperationResultDto> ExecuteOperationInternal(LobbyOperation message, CancellationToken ct)
122122
{
123123
using var linkedCts = CancellationTokenSource.CreateLinkedTokenSource(Token, ct);
124-
var task = WaitForOperationResult(message.OperationId, ElympicsTimeout.WebSocketOperationTimeout, linkedCts.Token);
124+
var task = WaitForOperationResult(message.OperationId, ElympicsTimeout.WebSocketOperationTimeout, $"{nameof(ExecuteOperationInternal)} {message.GetType().Name} with {nameof(message.OperationId)}: {message.OperationId}", linkedCts.Token);
125125
SendMessage(message);
126126
return await task;
127127
}
@@ -168,6 +168,7 @@ private async UniTask OpenWebSocket(IWebSocket ws)
168168
{
169169
var openTask = ResultUtils.WaitForResult<ValueTuple, WebSocketOpenEventHandler>(ElympicsTimeout.WebSocketOpeningTimeout,
170170
tcs => () => tcs.TrySetResult(new ValueTuple()),
171+
nameof(OpenWebSocket),
171172
handler => ws.OnOpen += handler,
172173
handler => ws.OnOpen -= handler,
173174
Token);
@@ -342,9 +343,10 @@ private async UniTask PropagateDisconnection(DisconnectionReason reason)
342343
Disconnected?.Invoke(data);
343344
}
344345

345-
private UniTask<OperationResultDto> WaitForOperationResult(Guid operationId, TimeSpan timeout, CancellationToken ct) =>
346+
private UniTask<OperationResultDto> WaitForOperationResult(Guid operationId, TimeSpan timeout, string operationName, CancellationToken ct) =>
346347
ResultUtils.WaitForResult<OperationResultDto, Action<OperationResultDto>>(timeout,
347348
tcs => result => _ = result.Success ? tcs.TrySetResult(result) : tcs.TrySetException(new LobbyOperationException(result)),
349+
operationName,
348350
handler => _operationResultHandlers.TryAdd(operationId, handler),
349351
_ => _operationResultHandlers.TryRemove(operationId, out var _),
350352
ct);
@@ -354,6 +356,7 @@ private async UniTask<TData> WaitForLobbyData<TData>(Guid requestId, TimeSpan ti
354356
{
355357
var result = await ResultUtils.WaitForResult<IFromLobby, Action<IFromLobby>>(timeout,
356358
tcs => result => _ = tcs.TrySetResult(result),
359+
$"{nameof(WaitForLobbyData)} {nameof(TData)} with {nameof(requestId)}: {requestId}",
357360
handler => _dataResponses.TryAdd(requestId, handler),
358361
_ => _dataResponses.TryRemove(requestId, out var _),
359362
ct);

0 commit comments

Comments
 (0)