Add negotiate timeout option to hub options (#1003)

This commit is contained in:
BrennanConroy 2017-10-11 08:59:37 -07:00 committed by GitHub
parent de535422d7
commit 8701baf73d
3 changed files with 13 additions and 6 deletions

View File

@ -30,7 +30,6 @@ namespace Microsoft.AspNetCore.SignalR
{ {
private static readonly Base64Encoder Base64Encoder = new Base64Encoder(); private static readonly Base64Encoder Base64Encoder = new Base64Encoder();
private static readonly PassThroughEncoder PassThroughEncoder = new PassThroughEncoder(); private static readonly PassThroughEncoder PassThroughEncoder = new PassThroughEncoder();
private static readonly TimeSpan NegotiateTimeout = TimeSpan.FromSeconds(5);
private readonly Dictionary<string, HubMethodDescriptor> _methods = new Dictionary<string, HubMethodDescriptor>(StringComparer.OrdinalIgnoreCase); private readonly Dictionary<string, HubMethodDescriptor> _methods = new Dictionary<string, HubMethodDescriptor>(StringComparer.OrdinalIgnoreCase);
@ -39,7 +38,7 @@ namespace Microsoft.AspNetCore.SignalR
private readonly ILogger<HubEndPoint<THub>> _logger; private readonly ILogger<HubEndPoint<THub>> _logger;
private readonly IServiceScopeFactory _serviceScopeFactory; private readonly IServiceScopeFactory _serviceScopeFactory;
private readonly IHubProtocolResolver _protocolResolver; private readonly IHubProtocolResolver _protocolResolver;
private readonly IOptions<HubOptions> _hubOptions; private readonly HubOptions _hubOptions;
private readonly IUserIdProvider _userIdProvider; private readonly IUserIdProvider _userIdProvider;
public HubEndPoint(HubLifetimeManager<THub> lifetimeManager, public HubEndPoint(HubLifetimeManager<THub> lifetimeManager,
@ -53,7 +52,7 @@ namespace Microsoft.AspNetCore.SignalR
_protocolResolver = protocolResolver; _protocolResolver = protocolResolver;
_lifetimeManager = lifetimeManager; _lifetimeManager = lifetimeManager;
_hubContext = hubContext; _hubContext = hubContext;
_hubOptions = hubOptions; _hubOptions = hubOptions.Value;
_logger = logger; _logger = logger;
_serviceScopeFactory = serviceScopeFactory; _serviceScopeFactory = serviceScopeFactory;
_userIdProvider = userIdProvider; _userIdProvider = userIdProvider;
@ -131,7 +130,7 @@ namespace Microsoft.AspNetCore.SignalR
{ {
using (var cts = new CancellationTokenSource()) using (var cts = new CancellationTokenSource())
{ {
cts.CancelAfter(NegotiateTimeout); cts.CancelAfter(_hubOptions.NegotiateTimeout);
while (await connection.Input.WaitToReadAsync(cts.Token)) while (await connection.Input.WaitToReadAsync(cts.Token))
{ {
while (connection.Input.TryRead(out var buffer)) while (connection.Input.TryRead(out var buffer))

View File

@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved. // Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using Microsoft.AspNetCore.SignalR.Internal.Protocol; using Microsoft.AspNetCore.SignalR.Internal.Protocol;
using MsgPack.Serialization; using MsgPack.Serialization;
using Newtonsoft.Json; using Newtonsoft.Json;
@ -11,5 +12,6 @@ namespace Microsoft.AspNetCore.SignalR
{ {
public JsonSerializerSettings JsonSerializerSettings { get; set; } = JsonHubProtocol.CreateDefaultSerializerSettings(); public JsonSerializerSettings JsonSerializerSettings { get; set; } = JsonHubProtocol.CreateDefaultSerializerSettings();
public SerializationContext MessagePackSerializationContext { get; set; } = MessagePackHubProtocol.CreateDefaultSerializationContext(); public SerializationContext MessagePackSerializationContext { get; set; } = MessagePackHubProtocol.CreateDefaultSerializationContext();
public TimeSpan NegotiateTimeout { get; set; } = TimeSpan.FromSeconds(5);
} }
} }

View File

@ -274,7 +274,13 @@ namespace Microsoft.AspNetCore.SignalR.Tests
[Fact] [Fact]
public async Task NegotiateTimesOut() public async Task NegotiateTimesOut()
{ {
var serviceProvider = CreateServiceProvider(); var serviceProvider = CreateServiceProvider(services =>
{
services.Configure<HubOptions>(hubOptions =>
{
hubOptions.NegotiateTimeout = TimeSpan.FromMilliseconds(5);
});
});
var endPoint = serviceProvider.GetService<HubEndPoint<SimpleHub>>(); var endPoint = serviceProvider.GetService<HubEndPoint<SimpleHub>>();
using (var client = new TestClient()) using (var client = new TestClient())
@ -282,7 +288,7 @@ namespace Microsoft.AspNetCore.SignalR.Tests
// TestClient automatically writes negotiate, for this test we want to assume negotiate never gets sent // TestClient automatically writes negotiate, for this test we want to assume negotiate never gets sent
client.Connection.Transport.In.TryRead(out var item); client.Connection.Transport.In.TryRead(out var item);
await endPoint.OnConnectedAsync(client.Connection).OrTimeout(TimeSpan.FromSeconds(10)); await endPoint.OnConnectedAsync(client.Connection).OrTimeout();
} }
} }