Skip to content

Commit

Permalink
Update TimeoutException message (#6773)
Browse files Browse the repository at this point in the history
  • Loading branch information
sw-joelmut committed Apr 11, 2024
1 parent 2a488e4 commit ed06db2
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
Expand Up @@ -43,6 +43,14 @@ protected StreamingConnection(ILogger logger)
/// <value>A <see cref="ILogger"/> for the streaming connection.</value>
protected ILogger Logger { get; }

/// <summary>
/// Gets a value indicating whether this is currently connected.
/// </summary>
/// <value>
/// True if this is currently connected, otherwise false.
/// </value>
protected bool IsConnected { get; private set; } = false;

/// <summary>
/// Sends a streaming request through the connection.
/// </summary>
Expand All @@ -65,7 +73,20 @@ public virtual async Task<ReceiveResponse> SendStreamingRequestAsync(StreamingRe
throw new InvalidOperationException("Cannot send streaming request since the session is not set up.");
}

return await _session.SendRequestAsync(request, cancellationToken).ConfigureAwait(false);
try
{
return await _session.SendRequestAsync(request, cancellationToken).ConfigureAwait(false);
}
catch (TimeoutException ex)
{
var timeoutMessage = $"The connection to the client has been disconnected, and the request has timed out after waiting {TaskExtensions.DefaultTimeout.Seconds} seconds for a response.";
if (IsConnected)
{
timeoutMessage = $"The request sent to the client has timed out after waiting {TaskExtensions.DefaultTimeout.Seconds} seconds for a response.";
}

throw new OperationCanceledException(timeoutMessage, ex, cancellationToken);
}
}

/// <summary>
Expand Down Expand Up @@ -96,7 +117,7 @@ public virtual async Task ListenAsync(RequestHandler requestHandler, Cancellatio
_session = new StreamingSession(requestHandler, _application, Logger, cancellationToken);

// Start transport and application
var transportTask = _transport.ConnectAsync(default, cancellationToken);
var transportTask = _transport.ConnectAsync((connected) => IsConnected = connected, cancellationToken);
var applicationTask = _application.ListenAsync(cancellationToken);

var tasks = new List<Task> { transportTask, applicationTask };
Expand Down
Expand Up @@ -105,7 +105,20 @@ public async Task<ReceiveResponse> SendAsync(StreamingRequest message, Cancellat
throw new ArgumentNullException(nameof(message));
}

return await _session.SendRequestAsync(message, cancellationToken).ConfigureAwait(false);
try
{
return await _session.SendRequestAsync(message, cancellationToken).ConfigureAwait(false);
}
catch (TimeoutException ex)
{
var timeoutMessage = $"The underlying connection has been disconnected, and the request has timed out after waiting {TaskExtensions.DefaultTimeout.Seconds} seconds for a response.";
if (IsConnected)
{
timeoutMessage = $"The request sent to the underlying connection has timed out after waiting {TaskExtensions.DefaultTimeout.Seconds} seconds for a response.";
}

throw new OperationCanceledException(timeoutMessage, ex, cancellationToken);
}
}

/// <inheritdoc />
Expand Down
8 changes: 4 additions & 4 deletions libraries/Microsoft.Bot.Connector.Streaming/TaskExtensions.cs
Expand Up @@ -6,14 +6,14 @@
using System.Threading.Tasks;

namespace Microsoft.Bot.Connector.Streaming
{
{
internal static class TaskExtensions
{
private static readonly TimeSpan _defaultTimeout = TimeSpan.FromSeconds(30);
public static readonly TimeSpan DefaultTimeout = TimeSpan.FromSeconds(30);

public static async Task<T> DefaultTimeOutAsync<T>(this Task<T> task)
{
return await task.TimeoutAfterAsync<T>(_defaultTimeout).ConfigureAwait(false);
return await task.TimeoutAfterAsync<T>(DefaultTimeout).ConfigureAwait(false);
}

public static async Task<T> TimeoutAfterAsync<T>(this Task<T> task, TimeSpan timeout)
Expand Down Expand Up @@ -41,7 +41,7 @@ public static async Task<T> TimeoutAfterAsync<T>(this Task<T> task, TimeSpan tim

public static async Task DefaultTimeOutAsync(this Task task)
{
await task.TimeoutAfterAsync(_defaultTimeout).ConfigureAwait(false);
await task.TimeoutAfterAsync(DefaultTimeout).ConfigureAwait(false);
}

public static async Task TimeoutAfterAsync(this Task task, TimeSpan timeout)
Expand Down

0 comments on commit ed06db2

Please sign in to comment.