-
Notifications
You must be signed in to change notification settings - Fork 981
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
Allocation-free Write(AndFlush)Async using ValueTask #375
Open
maksimkim
wants to merge
29
commits into
Azure:dev
Choose a base branch
from
maksimkim:writevaluetask
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
0be4706
custom alloc free custom awaitable
maksimkim ff944b2
ChannelFuture -> ValueTask
maksimkim 2b6f37d
tls fixes
maksimkim 47ff644
single callback per future + callback execution on executor
maksimkim 4f8f30e
uv changes
maksimkim c70594a
continuations and recycle are inlined
maksimkim eef526d
test fixes
maksimkim 8abd878
revert libuv changes
maksimkim cd0b11e
cleanup
maksimkim 1cef415
update packages
maksimkim 5bed79f
multi continuation support returned back
maksimkim d0836b0
using nuget.config on pkg restore
maksimkim ed79cce
add nuget.org source
maksimkim 208e69c
rename
maksimkim c9d0c8a
execution and sync context preservation
maksimkim 67b306b
context propagation fixes
maksimkim 619bfb8
post rebase fixes
maksimkim baa5e3a
multicontinuation fix for timeout handler
maksimkim 3428e2f
switch to nuget.org for tasks.extensions package
maksimkim eb5ab1b
default WriteAndFlushAsync returns Task
maksimkim 4dcb8bb
post rebase fixes
maksimkim 78ec006
remove nuget config
maksimkim fc447d7
upgrade packages to stable version
maksimkim 03e7b1d
ws fixes
maksimkim 5fab149
bump up package versions
maksimkim 6c99f3a
http test fixes
maksimkim 05e4aa6
fix keep alive implementation
maksimkim 7f08e1d
disable configureawait
maksimkim c1c9865
package version fixes
maksimkim 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 was deleted.
Oops, something went wrong.
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,12 +15,13 @@ public class WebSocketServerExtensionHandler : ChannelHandlerAdapter | |
readonly List<IWebSocketServerExtensionHandshaker> extensionHandshakers; | ||
|
||
List<IWebSocketServerExtension> validExtensions; | ||
Action<Task, object> upgradeCompletedContinuation; | ||
|
||
public WebSocketServerExtensionHandler(params IWebSocketServerExtensionHandshaker[] extensionHandshakers) | ||
{ | ||
Contract.Requires(extensionHandshakers != null && extensionHandshakers.Length > 0); | ||
|
||
this.extensionHandshakers = new List<IWebSocketServerExtensionHandshaker>(extensionHandshakers); | ||
this.upgradeCompletedContinuation = this.OnUpgradeCompleted; | ||
} | ||
|
||
public override void ChannelRead(IChannelHandlerContext ctx, object msg) | ||
|
@@ -67,16 +68,16 @@ public override void ChannelRead(IChannelHandlerContext ctx, object msg) | |
base.ChannelRead(ctx, msg); | ||
} | ||
|
||
public override Task WriteAsync(IChannelHandlerContext ctx, object msg) | ||
public override ValueTask WriteAsync(IChannelHandlerContext ctx, object msg) | ||
{ | ||
Action<Task> continuationAction = null; | ||
|
||
HttpHeaders responseHeaders; | ||
string headerValue = null; | ||
|
||
if (msg is IHttpResponse response | ||
&& WebSocketExtensionUtil.IsWebsocketUpgrade(response.Headers) | ||
&& WebSocketExtensionUtil.IsWebsocketUpgrade(responseHeaders = response.Headers) | ||
&& this.validExtensions != null) | ||
{ | ||
string headerValue = null; | ||
if (response.Headers.TryGet(HttpHeaderNames.SecWebsocketExtensions, out ICharSequence value)) | ||
if (responseHeaders.TryGet(HttpHeaderNames.SecWebsocketExtensions, out ICharSequence value)) | ||
{ | ||
headerValue = value?.ToString(); | ||
} | ||
|
@@ -88,31 +89,33 @@ public override Task WriteAsync(IChannelHandlerContext ctx, object msg) | |
extensionData.Name, extensionData.Parameters); | ||
} | ||
|
||
continuationAction = promise => | ||
{ | ||
if (promise.Status == TaskStatus.RanToCompletion) | ||
{ | ||
foreach (IWebSocketServerExtension extension in this.validExtensions) | ||
{ | ||
WebSocketExtensionDecoder decoder = extension.NewExtensionDecoder(); | ||
WebSocketExtensionEncoder encoder = extension.NewExtensionEncoder(); | ||
ctx.Channel.Pipeline.AddAfter(ctx.Name, decoder.GetType().Name, decoder); | ||
ctx.Channel.Pipeline.AddAfter(ctx.Name, encoder.GetType().Name, encoder); | ||
} | ||
} | ||
ctx.Channel.Pipeline.Remove(ctx.Name); | ||
}; | ||
|
||
if (headerValue != null) | ||
{ | ||
response.Headers.Set(HttpHeaderNames.SecWebsocketExtensions, headerValue); | ||
responseHeaders.Set(HttpHeaderNames.SecWebsocketExtensions, headerValue); | ||
} | ||
|
||
Task task = base.WriteAsync(ctx, msg).AsTask(); | ||
task.ContinueWith(this.upgradeCompletedContinuation, ctx, TaskContinuationOptions.ExecuteSynchronously); | ||
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. |
||
return new ValueTask(task); | ||
} | ||
|
||
return continuationAction == null | ||
? base.WriteAsync(ctx, msg) | ||
: base.WriteAsync(ctx, msg) | ||
.ContinueWith(continuationAction, TaskContinuationOptions.ExecuteSynchronously); | ||
return base.WriteAsync(ctx, msg); | ||
} | ||
|
||
void OnUpgradeCompleted(Task task, object state) | ||
{ | ||
var ctx = (IChannelHandlerContext)state; | ||
if (task.Status == TaskStatus.RanToCompletion) | ||
{ | ||
foreach (IWebSocketServerExtension extension in this.validExtensions) | ||
{ | ||
WebSocketExtensionDecoder decoder = extension.NewExtensionDecoder(); | ||
WebSocketExtensionEncoder encoder = extension.NewExtensionEncoder(); | ||
ctx.Channel.Pipeline.AddAfter(ctx.Name, decoder.GetType().Name, decoder); | ||
ctx.Channel.Pipeline.AddAfter(ctx.Name, encoder.GetType().Name, encoder); | ||
} | ||
} | ||
ctx.Channel.Pipeline.Remove(ctx.Name); | ||
} | ||
} | ||
} |
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
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.
@StormHub, check this out. I change it to align with netty. So upgrade doesn't wait write to complete.
https://github.com/netty/netty/blob/00afb19d7a37de21b35ce4f6cb3fa7f74809f2ab/codec-http/src/main/java/io/netty/handler/codec/http/HttpServerUpgradeHandler.java#L324