Skip to content
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

Issue #12266 - InvocationType improvements and cleanups. #12596

Open
wants to merge 18 commits into
base: jetty-12.1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
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 @@ -14,6 +14,7 @@
package org.eclipse.jetty.client;

import java.util.Map;
import java.util.Objects;

import org.eclipse.jetty.util.Promise;
import org.eclipse.jetty.util.annotation.ManagedObject;
Expand All @@ -28,6 +29,7 @@ public abstract class AbstractHttpClientTransport extends ContainerLifeCycle imp

private HttpClient client;
private ConnectionPool.Factory factory;
private InvocationType invocationType = InvocationType.BLOCKING;

protected HttpClient getHttpClient()
{
Expand Down Expand Up @@ -60,4 +62,16 @@ protected void connectFailed(Map<String, Object> context, Throwable failure)
Promise<Connection> promise = (Promise<Connection>)context.get(HTTP_CONNECTION_PROMISE_CONTEXT_KEY);
promise.failed(failure);
}

@Override
public InvocationType getInvocationType()
{
return invocationType;
}

@Override
public void setInvocationType(InvocationType invocationType)
{
this.invocationType = Objects.requireNonNull(invocationType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,6 @@ public void onComplete(Result result)
{
HttpRequest request = (HttpRequest)result.getRequest();
ContentResponse response = new HttpContentResponse(result.getResponse(), getContent(), getMediaType(), getEncoding());
if (result.getResponseFailure() != null)
{
if (LOG.isDebugEnabled())
LOG.debug("Authentication challenge failed", result.getFailure());
forwardFailureComplete(request, result.getRequestFailure(), response, result.getResponseFailure());
return;
}

String authenticationAttribute = getAuthenticationAttribute();
HttpConversation conversation = request.getConversation();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,23 @@

import org.eclipse.jetty.client.transport.HttpDestination;
import org.eclipse.jetty.io.ClientConnectionFactory;
import org.eclipse.jetty.util.thread.Invocable;

/**
* {@link HttpClientTransport} represents what transport implementations should provide
* in order to plug-in a different transport for {@link HttpClient}.
* in order to plug in a different transport for {@link HttpClient}.
* <p>
* While the {@link HttpClient} APIs define the HTTP semantic (request, response, headers, etc.)
* <em>how</em> an HTTP exchange is carried over the network depends on implementations of this class.
* <p>
* The default implementation uses the HTTP protocol to carry over the network the HTTP exchange,
* but the HTTP exchange may also be carried using the FCGI protocol, the HTTP/2 protocol or,
* in future, other protocols.
* in the future, other protocols.
*/
public interface HttpClientTransport extends ClientConnectionFactory, HttpClient.Aware
public interface HttpClientTransport extends ClientConnectionFactory, HttpClient.Aware, Invocable
{
public static final String HTTP_DESTINATION_CONTEXT_KEY = "org.eclipse.jetty.client.destination";
public static final String HTTP_CONNECTION_PROMISE_CONTEXT_KEY = "org.eclipse.jetty.client.connection.promise";
String HTTP_DESTINATION_CONTEXT_KEY = "org.eclipse.jetty.client.destination";
String HTTP_CONNECTION_PROMISE_CONTEXT_KEY = "org.eclipse.jetty.client.connection.promise";

/**
* Sets the {@link HttpClient} instance on this transport.
Expand All @@ -45,15 +46,15 @@ public interface HttpClientTransport extends ClientConnectionFactory, HttpClient
* @param client the {@link HttpClient} that uses this transport.
*/
@Override
public void setHttpClient(HttpClient client);
void setHttpClient(HttpClient client);

/**
* Creates a new Origin with the given request.
*
* @param request the request that triggers the creation of the Origin
* @return an Origin that identifies a destination
*/
public Origin newOrigin(Request request);
Origin newOrigin(Request request);

/**
* Creates a new, transport-specific, {@link HttpDestination} object.
Expand All @@ -64,24 +65,38 @@ public interface HttpClientTransport extends ClientConnectionFactory, HttpClient
* @param origin the destination origin
* @return a new, transport-specific, {@link HttpDestination} object
*/
public Destination newDestination(Origin origin);
Destination newDestination(Origin origin);

/**
* Establishes a physical connection to the given {@code address}.
*
* @param address the address to connect to
* @param context the context information to establish the connection
*/
public void connect(SocketAddress address, Map<String, Object> context);
void connect(SocketAddress address, Map<String, Object> context);

/**
* @return the factory for ConnectionPool instances
*/
public ConnectionPool.Factory getConnectionPoolFactory();
ConnectionPool.Factory getConnectionPoolFactory();

/**
* Set the factory for ConnectionPool instances.
* @param factory the factory for ConnectionPool instances
*/
public void setConnectionPoolFactory(ConnectionPool.Factory factory);
void setConnectionPoolFactory(ConnectionPool.Factory factory);

/**
* @return the {@link InvocationType} associated with this {@code HttpClientTransport}.
Copy link
Contributor

Choose a reason for hiding this comment

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

The javadoc should explain that the invocation type is going to be used to figure out how to execute the listeners.

*/
@Override
default InvocationType getInvocationType()
{
return Invocable.super.getInvocationType();
}

/**
* @param invocationType the {@link InvocationType} associated with this {@code HttpClientTransport}.
*/
void setInvocationType(InvocationType invocationType);
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,6 @@ public void onComplete(Result result)
{
Request request = result.getRequest();
Response response = result.getResponse();
if (result.getResponseFailure() == null)
redirector.redirect(request, response, null);
else
redirector.fail(request, response, result.getFailure());
redirector.redirect(request, response, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ interface AsyncContentListener extends ContentSourceListener
@Override
default void onContentSource(Response response, Content.Source contentSource)
{
Runnable demandCallback = Invocable.from(Invocable.InvocationType.NON_BLOCKING, () -> onContentSource(response, contentSource));
Runnable demandCallback = Invocable.from(Invocable.getInvocationType(contentSource), () -> onContentSource(response, contentSource));
Copy link
Contributor

Choose a reason for hiding this comment

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

There should probably be a comment explaining why we use the invocation type of the content source: how this trick exactly works is easy to forget.

Content.Chunk chunk = contentSource.read();
if (chunk == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.List;
import java.util.Map;

import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.client.transport.internal.HttpConnectionOverHTTP;
import org.eclipse.jetty.io.ClientConnectionFactory;
import org.eclipse.jetty.io.EndPoint;
Expand Down Expand Up @@ -49,8 +50,10 @@ public void setInitializeConnections(boolean initialize)
@Override
public org.eclipse.jetty.io.Connection newConnection(EndPoint endPoint, Map<String, Object> context)
{
HttpClient httpClient = (HttpClient)context.get(ClientConnectionFactory.CLIENT_CONTEXT_KEY);
HttpConnectionOverHTTP connection = new HttpConnectionOverHTTP(endPoint, context);
connection.setInitialize(isInitializeConnections());
connection.setInvocationType(httpClient.getHttpClientTransport().getInvocationType());
return customize(connection, context);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@
import org.eclipse.jetty.io.EndPoint;
import org.eclipse.jetty.util.annotation.ManagedAttribute;
import org.eclipse.jetty.util.annotation.ManagedObject;
import org.eclipse.jetty.util.thread.Invocable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@ManagedObject("The HTTP/1.1 client transport")
public class HttpClientTransportOverHTTP extends AbstractConnectorHttpClientTransport
public class HttpClientTransportOverHTTP extends AbstractConnectorHttpClientTransport implements Invocable
sbordet marked this conversation as resolved.
Show resolved Hide resolved
{
public static final Origin.Protocol HTTP11 = new Origin.Protocol(List.of("http/1.1"), false);
private static final Logger LOG = LoggerFactory.getLogger(HttpClientTransportOverHTTP.class);
Expand Down
Loading
Loading