Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API proposal: enable configuring connection timeouts in HubConnectionBuilder #44742

Closed
surayya-MS opened this issue Oct 26, 2022 · 7 comments · Fixed by #46172
Closed

API proposal: enable configuring connection timeouts in HubConnectionBuilder #44742

surayya-MS opened this issue Oct 26, 2022 · 7 comments · Fixed by #46172
Assignees
Labels
api-approved API was approved in API review, it can be implemented area-signalr Includes: SignalR clients and servers Blazor ♥ SignalR This issue is related to the experience of Signal R and Blazor working together
Milestone

Comments

@surayya-MS
Copy link
Member

surayya-MS commented Oct 26, 2022

Background and Motivation

Related to #18840

When debugging a server-side blazor app customers often see this error pop up in the client (browser) while stepping through code:
Error: Connection disconnected with error 'Error: Server timeout elapsed without receiving a message from the server.'.
This can be disruptive, requiring a refresh of the browser to recover after continuing the debug session.

The workaround for this is to set serverTimeoutInMilliseconds for HubConnection in Blazor.start.

Blazor.start({
    configureSignalR: function (builder) {
        let c = builder.build();
        c.serverTimeoutInMilliseconds = 60000;
        c.keepAliveIntervalInMilliseconds = 30000;
        builder.build = () => {
            return c;
        };
    };
})

Instead of overriding build method of HubConnectionBuilder with set serverTimeoutInMilliseconds it would be nice to set this things directly in the builder:

Blazor.start({
    configureSignalR: function (builder) {
        builder.serverTimeoutInMilliseconds = 60000;
        builder.keepAliveIntervalInMilliseconds = 30000;
    };
});

Proposed API

TypeScript Client

Enable configuring the connection timeouts in HubConnectionBuilder like serverTimeoutInMilliseconds and keepAliveIntervalInMilliseconds .

class HubConnectionBuilder {
+    public serverTimeoutInMilliseconds?: number;
+
+    public keepAliveIntervalInMilliseconds?: number;

    public reconnectPolicy?: IRetryPolicy;

    public configureLogging(logLevel: LogLevel): HubConnectionBuilder;

    public withUrl(url: string): HubConnectionBuilder;

    public withAutomaticReconnect(): HubConnectionBuilder;
}

C# Client

public class HubConnectionBuilder : IHubConnectionBuilder
{
    public IServiceCollection Services { get; }

+    public TimeSpan? ServerTimeout { get; set; }

+    public TimeSpan? KeepAliveInterval { get; set; }

    public HubConnectionBuilder();

    public HubConnection Build();
}

public static class HubConnectionBuilderExtensions
{
    public static IHubConnectionBuilder ConfigureLogging(this IHubConnectionBuilder hubConnectionBuilder, Action<ILoggingBuilder> configureLogging);

    public static IHubConnectionBuilder WithAutomaticReconnect(this IHubConnectionBuilder hubConnectionBuilder, TimeSpan[] reconnectDelays);
}

Usage Examples

Customers will use it to configure the connection timeout in server-side blazor app like so:

Blazor.start({
    configureSignalR: function (builder) {
        builder.serverTimeoutInMilliseconds = 60000;
        builder.keepAliveIntervalInMilliseconds = 30000;
    };
});

Alternative Designs

As an alternative we can add a delegate to Blazor.start to configure HubConnection

Blazor.start({
            configureSignalR: builder => builder.configureLogging("debug"),
            configureConnection: function (connection) {
                connection.serverTimeoutInMilliseconds = 60000;
                connection.keepAliveIntervalInMilliseconds = 30000;
            },
        });

Risks

I'm don't think there are any

@surayya-MS surayya-MS added area-signalr Includes: SignalR clients and servers api-suggestion Early API idea and discussion, it is NOT ready for implementation Blazor ♥ SignalR This issue is related to the experience of Signal R and Blazor working together labels Oct 26, 2022
@surayya-MS
Copy link
Member Author

@BrennanConroy , any updates on this API proposal? Do you think this is worth implementing or should we implement the alternative design (#44623)?

@BrennanConroy
Copy link
Member

We need to put what the API will look like in the issue. Example: #43355 (comment)

Also, we probably want to do this across all the clients, not just the Typescript one.

@surayya-MS
Copy link
Member Author

Included what API will look like for TypeScript and C# clients. Also opened a PR for it. If the proposed design looks good I will proceed with Java client.

@BrennanConroy
Copy link
Member

Thanks, simplified it because we don't need to see the implementation (and all the doc comments etc.) during API review.

@surayya-MS surayya-MS added the api-ready-for-review API is ready for formal API review - https://github.com/dotnet/apireviews label Jan 5, 2023
@ghost
Copy link

ghost commented Jan 5, 2023

Thank you for submitting this for API review. This will be reviewed by @dotnet/aspnet-api-review at the next meeting of the ASP.NET Core API Review group. Please ensure you take a look at the API review process documentation and ensure that:

  • The PR contains changes to the reference-assembly that describe the API change. Or, you have included a snippet of reference-assembly-style code that illustrates the API change.
  • The PR describes the impact to users, both positive (useful new APIs) and negative (breaking changes).
  • Someone is assigned to "champion" this change in the meeting, and they understand the impact and design of the change.

@halter73
Copy link
Member

halter73 commented Jan 9, 2023

API Review Notes:

  • This is particularly important to blazor which doesn't expose the connection. This could allow unwanted interference with the blazor app
  • Do we need the C# client changes? No, but it's more consistent.
  • Should we use a new option type? Or methods to set these properties? Let's use methods. It does not allow reading the setting back of the builder, but we don't think this is important. It can still be read off of the HubConnection.
  • Is there more config we want to add the the builders that are currently on the HubConnection type? HandshakeTimeout? We can add HandshakeTimeout later.

TS

export class HubConnectionBuilder
{
+    public withServerTimeout(milliseconds: number): HubConnectionBuilder;
+    public withKeepAliveInterval(milliseconds: number): HubConnectionBuilder;
}

Example Usage:

Blazor.start({
    configureSignalR: function (builder) {
        builder.withServerTimeout(60000).withKeepAliveInterval(3000);
    };
});

C#

namespace Microsoft.AspNetCore.SignalR.Client;

public static class HubConnectionBuilderExtensions
{
+   public static IHubConnectionBuilder WithServerTimeout(this IHubConnectionBuilder hubConnectionBuilder, TimeSpan timeout);
+   public static IHubConnectionBuilder WithKeepAliveInterval(this IHubConnectionBuilder hubConnectionBuilder, TimeSpan interval);

Java

We also approve the equivalent change to Java's HttpHubConnectionBuilder.

API Approved!

@halter73 halter73 added api-approved API was approved in API review, it can be implemented and removed api-suggestion Early API idea and discussion, it is NOT ready for implementation api-ready-for-review API is ready for formal API review - https://github.com/dotnet/apireviews labels Jan 9, 2023
@BrennanConroy BrennanConroy added this to the 8.0-preview1 milestone Jan 13, 2023
@halter73
Copy link
Member

We had an internal conversation about moving the C# client's HubConnectionBuilderHttpExtensions methods to HubConnectionBuilderExtensions. @surayya-MS pointed out it would be more consistent this way.

In HubConnectionBuilderHttpExtensions there are only methods to set Url which is done by configuring HttpConnectionOptions. I needed to save ServerTimeout and KeepAliveInterval somewhere so then l could use them to set same properties in HubConnection. At first I considered adding those to HttpConnectionOptions but HttpConnectionOptions is used not only for HubConnection. That's why I created internal class HubConnectionOptions. And since there is no relation to Http it made more sense to add the new methods to HubConnectionBuilderExtensions instead of HubConnectionBuilderHttpExtensions.

The API reviewers agreed that it makes sense to move the methods. The updated approved API for the C# client is now:

C#

namespace Microsoft.AspNetCore.SignalR.Client;

public static class HubConnectionBuilderExtensions
{
+   public static IHubConnectionBuilder WithServerTimeout(this IHubConnectionBuilder hubConnectionBuilder, TimeSpan timeout);
+   public static IHubConnectionBuilder WithKeepAliveInterval(this IHubConnectionBuilder hubConnectionBuilder, TimeSpan interval);

I've updated the original API approval comment to reflect this.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api-approved API was approved in API review, it can be implemented area-signalr Includes: SignalR clients and servers Blazor ♥ SignalR This issue is related to the experience of Signal R and Blazor working together
Projects
None yet
3 participants