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

[4.1] Futures removeListener: JavaDoc and implementation are inconsistent - duplicate listeners #5351

Closed
mickare opened this issue Jun 4, 2016 · 2 comments
Assignees

Comments

@mickare
Copy link

mickare commented Jun 4, 2016

Netty version: 4.1.0.CR7

Context:

I encountered an inconsistency between the JavaDoc description and implementation of the Future.
I want to add multiple times (e.g. x 1000) the same close listener to a channel but I don't want this listener to be called multiple times.
Also I don't want to remove the listener prior to adding it. I could do this but I don't like the wasteful Java array copy calls.

JavaDoc vs Implementation

The Netty 4.1 JavaDoc on Future says:

Future removeListener(GenericFutureListener<? extends Future<? super V>> listener)

Removes the specified listener from this future. The specified listener is no longer notified when this future is done. If the specified listener is not associated with this future, this method does nothing and returns silently.

But if I add multiple times the same listener to the future, only the first entry is removed. (see implementation)

With the current implementation in mind the JavaDoc should state that only the first occurrence of the listener is removed. Like:

Removes the first occurrence of the specified listener from this future.

More on futures (could be an independent proposal):

Even more the listener is called multiple times. I think futures are part of the observer pattern. That means an observer should be notified only once an event occurs.

That means there should be no duplicate listeners in the future.
E.g.: the listeners array sould be changed to a set, or duplicates avoided.

Or is there a puprose, that I can not see at the moment? Like performance? But if it is performance, then would not multiple calls on the same listener effect the performance? For example:

private final ChannelFutureListener closeListener = f -> cache.invalidate(f.channel);

public void oftenCalled(Channel ch) {
  ch.closeFuture().addListener(closeListener);
  ...
}

With the current implementation closeListener would be called as often as the method oftenCalled.
I had just a discussion that there could be a use-case of a self-incrementing counter, that uses the future to add a future increment. This counter would need multiple entries.
Therefore I propose to add a new method "addListenerIfAbsent", that could resolve the proposal. I'll create a new issue for that.

Best regards,
Michael

@mickare
Copy link
Author

mickare commented Jun 4, 2016

Splitted the proposal to new issue: #5352

@Scottmitch
Copy link
Member

@mickare - Thanks for bringing this to our attention. The javadocs should be updated. Let me take care.

@Scottmitch Scottmitch self-assigned this Jun 6, 2016
Scottmitch added a commit to Scottmitch/netty that referenced this issue Jun 8, 2016
Motivation:
The javaDocs for Future.removeListener do not clarify that only the first occurrence of the listener is guaranteed to be removed.

Modifications:
- Clarify the javaDocs for Future.removeListener[s] so it is known that the only the first occurrence of the listener will be removed.

Result:
Fixes netty#5351
Scottmitch added a commit that referenced this issue Jun 8, 2016
Motivation:
The javaDocs for Future.removeListener do not clarify that only the first occurrence of the listener is guaranteed to be removed.

Modifications:
- Clarify the javaDocs for Future.removeListener[s] so it is known that the only the first occurrence of the listener will be removed.

Result:
Fixes #5351
liuzhengyang pushed a commit to liuzhengyang/netty that referenced this issue Sep 10, 2017
Motivation:
The javaDocs for Future.removeListener do not clarify that only the first occurrence of the listener is guaranteed to be removed.

Modifications:
- Clarify the javaDocs for Future.removeListener[s] so it is known that the only the first occurrence of the listener will be removed.

Result:
Fixes netty#5351
pulllock pushed a commit to pulllock/netty that referenced this issue Oct 19, 2023
Motivation:
The javaDocs for Future.removeListener do not clarify that only the first occurrence of the listener is guaranteed to be removed.

Modifications:
- Clarify the javaDocs for Future.removeListener[s] so it is known that the only the first occurrence of the listener will be removed.

Result:
Fixes netty#5351
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants