-
Notifications
You must be signed in to change notification settings - Fork 10.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
320 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 0 additions & 112 deletions
112
src/Servers/Kestrel/Core/src/Internal/Http2/Http2ConnectionKeepAlive.cs
This file was deleted.
Oops, something went wrong.
116 changes: 116 additions & 0 deletions
116
src/Servers/Kestrel/Core/src/Internal/Http2/Http2KeepAlive.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
// 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. | ||
|
||
using System; | ||
using System.Buffers; | ||
using System.Diagnostics; | ||
using System.Threading; | ||
using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure; | ||
|
||
namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http2 | ||
{ | ||
internal enum KeepAliveState | ||
{ | ||
None, | ||
SendPing, | ||
PingSent, | ||
Timeout | ||
} | ||
|
||
internal class Http2KeepAlive | ||
{ | ||
internal static readonly ReadOnlySequence<byte> PingPayload = new ReadOnlySequence<byte>(new byte[8]); | ||
|
||
private readonly TimeSpan _keepAliveInterval; | ||
private readonly TimeSpan _keepAliveTimeout; | ||
private readonly ISystemClock _systemClock; | ||
private readonly object _lock = new object(); | ||
private KeepAliveState _state; | ||
private bool _bytesReceivedCurrentTick; | ||
private long _lastBytesReceivedTimestamp; | ||
private long _pingSentTimestamp; | ||
|
||
public Http2KeepAlive(TimeSpan keepAliveInterval, TimeSpan keepAliveTimeout, ISystemClock systemClock) | ||
{ | ||
_keepAliveInterval = keepAliveInterval; | ||
_keepAliveTimeout = keepAliveTimeout; | ||
_systemClock = systemClock; | ||
} | ||
|
||
internal KeepAliveState ProcessKeepAlive(bool dataReceived) | ||
{ | ||
lock (_lock) | ||
{ | ||
if (dataReceived) | ||
{ | ||
_bytesReceivedCurrentTick = true; | ||
} | ||
return _state; | ||
} | ||
} | ||
|
||
public void PingSent() | ||
{ | ||
lock (_lock) | ||
{ | ||
_state = KeepAliveState.PingSent; | ||
_pingSentTimestamp = _systemClock.UtcNowTicks; | ||
} | ||
} | ||
|
||
public void PingAckReceived() | ||
{ | ||
lock (_lock) | ||
{ | ||
if (_state == KeepAliveState.PingSent) | ||
{ | ||
_pingSentTimestamp = 0; | ||
_state = KeepAliveState.None; | ||
} | ||
} | ||
} | ||
|
||
public void Tick(DateTimeOffset now) | ||
{ | ||
var timestamp = now.Ticks; | ||
|
||
lock (_lock) | ||
{ | ||
// Bytes were received since the last tick. | ||
// Update a timestamp of when bytes were last received. | ||
if (_bytesReceivedCurrentTick) | ||
{ | ||
_lastBytesReceivedTimestamp = timestamp; | ||
_bytesReceivedCurrentTick = false; | ||
} | ||
|
||
switch (_state) | ||
{ | ||
case KeepAliveState.None: | ||
// Check whether keep alive interval has passed since last bytes received | ||
if (timestamp > (_lastBytesReceivedTimestamp + _keepAliveInterval.Ticks)) | ||
{ | ||
_state = KeepAliveState.SendPing; | ||
} | ||
return; | ||
case KeepAliveState.SendPing: | ||
return; | ||
case KeepAliveState.PingSent: | ||
if (_keepAliveTimeout != Timeout.InfiniteTimeSpan) | ||
{ | ||
if (timestamp > (_pingSentTimestamp + _keepAliveTimeout.Ticks)) | ||
{ | ||
_pingSentTimestamp = 0; | ||
_state = KeepAliveState.Timeout; | ||
} | ||
} | ||
return; | ||
case KeepAliveState.Timeout: | ||
return; | ||
} | ||
} | ||
|
||
Debug.Fail("Should never reach here."); | ||
} | ||
} | ||
} |
Oops, something went wrong.