This repository was archived by the owner on Dec 18, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 515
Make all write operations atomic #562
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e90b61e
Move call to CopyFrom in SocketOutput.WriteAsync inside lock to make …
halter73 df695ac
Use TryEnter to acquire SocketOutput._contextLock on the libuv event …
halter73 daa2b7e
Remove _tasksCompleted from SocketOutput
halter73 ab5ef54
Make chunked writes atomic
halter73 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| // 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.Text; | ||
| using Microsoft.AspNet.Server.Kestrel.Infrastructure; | ||
|
|
||
| namespace Microsoft.AspNet.Server.Kestrel.Http | ||
| { | ||
| public static class ChunkWriter | ||
| { | ||
| private static readonly ArraySegment<byte> _endChunkBytes = CreateAsciiByteArraySegment("\r\n"); | ||
| private static readonly byte[] _hex = Encoding.ASCII.GetBytes("0123456789abcdef"); | ||
|
|
||
| private static ArraySegment<byte> CreateAsciiByteArraySegment(string text) | ||
| { | ||
| var bytes = Encoding.ASCII.GetBytes(text); | ||
| return new ArraySegment<byte>(bytes); | ||
| } | ||
|
|
||
| public static ArraySegment<byte> BeginChunkBytes(int dataCount) | ||
| { | ||
| var bytes = new byte[10] | ||
| { | ||
| _hex[((dataCount >> 0x1c) & 0x0f)], | ||
| _hex[((dataCount >> 0x18) & 0x0f)], | ||
| _hex[((dataCount >> 0x14) & 0x0f)], | ||
| _hex[((dataCount >> 0x10) & 0x0f)], | ||
| _hex[((dataCount >> 0x0c) & 0x0f)], | ||
| _hex[((dataCount >> 0x08) & 0x0f)], | ||
| _hex[((dataCount >> 0x04) & 0x0f)], | ||
| _hex[((dataCount >> 0x00) & 0x0f)], | ||
| (byte)'\r', | ||
| (byte)'\n', | ||
| }; | ||
|
|
||
| // Determine the most-significant non-zero nibble | ||
| int total, shift; | ||
| total = (dataCount > 0xffff) ? 0x10 : 0x00; | ||
| dataCount >>= total; | ||
| shift = (dataCount > 0x00ff) ? 0x08 : 0x00; | ||
| dataCount >>= shift; | ||
| total |= shift; | ||
| total |= (dataCount > 0x000f) ? 0x04 : 0x00; | ||
|
Contributor
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. I like this sorcery 👍
Contributor
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. Oh now I see it's just copied from another class :P
Member
Author
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. This was simply moved from Frame. Credit @lodejard. |
||
|
|
||
| var offset = 7 - (total >> 2); | ||
| return new ArraySegment<byte>(bytes, offset, 10 - offset); | ||
| } | ||
|
|
||
| public static int WriteBeginChunkBytes(ref MemoryPoolIterator2 start, int dataCount) | ||
| { | ||
| var chunkSegment = BeginChunkBytes(dataCount); | ||
| start.CopyFrom(chunkSegment); | ||
| return chunkSegment.Count; | ||
| } | ||
|
|
||
| public static void WriteEndChunkBytes(ref MemoryPoolIterator2 start) | ||
| { | ||
| start.CopyFrom(_endChunkBytes); | ||
| } | ||
| } | ||
| } | ||
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
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.
Remove TODO?
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 still a todo though. Someday...
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.
What's preventing us from addressing it?
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.
Debugging. All I know is calling
WriteAsynconSslStreamdidn't work when last I tried it. I think the call never completed, but I'm not 100% on that. You are definitely right that I should have left a better comment when I first wrote this code.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.
Can you retest post #567 ? Ran into issue #565 with Ssl WriteAsync as its sync.
On the upside; I think I fixed the issue :)
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.
K, so tests fail changing this to async. Will dig in and do a different PR if I get a resolution. Added issue #569
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.
@benaadams Thanks for looking into this.