Skip to content

Commit

Permalink
Use a TL instead of touching the userProperties map [IMMUTANT-561]
Browse files Browse the repository at this point in the history
Altering userProperties can cause ConcurrentModificationExceptions under
load.
  • Loading branch information
tobias committed May 13, 2015
1 parent 1123a66 commit 91cd86c
Showing 1 changed file with 12 additions and 9 deletions.
Expand Up @@ -24,13 +24,9 @@

public class DelegatingJavaxEndpoint extends Endpoint {

public static String endpointKey() {
return "session-endpoint-" + Thread.currentThread().getId();
}

@Override
public void onOpen(Session session, EndpointConfig endpointConfig) {
Endpoint endpoint = delegate(session);
Endpoint endpoint = delegate();
if (endpoint != null) {
endpoint.onOpen(session, endpointConfig);
} else {
Expand All @@ -40,22 +36,26 @@ public void onOpen(Session session, EndpointConfig endpointConfig) {

@Override
public void onClose(Session session, CloseReason closeReason) {
Endpoint endpoint = delegate(session);
Endpoint endpoint = delegate();
if (endpoint != null) {
endpoint.onClose(session, closeReason);
}
}

@Override
public void onError(Session session, Throwable err) {
Endpoint endpoint = delegate(session);
Endpoint endpoint = delegate();
if (endpoint != null) {
endpoint.onError(session, err);
} else {
close(session);
}
}

public static void setCurrentDelegate(Endpoint endpoint) {
delegateTL.set(endpoint);
}

private void close(Session session) {
try {
session.close(POLICY_CLOSE);
Expand All @@ -64,10 +64,12 @@ private void close(Session session) {
}
}

private synchronized Endpoint delegate(final Session session) {
private synchronized Endpoint delegate() {
if (this.delegate == null) {
this.delegate = (Endpoint)session.getUserProperties().remove(endpointKey());
this.delegate = delegateTL.get();
delegateTL.remove();
}

return this.delegate;
}

Expand All @@ -79,4 +81,5 @@ public int getCode() {
return 1003;
}
}, null);
private final static ThreadLocal<Endpoint> delegateTL = new ThreadLocal<>();
}

0 comments on commit 91cd86c

Please sign in to comment.