Skip to content

Stale check command for async connections #547

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import javax.net.ssl.SSLSession;

import org.apache.hc.core5.concurrent.CancellableDependency;
import org.apache.hc.core5.concurrent.FutureCallback;
import org.apache.hc.core5.http.ConnectionClosedException;
import org.apache.hc.core5.http.EndpointDetails;
import org.apache.hc.core5.http.Header;
Expand All @@ -65,6 +66,7 @@
import org.apache.hc.core5.http.nio.HandlerFactory;
import org.apache.hc.core5.http.nio.command.ExecutableCommand;
import org.apache.hc.core5.http.nio.command.ShutdownCommand;
import org.apache.hc.core5.http.nio.command.StaleCheckCommand;
import org.apache.hc.core5.http.protocol.HttpCoreContext;
import org.apache.hc.core5.http.protocol.HttpProcessor;
import org.apache.hc.core5.http2.H2ConnectionException;
Expand Down Expand Up @@ -410,6 +412,23 @@ private int generateStreamId() {
}
}

void doStalecheck(final FutureCallback<Boolean> callback) throws IOException {
if (!ioSession.isOpen() || connState.compareTo(ConnectionHandshake.ACTIVE) > 0) {
callback.completed(false);
}
final ByteBuffer buffer = ByteBuffer.allocate(0);
final int bytesRead = ioSession.channel().read(buffer);
if (bytesRead == -1) {
if (connState == ConnectionHandshake.ACTIVE) {
connState = ConnectionHandshake.GRACEFUL_SHUTDOWN;
}
requestSessionOutput();
callback.completed(false);
} else {
callback.completed(true);
}
}

public final void onConnect() throws HttpException, IOException {
connState = ConnectionHandshake.ACTIVE;
final RawFrame settingsFrame = frameFactory.createSettings(
Expand Down Expand Up @@ -647,6 +666,8 @@ private void processPendingCommands() throws IOException, HttpException {
if (!outputQueue.isEmpty()) {
return;
}
} else if (command instanceof StaleCheckCommand) {
doStalecheck(((StaleCheckCommand) command).getCallback());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import javax.net.ssl.SSLHandshakeException;
import javax.net.ssl.SSLSession;

import org.apache.hc.core5.concurrent.FutureCallback;
import org.apache.hc.core5.http.ConnectionClosedException;
import org.apache.hc.core5.http.ContentLengthStrategy;
import org.apache.hc.core5.http.EndpointDetails;
Expand Down Expand Up @@ -68,6 +69,7 @@
import org.apache.hc.core5.http.nio.command.CommandSupport;
import org.apache.hc.core5.http.nio.command.RequestExecutionCommand;
import org.apache.hc.core5.http.nio.command.ShutdownCommand;
import org.apache.hc.core5.http.nio.command.StaleCheckCommand;
import org.apache.hc.core5.io.CloseMode;
import org.apache.hc.core5.io.SocketTimeoutExceptionFactory;
import org.apache.hc.core5.reactor.Command;
Expand Down Expand Up @@ -244,6 +246,8 @@ private void processCommands() throws HttpException, IOException {
execute((RequestExecutionCommand) command);
return;
}
} else if (command instanceof StaleCheckCommand) {
doStalecheck(((StaleCheckCommand) command).getCallback());
} else {
throw new HttpException("Unexpected command: " + command.getClass());
}
Expand Down Expand Up @@ -429,6 +433,20 @@ void requestShutdown(final CloseMode closeMode) {
ioSession.setEvent(SelectionKey.OP_WRITE);
}

void doStalecheck(final FutureCallback<Boolean> callback) throws IOException {
if (!ioSession.isOpen() || connState.compareTo(ConnectionState.ACTIVE) > 0) {
callback.completed(false);
}
final ByteBuffer buffer = ByteBuffer.allocate(0);
final int bytesRead = ioSession.channel().read(buffer);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this read is actually redundant. Command is modeled as a write event, and reads are processed before writes. So if the connection is closed, we'll already know by the time we get here, provided that we include the changes from #543.

The end result ends up being very similar to the "synchronization barrier" between the event loop and the connection pool that I was talking about in that PR. The internal race condition basically goes away as long as connection reuse is completed through the event loop, which then has a chance to update all the relevant bookkeeping with respect to whatever IO events are pending.

if (bytesRead == -1) {
requestShutdown(CloseMode.GRACEFUL);
callback.completed(false);
} else {
callback.completed(true);
}
}

void commitMessageHead(
final OutgoingMessage messageHead,
final boolean endStream,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/

package org.apache.hc.core5.http.nio.command;

import org.apache.hc.core5.annotation.Internal;
import org.apache.hc.core5.concurrent.FutureCallback;
import org.apache.hc.core5.reactor.Command;
import org.apache.hc.core5.util.Args;

/**
* Stale check command.
*
* @since 5.4
*/
@Internal
public final class StaleCheckCommand implements Command {

private final FutureCallback<Boolean> callback;

public StaleCheckCommand(final FutureCallback<Boolean> callback) {
this.callback = Args.notNull(callback, "Callback");
}

public FutureCallback<Boolean> getCallback() {
return callback;
}

@Override
public boolean cancel() {
callback.cancelled();
return true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cancel the callback

}

}
Loading