Have (Epoll|KQueue)RecvByteAllocatorHandle extend DelegatingHandle (#9060)

Motivation

These implementations delegate most of their methods to an existing Handle and previously extended RecvByteBufAllocator.DelegatingHandle. This was reverted in #6322 with the introduction of ExtendedHandle but it's not clear to me why it needed to be - the code looks a lot cleaner.

Modifications

Have (Epoll|KQueue)RecvByteAllocatorHandle extend DelegatingHandle again, while still implementing ExtendedHandle.

Result

Less code.
This commit is contained in:
Nick Hill 2019-04-16 00:14:09 -07:00 committed by Norman Maurer
parent 0523c781e8
commit 7f79182c82
2 changed files with 19 additions and 88 deletions

View File

@ -15,25 +15,22 @@
*/ */
package io.netty.channel.epoll; package io.netty.channel.epoll;
import static java.util.Objects.requireNonNull;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator; import io.netty.buffer.ByteBufAllocator;
import io.netty.channel.ChannelConfig; import io.netty.channel.RecvByteBufAllocator.DelegatingHandle;
import io.netty.channel.RecvByteBufAllocator; import io.netty.channel.RecvByteBufAllocator.ExtendedHandle;
import io.netty.channel.unix.PreferredDirectByteBufAllocator; import io.netty.channel.unix.PreferredDirectByteBufAllocator;
import io.netty.util.UncheckedBooleanSupplier; import io.netty.util.UncheckedBooleanSupplier;
class EpollRecvByteAllocatorHandle implements RecvByteBufAllocator.ExtendedHandle { class EpollRecvByteAllocatorHandle extends DelegatingHandle implements ExtendedHandle {
private final PreferredDirectByteBufAllocator preferredDirectByteBufAllocator = private final PreferredDirectByteBufAllocator preferredDirectByteBufAllocator =
new PreferredDirectByteBufAllocator(); new PreferredDirectByteBufAllocator();
private final RecvByteBufAllocator.ExtendedHandle delegate;
private final UncheckedBooleanSupplier defaultMaybeMoreDataSupplier = this::maybeMoreDataToRead; private final UncheckedBooleanSupplier defaultMaybeMoreDataSupplier = this::maybeMoreDataToRead;
private boolean isEdgeTriggered; private boolean isEdgeTriggered;
private boolean receivedRdHup; private boolean receivedRdHup;
EpollRecvByteAllocatorHandle(RecvByteBufAllocator.ExtendedHandle handle) { EpollRecvByteAllocatorHandle(ExtendedHandle handle) {
delegate = requireNonNull(handle, "handle"); super(handle);
} }
final void receivedRdHup() { final void receivedRdHup() {
@ -70,57 +67,17 @@ class EpollRecvByteAllocatorHandle implements RecvByteBufAllocator.ExtendedHandl
public final ByteBuf allocate(ByteBufAllocator alloc) { public final ByteBuf allocate(ByteBufAllocator alloc) {
// We need to ensure we always allocate a direct ByteBuf as we can only use a direct buffer to read via JNI. // We need to ensure we always allocate a direct ByteBuf as we can only use a direct buffer to read via JNI.
preferredDirectByteBufAllocator.updateAllocator(alloc); preferredDirectByteBufAllocator.updateAllocator(alloc);
return delegate.allocate(preferredDirectByteBufAllocator); return delegate().allocate(preferredDirectByteBufAllocator);
}
@Override
public final int guess() {
return delegate.guess();
}
@Override
public final void reset(ChannelConfig config) {
delegate.reset(config);
}
@Override
public final void incMessagesRead(int numMessages) {
delegate.incMessagesRead(numMessages);
}
@Override
public final void lastBytesRead(int bytes) {
delegate.lastBytesRead(bytes);
}
@Override
public final int lastBytesRead() {
return delegate.lastBytesRead();
}
@Override
public final int attemptedBytesRead() {
return delegate.attemptedBytesRead();
}
@Override
public final void attemptedBytesRead(int bytes) {
delegate.attemptedBytesRead(bytes);
}
@Override
public final void readComplete() {
delegate.readComplete();
} }
@Override @Override
public final boolean continueReading(UncheckedBooleanSupplier maybeMoreDataSupplier) { public final boolean continueReading(UncheckedBooleanSupplier maybeMoreDataSupplier) {
return delegate.continueReading(maybeMoreDataSupplier); return ((ExtendedHandle) delegate()).continueReading(maybeMoreDataSupplier);
} }
@Override @Override
public final boolean continueReading() { public final boolean continueReading() {
// We must override the supplier which determines if there maybe more data to read. // We must override the supplier which determines if there maybe more data to read.
return delegate.continueReading(defaultMaybeMoreDataSupplier); return continueReading(defaultMaybeMoreDataSupplier);
} }
} }

View File

@ -18,42 +18,36 @@ package io.netty.channel.kqueue;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator; import io.netty.buffer.ByteBufAllocator;
import io.netty.channel.ChannelConfig; import io.netty.channel.ChannelConfig;
import io.netty.channel.RecvByteBufAllocator; import io.netty.channel.RecvByteBufAllocator.DelegatingHandle;
import io.netty.channel.RecvByteBufAllocator.ExtendedHandle;
import io.netty.channel.unix.PreferredDirectByteBufAllocator; import io.netty.channel.unix.PreferredDirectByteBufAllocator;
import io.netty.util.UncheckedBooleanSupplier; import io.netty.util.UncheckedBooleanSupplier;
import static java.lang.Math.max; import static java.lang.Math.max;
import static java.lang.Math.min; import static java.lang.Math.min;
import static java.util.Objects.requireNonNull;
final class KQueueRecvByteAllocatorHandle implements RecvByteBufAllocator.ExtendedHandle { final class KQueueRecvByteAllocatorHandle extends DelegatingHandle implements ExtendedHandle {
private final PreferredDirectByteBufAllocator preferredDirectByteBufAllocator = private final PreferredDirectByteBufAllocator preferredDirectByteBufAllocator =
new PreferredDirectByteBufAllocator(); new PreferredDirectByteBufAllocator();
private final RecvByteBufAllocator.ExtendedHandle delegate;
private final UncheckedBooleanSupplier defaultMaybeMoreDataSupplier = this::maybeMoreDataToRead; private final UncheckedBooleanSupplier defaultMaybeMoreDataSupplier = this::maybeMoreDataToRead;
private boolean overrideGuess; private boolean overrideGuess;
private boolean readEOF; private boolean readEOF;
private long numberBytesPending; private long numberBytesPending;
KQueueRecvByteAllocatorHandle(RecvByteBufAllocator.ExtendedHandle handle) { KQueueRecvByteAllocatorHandle(ExtendedHandle handle) {
delegate = requireNonNull(handle, "handle"); super(handle);
} }
@Override @Override
public int guess() { public int guess() {
return overrideGuess ? guess0() : delegate.guess(); return overrideGuess ? guess0() : delegate().guess();
} }
@Override @Override
public void reset(ChannelConfig config) { public void reset(ChannelConfig config) {
overrideGuess = ((KQueueChannelConfig) config).getRcvAllocTransportProvidesGuess(); overrideGuess = ((KQueueChannelConfig) config).getRcvAllocTransportProvidesGuess();
delegate.reset(config); delegate().reset(config);
}
@Override
public void incMessagesRead(int numMessages) {
delegate.incMessagesRead(numMessages);
} }
@Override @Override
@ -61,44 +55,24 @@ final class KQueueRecvByteAllocatorHandle implements RecvByteBufAllocator.Extend
// We need to ensure we always allocate a direct ByteBuf as we can only use a direct buffer to read via JNI. // We need to ensure we always allocate a direct ByteBuf as we can only use a direct buffer to read via JNI.
preferredDirectByteBufAllocator.updateAllocator(alloc); preferredDirectByteBufAllocator.updateAllocator(alloc);
return overrideGuess ? preferredDirectByteBufAllocator.ioBuffer(guess0()) : return overrideGuess ? preferredDirectByteBufAllocator.ioBuffer(guess0()) :
delegate.allocate(preferredDirectByteBufAllocator); delegate().allocate(preferredDirectByteBufAllocator);
} }
@Override @Override
public void lastBytesRead(int bytes) { public void lastBytesRead(int bytes) {
numberBytesPending = bytes < 0 ? 0 : max(0, numberBytesPending - bytes); numberBytesPending = bytes < 0 ? 0 : max(0, numberBytesPending - bytes);
delegate.lastBytesRead(bytes); delegate().lastBytesRead(bytes);
}
@Override
public int lastBytesRead() {
return delegate.lastBytesRead();
}
@Override
public void attemptedBytesRead(int bytes) {
delegate.attemptedBytesRead(bytes);
}
@Override
public int attemptedBytesRead() {
return delegate.attemptedBytesRead();
}
@Override
public void readComplete() {
delegate.readComplete();
} }
@Override @Override
public boolean continueReading(UncheckedBooleanSupplier maybeMoreDataSupplier) { public boolean continueReading(UncheckedBooleanSupplier maybeMoreDataSupplier) {
return delegate.continueReading(maybeMoreDataSupplier); return ((ExtendedHandle) delegate()).continueReading(maybeMoreDataSupplier);
} }
@Override @Override
public boolean continueReading() { public boolean continueReading() {
// We must override the supplier which determines if there maybe more data to read. // We must override the supplier which determines if there maybe more data to read.
return delegate.continueReading(defaultMaybeMoreDataSupplier); return continueReading(defaultMaybeMoreDataSupplier);
} }
void readEOF() { void readEOF() {