-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Configure ping in HTTP2 #40257
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
Configure ping in HTTP2 #40257
Changes from 4 commits
17ddbce
2afc76f
c590b7c
eb37332
712479f
95bc2f3
de89629
b3e4b3d
e06f092
6e2f371
b7ed6ce
20f44cf
b684730
014c8d6
60a757d
886a2da
863f636
a987919
b221f43
be468cd
db89b58
02785d6
d354a29
b83cfc3
63e3d3f
258f1c4
2695f2e
0ea45e8
7078d8e
9c1b6e7
86c8413
c10beb9
b9c6b33
40e0e5c
3a753b7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| namespace System.Net.Http | ||
| { | ||
| public enum HttpKeepAlivePingPolicy | ||
| { | ||
| WithActiveRequests, | ||
| Always | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
| using System.Buffers.Binary; | ||
| using System.Collections.Generic; | ||
| using System.Diagnostics; | ||
| using System.Diagnostics.CodeAnalysis; | ||
| using System.IO; | ||
| using System.Net.Http.Headers; | ||
| using System.Net.Http.HPack; | ||
| using System.Net.Security; | ||
| using System.Runtime.CompilerServices; | ||
| using System.Text; | ||
| using System.Threading; | ||
| using System.Threading.Channels; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace System.Net.Http | ||
| { | ||
| internal sealed partial class Http2Connection | ||
| { | ||
| internal enum KeepAliveState | ||
| { | ||
| None, | ||
| PingSent | ||
| } | ||
|
|
||
| internal class Http2KeepAlive | ||
aik-jahoda marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
|
|
||
|
||
| private long _pingPayload; | ||
|
|
||
| private readonly TimeSpan _keepAlivePingDelay; | ||
| private readonly TimeSpan _keepAlivePingTimeout; | ||
| private DateTimeOffset _nextPingRequestTimestamp; | ||
aik-jahoda marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| private DateTimeOffset _pingTimeoutTimestamp; | ||
aik-jahoda marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| private HttpKeepAlivePingPolicy _keepAlivePingPolicy; | ||
| private KeepAliveState _state; | ||
| private Http2Connection _connection; | ||
|
|
||
| public Http2KeepAlive(Http2Connection connection, HttpConnectionSettings settings) | ||
| { | ||
| _keepAlivePingDelay = settings._keepAlivePingDelay; | ||
| _keepAlivePingTimeout = settings._keepAlivePingTimeout; | ||
| _nextPingRequestTimestamp = DateTimeOffset.Now.Add(settings._keepAlivePingDelay); | ||
aik-jahoda marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| _keepAlivePingPolicy = settings._keepAlivePingPolicy; | ||
| _connection = connection; | ||
| } | ||
|
|
||
| public void ProcessFrame() | ||
aik-jahoda marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| _nextPingRequestTimestamp = DateTimeOffset.Now.Add(_keepAlivePingDelay); | ||
| } | ||
|
|
||
| public bool ProcessPingAck(long payload) | ||
| { | ||
| if (_state != KeepAliveState.PingSent) | ||
| return false; | ||
| if (Interlocked.Read(ref _pingPayload) != payload) | ||
| return false; | ||
| _state = KeepAliveState.None; | ||
| ProcessFrame(); | ||
| return true; | ||
| } | ||
|
|
||
| public void VerifyKeepAlive() | ||
| { | ||
| if (_keepAlivePingPolicy == HttpKeepAlivePingPolicy.WithActiveRequests && _connection._httpStreams.Count == 0) | ||
| return; | ||
|
|
||
| var now = DateTimeOffset.Now; | ||
| switch (_state) | ||
aik-jahoda marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| case KeepAliveState.None: | ||
| // Check whether keep alive delay has passed since last frame received | ||
| if (_keepAlivePingDelay > TimeSpan.Zero && now > _nextPingRequestTimestamp) | ||
aik-jahoda marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| // Set the status directly to ping sent and set the timestamp | ||
| _state = KeepAliveState.PingSent; | ||
| _pingTimeoutTimestamp = now.Add(_keepAlivePingTimeout); | ||
| Interlocked.Increment(ref _pingPayload); | ||
| _connection.SendPingAsync(_pingPayload); | ||
aik-jahoda marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return; | ||
| } | ||
| break; | ||
| case KeepAliveState.PingSent: | ||
| if (_keepAlivePingTimeout != TimeSpan.MaxValue) | ||
aik-jahoda marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| if (now > _pingTimeoutTimestamp) | ||
| Http2Connection.ThrowProtocolError(); | ||
| } | ||
|
|
||
| break; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -1689,6 +1689,17 @@ private static bool GetIsWindows7Or2008R2() | |||||
| return false; | ||||||
| } | ||||||
|
|
||||||
| internal void HeartBeat() | ||||||
| { | ||||||
| Http2Connection[]? localHttp2Connections = _http2Connections; | ||||||
aik-jahoda marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| if (localHttp2Connections != null) | ||||||
| { | ||||||
| foreach (var http2Connection in localHttp2Connections) | ||||||
aik-jahoda marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| http2Connection?.HeartBeat(); | ||||||
|
||||||
| http2Connection?.HeartBeat(); | |
| http2Connection.HeartBeat(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed, AFAIK this will never be null, the array gets resized with each addition/removal.
Uh oh!
There was an error while loading. Please reload this page.