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 #4650 - do not use ServiceLoader every time a WSSession is started #4664

Merged
merged 1 commit into from
Mar 17, 2020
Merged
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 @@ -64,16 +64,17 @@
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 RemoteEndpointFactory defaultRemoteEndpointFactory;
private final WebSocketContainerScope containerScope;
private final URI requestURI;
private final LogicalConnection connection;
private final EventDriver websocket;
private final Executor executor;
private final WebSocketPolicy policy;
private final AtomicBoolean onCloseCalled = new AtomicBoolean(false);
private final RemoteEndpointFactory remoteEndpointFactory;
private ClassLoader classLoader;
private ExtensionFactory extensionFactory;
private RemoteEndpointFactory remoteEndpointFactory;
private String protocolVersion;
private Map<String, String[]> parameterMap = new HashMap<>();
private RemoteEndpoint remote;
Expand All @@ -83,6 +84,15 @@ public class WebSocketSession extends ContainerLifeCycle implements Session, Rem
private UpgradeResponse upgradeResponse;
private CompletableFuture<Session> openFuture;

static
{
// Attempt to discover a RemoteEndpointFactory with the SerivceLoader.
Iterator<RemoteEndpointFactory> iter = ServiceLoader.load(RemoteEndpointFactory.class).iterator();
lachlan-roberts marked this conversation as resolved.
Show resolved Hide resolved
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)
{
Objects.requireNonNull(containerScope, "Container Scope cannot be null");
Expand All @@ -98,6 +108,10 @@ public WebSocketSession(WebSocketContainerScope containerScope, URI requestURI,
this.incomingHandler = websocket;
this.policy = websocket.getPolicy();

remoteEndpointFactory = (defaultRemoteEndpointFactory == null) ? this : defaultRemoteEndpointFactory;
if (LOG.isDebugEnabled())
LOG.debug("Using RemoteEndpointFactory: {}", remoteEndpointFactory);

this.connection.setSession(this);

addBean(this.connection);
Expand Down Expand Up @@ -168,16 +182,6 @@ protected void doStart() throws Exception
if (LOG.isDebugEnabled())
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();
}

Expand Down