-
Notifications
You must be signed in to change notification settings - Fork 24.9k
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
Immediately flush channel after writing to buffer #31301
Merged
+54
−30
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b430b66
Immediately flush channel after writing to buffer
Tim-Brooks 419d161
Merge remote-tracking branch 'upstream/master' into optimistic_flush
Tim-Brooks b0f92d4
Merge remote-tracking branch 'upstream/master' into optimistic_flush
Tim-Brooks 99c174c
Changes
Tim-Brooks 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 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 |
---|---|---|
|
@@ -43,9 +43,6 @@ | |
* {@link #runLoop()}, the selector will run until {@link #close()} is called. This instance handles closing | ||
* of channels. Users should call {@link #queueChannelClose(NioChannel)} to schedule a channel for close by | ||
* this selector. | ||
* <p> | ||
* Children of this class should implement the specific {@link #processKey(SelectionKey)}, | ||
* {@link #preSelect()}, and {@link #cleanup()} functionality. | ||
*/ | ||
public class NioSelector implements Closeable { | ||
|
||
|
@@ -165,7 +162,7 @@ void singleLoop() { | |
} | ||
|
||
void cleanupAndCloseChannels() { | ||
cleanup(); | ||
cleanupPendingWrites(); | ||
channelsToClose.addAll(channelsToRegister); | ||
channelsToRegister.clear(); | ||
channelsToClose.addAll(selector.keys().stream().map(sk -> (ChannelContext<?>) sk.attachment()).collect(Collectors.toList())); | ||
|
@@ -219,7 +216,7 @@ void processKey(SelectionKey selectionKey) { | |
handleRead(channelContext); | ||
} | ||
} | ||
eventHandler.postHandling(channelContext); | ||
eventHandler.postSocketChannelHandling(channelContext); | ||
} | ||
|
||
} | ||
|
@@ -234,16 +231,6 @@ void preSelect() { | |
handleQueuedWrites(); | ||
} | ||
|
||
/** | ||
* Called once as the selector is being closed. | ||
*/ | ||
void cleanup() { | ||
WriteOperation op; | ||
while ((op = queuedWrites.poll()) != null) { | ||
executeFailedListener(op.getListener(), new ClosedSelectorException()); | ||
} | ||
} | ||
|
||
/** | ||
* Queues a write operation to be handled by the event loop. This can be called by any thread and is the | ||
* api available for non-selector threads to schedule writes. | ||
|
@@ -284,20 +271,29 @@ public void scheduleForRegistration(NioChannel channel) { | |
} | ||
|
||
/** | ||
* Queues a write operation directly in a channel's buffer. Channel buffers are only safe to be accessed | ||
* by the selector thread. As a result, this method should only be called by the selector thread. | ||
* Queues a write operation directly in a channel's buffer. If this channel does not have pending writes | ||
* already, the channel will be flushed. Channel buffers are only safe to be accessed by the selector | ||
* thread. As a result, this method should only be called by the selector thread. If this channel does | ||
* not have pending writes already, the channel will be flushed. | ||
* | ||
* @param writeOperation to be queued in a channel's buffer | ||
*/ | ||
public void queueWriteInChannelBuffer(WriteOperation writeOperation) { | ||
public void writeToChannel(WriteOperation writeOperation) { | ||
assertOnSelectorThread(); | ||
SocketChannelContext context = writeOperation.getChannel(); | ||
boolean shouldFlush = context.readyForFlush() == false; | ||
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 logic doesn't look intuitive to me. why should we flush if the context is not ready for it? maybe it's a naming issue? |
||
try { | ||
SelectionKeyUtils.setWriteInterested(context.getSelectionKey()); | ||
context.queueWriteOperation(writeOperation); | ||
} catch (Exception e) { | ||
shouldFlush = false; | ||
executeFailedListener(writeOperation.getListener(), e); | ||
} | ||
|
||
if (shouldFlush) { | ||
handleWrite(context); | ||
eventHandler.postSocketChannelHandling(context); | ||
} | ||
} | ||
|
||
/** | ||
|
@@ -332,6 +328,13 @@ public <V> void executeFailedListener(BiConsumer<V, Exception> listener, Excepti | |
} | ||
} | ||
|
||
private void cleanupPendingWrites() { | ||
WriteOperation op; | ||
while ((op = queuedWrites.poll()) != null) { | ||
executeFailedListener(op.getListener(), new ClosedSelectorException()); | ||
} | ||
} | ||
|
||
private void wakeup() { | ||
// TODO: Do we need the wakeup optimizations that some other libraries use? | ||
selector.wakeup(); | ||
|
@@ -394,7 +397,7 @@ private void handleQueuedWrites() { | |
WriteOperation writeOperation; | ||
while ((writeOperation = queuedWrites.poll()) != null) { | ||
if (writeOperation.getChannel().isOpen()) { | ||
queueWriteInChannelBuffer(writeOperation); | ||
writeToChannel(writeOperation); | ||
} else { | ||
executeFailedListener(writeOperation.getListener(), new ClosedChannelException()); | ||
} | ||
|
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
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.
I liked postHandling better since
SocketChannel
is kind of part of the signature due to the argument? WDYT?