Skip to content

Commit c68eadf

Browse files
committed
scope
1 parent e51ebd2 commit c68eadf

9 files changed

+21
-29
lines changed

src/SignalR/common/Http.Connections/src/ConnectionsDependencyInjectionExtensions.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System;
55
using Microsoft.AspNetCore.Http.Connections;
66
using Microsoft.AspNetCore.Http.Connections.Internal;
7-
using Microsoft.AspNetCore.Internal;
87
using Microsoft.Extensions.DependencyInjection.Extensions;
98
using Microsoft.Extensions.Options;
109

@@ -27,7 +26,6 @@ public static IServiceCollection AddConnections(this IServiceCollection services
2726
services.TryAddEnumerable(ServiceDescriptor.Singleton<IConfigureOptions<ConnectionOptions>, ConnectionOptionsSetup>());
2827
services.TryAddSingleton<HttpConnectionDispatcher>();
2928
services.TryAddSingleton<HttpConnectionManager>();
30-
services.TryAddSingleton<ISystemClock, SystemClock>();
3129
return services;
3230
}
3331

src/SignalR/common/Http.Connections/src/Internal/HttpConnectionManager.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,13 @@ internal partial class HttpConnectionManager
3131
private readonly ILogger<HttpConnectionManager> _logger;
3232
private readonly ILogger<HttpConnectionContext> _connectionLogger;
3333
private readonly TimeSpan _disconnectTimeout;
34-
private readonly ISystemClock _systemClock;
3534

36-
public HttpConnectionManager(ILoggerFactory loggerFactory, IHostApplicationLifetime appLifetime, IOptions<ConnectionOptions> connectionOptions, ISystemClock systemClock)
35+
public HttpConnectionManager(ILoggerFactory loggerFactory, IHostApplicationLifetime appLifetime, IOptions<ConnectionOptions> connectionOptions)
3736
{
3837
_logger = loggerFactory.CreateLogger<HttpConnectionManager>();
3938
_connectionLogger = loggerFactory.CreateLogger<HttpConnectionContext>();
4039
_nextHeartbeat = new TimerAwaitable(_heartbeatTickRate, _heartbeatTickRate);
4140
_disconnectTimeout = connectionOptions.Value.DisconnectTimeout ?? ConnectionOptionsSetup.DefaultDisconectTimeout;
42-
_systemClock = systemClock;
4341

4442
// Register these last as the callbacks could run immediately
4543
appLifetime.ApplicationStarted.Register(() => Start());
@@ -94,7 +92,6 @@ internal HttpConnectionContext CreateConnection(PipeOptions transportPipeOptions
9492
var pair = DuplexPipe.CreateConnectionPair(transportPipeOptions, appPipeOptions);
9593
connection.Transport = pair.Application;
9694
connection.Application = pair.Transport;
97-
connection.Features.Set(_systemClock);
9895

9996
_connections.TryAdd(connectionToken, (connection, connectionTimer));
10097

@@ -153,7 +150,7 @@ public void Scan()
153150
// Capture the connection state
154151
var lastSeenUtc = connection.LastSeenUtcIfInactive;
155152

156-
var utcNow = _systemClock.UtcNow;
153+
var utcNow = DateTimeOffset.UtcNow;
157154
// Once the decision has been made to dispose we don't check the status again
158155
// But don't clean up connections while the debugger is attached.
159156
if (!Debugger.IsAttached && lastSeenUtc.HasValue && (utcNow - lastSeenUtc.Value).TotalSeconds > _disconnectTimeout.TotalSeconds)

src/SignalR/common/Http.Connections/src/Microsoft.AspNetCore.Http.Connections.csproj

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
<Compile Include="$(SignalRSharedSourceRoot)StreamExtensions.cs" Link="StreamExtensions.cs" />
1717
<Compile Include="$(SignalRSharedSourceRoot)DuplexPipe.cs" Link="DuplexPipe.cs" />
1818
<Compile Include="$(SignalRSharedSourceRoot)TaskCache.cs" Link="Internal\TaskCache.cs" />
19-
<Compile Include="$(SignalRSharedSourceRoot)ISystemClock.cs" Link="Internal\ISystemClock.cs" />
20-
<Compile Include="$(SignalRSharedSourceRoot)SystemClock.cs" Link="Internal\SystemClock.cs" />
2119
</ItemGroup>
2220

2321
<ItemGroup>

src/SignalR/common/Http.Connections/test/HttpConnectionDispatcherTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2354,7 +2354,7 @@ private static HttpConnectionManager CreateConnectionManager(ILoggerFactory logg
23542354
{
23552355
var connectionOptions = new ConnectionOptions();
23562356
connectionOptions.DisconnectTimeout = disconnectTimeout;
2357-
return new HttpConnectionManager(loggerFactory ?? new LoggerFactory(), new EmptyApplicationLifetime(), Options.Create(connectionOptions), new SystemClock());
2357+
return new HttpConnectionManager(loggerFactory ?? new LoggerFactory(), new EmptyApplicationLifetime(), Options.Create(connectionOptions));
23582358
}
23592359

23602360
private string GetContentAsString(Stream body)

src/SignalR/common/Http.Connections/test/HttpConnectionManagerTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ public async Task ApplicationLifetimeCanStartBeforeHttpConnectionManagerInitiali
412412
private static HttpConnectionManager CreateConnectionManager(ILoggerFactory loggerFactory, IHostApplicationLifetime lifetime = null)
413413
{
414414
lifetime = lifetime ?? new EmptyApplicationLifetime();
415-
return new HttpConnectionManager(loggerFactory, lifetime, Options.Create(new ConnectionOptions()), new SystemClock());
415+
return new HttpConnectionManager(loggerFactory, lifetime, Options.Create(new ConnectionOptions()));
416416
}
417417

418418
[Flags]

src/SignalR/server/Core/src/HubConnectionContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public HubConnectionContext(ConnectionContext connectionContext, HubConnectionCo
6969

7070
HubCallerContext = new DefaultHubCallerContext(this);
7171

72-
_systemClock = _connectionContext.Features.Get<ISystemClock>() ?? new SystemClock();
72+
_systemClock = contextOptions.SystemClock;
7373
_lastSendTimeStamp = _systemClock.UtcNowTicks;
7474
}
7575

src/SignalR/server/Core/src/HubConnectionContextOptions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5+
using Microsoft.AspNetCore.Internal;
56

67
namespace Microsoft.AspNetCore.SignalR
78
{
@@ -29,5 +30,7 @@ public class HubConnectionContextOptions
2930
/// Gets or sets the maximum message size the client can send.
3031
/// </summary>
3132
public long? MaximumReceiveMessageSize { get; set; }
33+
34+
internal ISystemClock SystemClock { get; set; }
3235
}
3336
}

src/SignalR/server/Core/src/HubConnectionHandler.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Linq;
88
using System.Threading.Tasks;
99
using Microsoft.AspNetCore.Connections;
10+
using Microsoft.AspNetCore.Internal;
1011
using Microsoft.AspNetCore.SignalR.Internal;
1112
using Microsoft.AspNetCore.SignalR.Protocol;
1213
using Microsoft.Extensions.DependencyInjection;
@@ -31,6 +32,9 @@ public class HubConnectionHandler<THub> : ConnectionHandler where THub : Hub
3132
private readonly bool _enableDetailedErrors;
3233
private readonly long? _maximumMessageSize;
3334

35+
// Internal for testing
36+
internal ISystemClock SystemClock { get; set; } = new SystemClock();
37+
3438
/// <summary>
3539
/// Initializes a new instance of the <see cref="HubConnectionHandler{THub}"/> class.
3640
/// </summary>
@@ -98,6 +102,7 @@ public override async Task OnConnectedAsync(ConnectionContext connection)
98102
ClientTimeoutInterval = _hubOptions.ClientTimeoutInterval ?? _globalHubOptions.ClientTimeoutInterval ?? HubOptionsSetup.DefaultClientTimeoutInterval,
99103
StreamBufferCapacity = _hubOptions.StreamBufferCapacity ?? _globalHubOptions.StreamBufferCapacity ?? HubOptionsSetup.DefaultStreamBufferCapacity,
100104
MaximumReceiveMessageSize = _maximumMessageSize,
105+
SystemClock = SystemClock,
101106
};
102107

103108
Log.ConnectedStarting(_logger);

src/SignalR/server/SignalR/test/HubConnectionHandlerTests.cs

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2666,22 +2666,19 @@ public async Task WritesPingMessageIfNothingWrittenWhenKeepAliveIntervalElapses(
26662666
services.Configure<HubOptions>(options =>
26672667
options.KeepAliveInterval = TimeSpan.FromMilliseconds(interval)), LoggerFactory);
26682668
var connectionHandler = serviceProvider.GetService<HubConnectionHandler<MethodHub>>();
2669+
connectionHandler.SystemClock = clock;
26692670

26702671
using (var client = new TestClient(new NewtonsoftJsonHubProtocol()))
26712672
{
2672-
client.Connection.Features.Set<ISystemClock>(clock);
2673-
26742673
var connectionHandlerTask = await client.ConnectAsync(connectionHandler);
26752674
await client.Connected.OrTimeout();
26762675

2677-
// Trigger multiple keep alives, but make sure to yield some time up to unblock concurrent threads
2678-
// This is useful on AppVeyor because it's slow enough to end up with no time
2679-
// being available for the endpoint to run.
2680-
for (var i = 0; i < 5; i += 1)
2676+
// Trigger multiple keep alives
2677+
var heartbeatCount = 5;
2678+
for (var i = 0; i < heartbeatCount; i++)
26812679
{
26822680
clock.UtcNow = clock.UtcNow.AddMilliseconds(interval + 1);
26832681
client.TickHeartbeat();
2684-
await Task.Yield();
26852682
}
26862683

26872684
// Shut down
@@ -2715,7 +2712,7 @@ public async Task WritesPingMessageIfNothingWrittenWhenKeepAliveIntervalElapses(
27152712
break;
27162713
}
27172714
}
2718-
Assert.InRange(pingCounter, 1, Int32.MaxValue);
2715+
Assert.Equal(heartbeatCount, pingCounter);
27192716
}
27202717
}
27212718
}
@@ -2731,11 +2728,10 @@ public async Task ConnectionNotTimedOutIfClientNeverPings()
27312728
services.Configure<HubOptions>(options =>
27322729
options.ClientTimeoutInterval = TimeSpan.FromMilliseconds(timeout)), LoggerFactory);
27332730
var connectionHandler = serviceProvider.GetService<HubConnectionHandler<MethodHub>>();
2731+
connectionHandler.SystemClock = clock;
27342732

27352733
using (var client = new TestClient(new NewtonsoftJsonHubProtocol()))
27362734
{
2737-
client.Connection.Features.Set<ISystemClock>(clock);
2738-
27392735
var connectionHandlerTask = await client.ConnectAsync(connectionHandler);
27402736
await client.Connected.OrTimeout();
27412737
// This is a fake client -- it doesn't auto-ping to signal
@@ -2768,21 +2764,17 @@ public async Task ConnectionTimesOutIfInitialPingAndThenNoMessages()
27682764
services.Configure<HubOptions>(options =>
27692765
options.ClientTimeoutInterval = TimeSpan.FromMilliseconds(timeout)), LoggerFactory);
27702766
var connectionHandler = serviceProvider.GetService<HubConnectionHandler<MethodHub>>();
2767+
connectionHandler.SystemClock = clock;
27712768

27722769
using (var client = new TestClient(new NewtonsoftJsonHubProtocol()))
27732770
{
2774-
client.Connection.Features.Set<ISystemClock>(clock);
2775-
27762771
var connectionHandlerTask = await client.ConnectAsync(connectionHandler);
27772772
await client.Connected.OrTimeout();
27782773
await client.SendHubMessageAsync(PingMessage.Instance);
27792774

27802775
clock.UtcNow = clock.UtcNow.AddMilliseconds(timeout + 1);
27812776
client.TickHeartbeat();
27822777

2783-
clock.UtcNow = clock.UtcNow.AddMilliseconds(timeout + 1);
2784-
client.TickHeartbeat();
2785-
27862778
await connectionHandlerTask.OrTimeout();
27872779
}
27882780
}
@@ -2800,11 +2792,10 @@ public async Task ReceivingMessagesPreventsConnectionTimeoutFromOccuring()
28002792
services.Configure<HubOptions>(options =>
28012793
options.ClientTimeoutInterval = TimeSpan.FromMilliseconds(timeout)), LoggerFactory);
28022794
var connectionHandler = serviceProvider.GetService<HubConnectionHandler<MethodHub>>();
2795+
connectionHandler.SystemClock = clock;
28032796

28042797
using (var client = new TestClient(new NewtonsoftJsonHubProtocol()))
28052798
{
2806-
client.Connection.Features.Set<ISystemClock>(clock);
2807-
28082799
var connectionHandlerTask = await client.ConnectAsync(connectionHandler);
28092800
await client.Connected.OrTimeout();
28102801
await client.SendHubMessageAsync(PingMessage.Instance);

0 commit comments

Comments
 (0)