Skip to content

Commit

Permalink
Keep used NioIoOps and SelectionKey in sync (#14029)
Browse files Browse the repository at this point in the history
Motivation:

As the user is able to retieve the underlying SelectionKey via the
NioRegistration we need to ensure we always keep this and the current
used NioIoOps in sync.

Modifications:

Remove the local stored NioOps and just always reconstruct using the
underlying SelectionKey. This is not a concern in terms of performance
and object creation as we pre-construct them.

Result:

Keep current used NioIoOps and SelectionKey always in sync
  • Loading branch information
normanmaurer committed May 3, 2024
1 parent 7ed972d commit d3e830c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 15 deletions.
Expand Up @@ -58,10 +58,6 @@ public abstract class AbstractNioChannel extends AbstractChannel {
private final SelectableChannel ch;
protected final int readInterestOp;
protected final NioIoOps readOps;

// We always start with NONE as initial ops.
private NioIoOps ops = NioIoOps.NONE;

volatile NioIoRegistration registration;
boolean readPending;
private final Runnable clearReadPendingRunnable = new Runnable() {
Expand Down Expand Up @@ -110,21 +106,21 @@ protected AbstractNioChannel(Channel parent, SelectableChannel ch, NioIoOps read
}

protected void addAndSubmit(NioIoOps addOps) {
if (!ops.contains(addOps)) {
ops = ops.with(addOps);
int interestOps = selectionKey().interestOps();
if (!addOps.isIncludedIn(interestOps)) {
try {
registration().submit(ops);
registration().submit(NioIoOps.valueOf(interestOps).with(addOps));
} catch (Exception e) {
throw new ChannelException(e);
}
}
}

protected void removeAndSubmit(NioIoOps removeOps) {
if (ops.contains(removeOps)) {
ops = ops.without(removeOps);
int interestOps = selectionKey().interestOps();
if (removeOps.isIncludedIn(interestOps)) {
try {
registration().submit(ops);
registration().submit(NioIoOps.valueOf(interestOps).without(removeOps));
} catch (Exception e) {
throw new ChannelException(e);
}
Expand Down Expand Up @@ -417,7 +413,7 @@ public final void forceFlush() {

private boolean isFlushPending() {
NioIoRegistration registration = registration();
return registration.isValid() && ops.contains(NioIoOps.WRITE);
return registration.isValid() && NioIoOps.WRITE.isIncludedIn(registration.selectionKey().interestOps());
}

@Override
Expand Down Expand Up @@ -478,8 +474,6 @@ protected void doDeregister() throws Exception {
NioIoRegistration registration = registration();
if (registration != null) {
this.registration = null;
// Reset to NONE so we have the correct value when we register again.
ops = NioIoOps.NONE;
registration.cancel();
}
}
Expand Down
24 changes: 22 additions & 2 deletions transport/src/main/java/io/netty/channel/nio/NioIoOps.java
Expand Up @@ -93,7 +93,7 @@ private NioIoOps(int value) {
* @return {@code true} if a combination of the given.
*/
public boolean contains(NioIoOps ops) {
return (value & ops.value) != 0;
return isIncludedIn(ops.value);
}

/**
Expand Down Expand Up @@ -123,7 +123,7 @@ public NioIoOps without(NioIoOps ops) {
}

/**
* Returns the underlying value of the {@link NioIoOps}.
* Returns the underlying ops value of the {@link NioIoOps}.
*
* @return value.
*/
Expand Down Expand Up @@ -158,6 +158,26 @@ public static NioIoOps valueOf(int value) {
return eventOf(value).ops();
}

/**
* Returns {@code true} if this {@link NioIoOps} is <strong>included </strong> in the given {@code ops}.
*
* @param ops the ops to check.
* @return {@code true} if <strong>included</strong>, {@code false} otherwise.
*/
public boolean isIncludedIn(int ops) {
return (ops & value) != 0;
}

/**
* Returns {@code true} if this {@link NioIoOps} is <strong>not included</strong> in the given {@code ops}.
*
* @param ops the ops to check.
* @return {@code true} if <strong>not included</strong>, {@code false} otherwise.
*/
public boolean isNotIncludedIn(int ops) {
return (ops & value) == 0;
}

static NioIoEvent eventOf(int value) {
if (value > 0 && value < EVENTS.length) {
NioIoEvent event = EVENTS[value];
Expand Down

0 comments on commit d3e830c

Please sign in to comment.