-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Drain requests in native instead of managed #6816
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
Merged
Merged
Changes from 14 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
7043907
Fix draining issue
jkotalik de4de2b
Cleanup
jkotalik 8a9504f
minor cleanup
jkotalik 6092b95
initialize to 0
jkotalik 7075c38
All the feedback
jkotalik 397994e
Cleanup
jkotalik b205c64
More feedback
jkotalik 2a6a18d
Removing more places we call stop managed callbacks
jkotalik c55e436
feedback and add another shutdown test file
jkotalik 0606ad7
Rename went wrong
jkotalik 1eac070
Feedback:
jkotalik 4e79bb0
Small nits
jkotalik bd4196d
Fix 0 request scenario
jkotalik 12576c8
Keep registration code
jkotalik 0084cde
Closure
jkotalik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
// 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; | ||
|
@@ -23,6 +25,7 @@ internal class IISHttpServer : IServer | |
private static readonly NativeMethods.PFN_SHUTDOWN_HANDLER _shutdownHandler = HandleShutdown; | ||
private static readonly NativeMethods.PFN_DISCONNECT_HANDLER _onDisconnect = OnDisconnect; | ||
private static readonly NativeMethods.PFN_ASYNC_COMPLETION _onAsyncCompletion = OnAsyncCompletion; | ||
private static readonly NativeMethods.PFN_REQUESTS_DRAINED_HANDLER _requestsDrainedHandler = OnRequestsDrained; | ||
|
||
private IISContextFactory _iisContextFactory; | ||
private readonly MemoryPool<byte> _memoryPool = new SlabMemoryPool(); | ||
|
@@ -33,11 +36,9 @@ internal class IISHttpServer : IServer | |
private readonly IISNativeApplication _nativeApplication; | ||
private readonly ServerAddressesFeature _serverAddressesFeature; | ||
|
||
private volatile int _stopping; | ||
private bool Stopping => _stopping == 1; | ||
private int _outstandingRequests; | ||
private readonly TaskCompletionSource<object> _shutdownSignal = new TaskCompletionSource<object>(TaskCreationOptions.RunContinuationsAsynchronously); | ||
private bool? _websocketAvailable; | ||
private CancellationTokenRegistration _cancellationTokenRegistration; | ||
|
||
public IFeatureCollection Features { get; } = new FeatureCollection(); | ||
|
||
|
@@ -86,7 +87,7 @@ public Task StartAsync<TContext>(IHttpApplication<TContext> application, Cancell | |
_httpServerHandle = GCHandle.Alloc(this); | ||
|
||
_iisContextFactory = new IISContextFactory<TContext>(_memoryPool, application, _options, this, _logger); | ||
_nativeApplication.RegisterCallbacks(_requestHandler, _shutdownHandler, _onDisconnect, _onAsyncCompletion, (IntPtr)_httpServerHandle, (IntPtr)_httpServerHandle); | ||
_nativeApplication.RegisterCallbacks(_requestHandler, _shutdownHandler, _onDisconnect, _onAsyncCompletion, _requestsDrainedHandler, (IntPtr)_httpServerHandle, (IntPtr)_httpServerHandle); | ||
|
||
_serverAddressesFeature.Addresses = _options.ServerAddresses; | ||
|
||
|
@@ -95,50 +96,18 @@ public Task StartAsync<TContext>(IHttpApplication<TContext> application, Cancell | |
|
||
public Task StopAsync(CancellationToken cancellationToken) | ||
{ | ||
void RegisterCancelation() | ||
{ | ||
cancellationToken.Register(() => | ||
{ | ||
_nativeApplication.StopCallsIntoManaged(); | ||
_shutdownSignal.TrySetResult(null); | ||
}); | ||
} | ||
if (Interlocked.Exchange(ref _stopping, 1) == 1) | ||
{ | ||
RegisterCancelation(); | ||
|
||
return _shutdownSignal.Task; | ||
} | ||
|
||
// First call back into native saying "DON'T SEND ME ANY MORE REQUESTS" | ||
_nativeApplication.StopIncomingRequests(); | ||
|
||
try | ||
_cancellationTokenRegistration = cancellationToken.Register(() => | ||
{ | ||
// Wait for active requests to drain | ||
if (_outstandingRequests > 0) | ||
{ | ||
RegisterCancelation(); | ||
} | ||
else | ||
{ | ||
// We have drained all requests. Block any callbacks into managed at this point. | ||
_nativeApplication.StopCallsIntoManaged(); | ||
_shutdownSignal.TrySetResult(null); | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
_shutdownSignal.TrySetException(ex); | ||
} | ||
_shutdownSignal.TrySetResult(null); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Closure |
||
}); | ||
|
||
return _shutdownSignal.Task; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
_stopping = 1; | ||
|
||
// Block any more calls into managed from native as we are unloading. | ||
_nativeApplication.StopCallsIntoManaged(); | ||
_shutdownSignal.TrySetResult(null); | ||
|
@@ -152,29 +121,13 @@ public void Dispose() | |
_nativeApplication.Dispose(); | ||
} | ||
|
||
private void IncrementRequests() | ||
{ | ||
Interlocked.Increment(ref _outstandingRequests); | ||
} | ||
|
||
internal void DecrementRequests() | ||
{ | ||
if (Interlocked.Decrement(ref _outstandingRequests) == 0 && Stopping) | ||
{ | ||
// All requests have been drained. | ||
_nativeApplication.StopCallsIntoManaged(); | ||
_shutdownSignal.TrySetResult(null); | ||
} | ||
} | ||
|
||
private static NativeMethods.REQUEST_NOTIFICATION_STATUS HandleRequest(IntPtr pInProcessHandler, IntPtr pvRequestContext) | ||
{ | ||
IISHttpServer server = null; | ||
try | ||
{ | ||
// Unwrap the server so we can create an http context and process the request | ||
server = (IISHttpServer)GCHandle.FromIntPtr(pvRequestContext).Target; | ||
server.IncrementRequests(); | ||
|
||
var context = server._iisContextFactory.CreateHttpContext(pInProcessHandler); | ||
|
||
|
@@ -205,7 +158,6 @@ private static bool HandleShutdown(IntPtr pvRequestContext) | |
return true; | ||
} | ||
|
||
|
||
private static void OnDisconnect(IntPtr pvManagedHttpContext) | ||
{ | ||
IISHttpContext context = null; | ||
|
@@ -237,6 +189,23 @@ private static NativeMethods.REQUEST_NOTIFICATION_STATUS OnAsyncCompletion(IntPt | |
} | ||
} | ||
|
||
private static void OnRequestsDrained(IntPtr serverContext) | ||
{ | ||
IISHttpServer server = null; | ||
try | ||
{ | ||
server = (IISHttpServer)GCHandle.FromIntPtr(serverContext).Target; | ||
|
||
server._nativeApplication.StopCallsIntoManaged(); | ||
server._shutdownSignal.TrySetResult(null); | ||
server._cancellationTokenRegistration.Dispose(); | ||
} | ||
catch (Exception ex) | ||
{ | ||
server?._logger.LogError(0, ex, $"Unexpected exception in {nameof(IISHttpServer)}.{nameof(OnRequestsDrained)}."); | ||
} | ||
} | ||
|
||
private class IISContextFactory<T> : IISContextFactory | ||
{ | ||
private readonly IHttpApplication<T> _application; | ||
|
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This is unrelated, I just noticed that we were checking the wrong wait timeout here.