Merge pull request #4664 from eclipse/jetty-9.4.x-4650-RemoteEndpointFactory

Issue #4650 - do not use ServiceLoader every time a WSSession is started
This commit is contained in:
Lachlan 2020-03-18 09:39:06 +11:00 committed by GitHub
commit a9ef2d0d74
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 15 additions and 11 deletions

View File

@ -64,6 +64,7 @@ import org.eclipse.jetty.websocket.common.scopes.WebSocketSessionScope;
public class WebSocketSession extends ContainerLifeCycle implements Session, RemoteEndpointFactory, WebSocketSessionScope, IncomingFrames, OutgoingFrames, Connection.Listener public class WebSocketSession extends ContainerLifeCycle implements Session, RemoteEndpointFactory, WebSocketSessionScope, IncomingFrames, OutgoingFrames, Connection.Listener
{ {
private static final Logger LOG = Log.getLogger(WebSocketSession.class); private static final Logger LOG = Log.getLogger(WebSocketSession.class);
private static final RemoteEndpointFactory defaultRemoteEndpointFactory;
private final WebSocketContainerScope containerScope; private final WebSocketContainerScope containerScope;
private final URI requestURI; private final URI requestURI;
private final LogicalConnection connection; private final LogicalConnection connection;
@ -71,9 +72,9 @@ public class WebSocketSession extends ContainerLifeCycle implements Session, Rem
private final Executor executor; private final Executor executor;
private final WebSocketPolicy policy; private final WebSocketPolicy policy;
private final AtomicBoolean onCloseCalled = new AtomicBoolean(false); private final AtomicBoolean onCloseCalled = new AtomicBoolean(false);
private final RemoteEndpointFactory remoteEndpointFactory;
private ClassLoader classLoader; private ClassLoader classLoader;
private ExtensionFactory extensionFactory; private ExtensionFactory extensionFactory;
private RemoteEndpointFactory remoteEndpointFactory;
private String protocolVersion; private String protocolVersion;
private Map<String, String[]> parameterMap = new HashMap<>(); private Map<String, String[]> parameterMap = new HashMap<>();
private RemoteEndpoint remote; private RemoteEndpoint remote;
@ -83,6 +84,15 @@ public class WebSocketSession extends ContainerLifeCycle implements Session, Rem
private UpgradeResponse upgradeResponse; private UpgradeResponse upgradeResponse;
private CompletableFuture<Session> openFuture; private CompletableFuture<Session> openFuture;
static
{
// Attempt to discover a RemoteEndpointFactory with the SerivceLoader.
Iterator<RemoteEndpointFactory> iter = ServiceLoader.load(RemoteEndpointFactory.class).iterator();
defaultRemoteEndpointFactory = iter.hasNext() ? iter.next() : null;
if (LOG.isDebugEnabled())
LOG.debug("Discovered default RemoteEndpointFactory: {}", defaultRemoteEndpointFactory);
}
public WebSocketSession(WebSocketContainerScope containerScope, URI requestURI, EventDriver websocket, LogicalConnection connection) public WebSocketSession(WebSocketContainerScope containerScope, URI requestURI, EventDriver websocket, LogicalConnection connection)
{ {
Objects.requireNonNull(containerScope, "Container Scope cannot be null"); Objects.requireNonNull(containerScope, "Container Scope cannot be null");
@ -98,6 +108,10 @@ public class WebSocketSession extends ContainerLifeCycle implements Session, Rem
this.incomingHandler = websocket; this.incomingHandler = websocket;
this.policy = websocket.getPolicy(); this.policy = websocket.getPolicy();
remoteEndpointFactory = (defaultRemoteEndpointFactory == null) ? this : defaultRemoteEndpointFactory;
if (LOG.isDebugEnabled())
LOG.debug("Using RemoteEndpointFactory: {}", remoteEndpointFactory);
this.connection.setSession(this); this.connection.setSession(this);
addBean(this.connection); addBean(this.connection);
@ -168,16 +182,6 @@ public class WebSocketSession extends ContainerLifeCycle implements Session, Rem
if (LOG.isDebugEnabled()) if (LOG.isDebugEnabled())
LOG.debug("starting - {}", this); LOG.debug("starting - {}", this);
Iterator<RemoteEndpointFactory> iter = ServiceLoader.load(RemoteEndpointFactory.class).iterator();
if (iter.hasNext())
remoteEndpointFactory = iter.next();
if (remoteEndpointFactory == null)
remoteEndpointFactory = this;
if (LOG.isDebugEnabled())
LOG.debug("Using RemoteEndpointFactory: {}", remoteEndpointFactory);
super.doStart(); super.doStart();
} }