Skip to content

Copy invocation handler list in Java client #43402

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

Merged
merged 2 commits into from
Aug 19, 2022
Merged
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 @@ -31,7 +31,12 @@ public InvocationHandler put(String target, Object action, Type... types) {
}
}
}
methodHandlers = new ArrayList<>(methodHandlers);
methodHandlers.add(handler);

// replace List in handlers map
handlers.remove(target);
handlers.put(target, methodHandlers);
return handler;
} finally {
lock.unlock();
Expand All @@ -41,7 +46,7 @@ public InvocationHandler put(String target, Object action, Type... types) {
public List<InvocationHandler> get(String key) {
try {
lock.lock();
return handlers.get(key);
return this.handlers.get(key);
} finally {
lock.unlock();
}
Expand All @@ -55,4 +60,21 @@ public void remove(String key) {
lock.unlock();
}
}

public void remove(String key, InvocationHandler handler) {
try {
lock.lock();
List<InvocationHandler> handlers = this.handlers.get(key);
if (handlers != null) {
handlers = new ArrayList<>(handlers);
handlers.remove(handler);
Copy link
Member

Choose a reason for hiding this comment

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

Instead of mutating the existing list, wouldn't it likely be more efficient to make this copy-on-write and replace the reference so we don't have to copy it every time in get()?

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmm, that's a good point. Looks like that's what we do in the .NET client so at least we were smart there 😄


// replace List in handlers map
this.handlers.remove(key);
this.handlers.put(key, handlers);
}
} finally {
lock.unlock();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ public class Subscription {
* Removes the client method handler represented by this subscription.
*/
public void unsubscribe() {
List<InvocationHandler> handler = this.handlers.get(target);
if (handler != null) {
handler.remove(this.handler);
}
this.handlers.remove(this.target, this.handler);
}
}