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 075cf8c02e
commit 9ed41db1d7
2 changed files with 19 additions and 87 deletions

View File

@ -17,16 +17,14 @@ package io.netty.channel.epoll;
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;
import io.netty.util.internal.ObjectUtil;
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 = new UncheckedBooleanSupplier() { private final UncheckedBooleanSupplier defaultMaybeMoreDataSupplier = new UncheckedBooleanSupplier() {
@Override @Override
public boolean get() { public boolean get() {
@ -36,8 +34,8 @@ class EpollRecvByteAllocatorHandle implements RecvByteBufAllocator.ExtendedHandl
private boolean isEdgeTriggered; private boolean isEdgeTriggered;
private boolean receivedRdHup; private boolean receivedRdHup;
EpollRecvByteAllocatorHandle(RecvByteBufAllocator.ExtendedHandle handle) { EpollRecvByteAllocatorHandle(ExtendedHandle handle) {
delegate = ObjectUtil.checkNotNull(handle, "handle"); super(handle);
} }
final void receivedRdHup() { final void receivedRdHup() {
@ -74,57 +72,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,18 +18,17 @@ 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 io.netty.util.internal.ObjectUtil;
import static java.lang.Math.max; import static java.lang.Math.max;
import static java.lang.Math.min; import static java.lang.Math.min;
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 = new UncheckedBooleanSupplier() { private final UncheckedBooleanSupplier defaultMaybeMoreDataSupplier = new UncheckedBooleanSupplier() {
@Override @Override
@ -41,24 +40,19 @@ final class KQueueRecvByteAllocatorHandle implements RecvByteBufAllocator.Extend
private boolean readEOF; private boolean readEOF;
private long numberBytesPending; private long numberBytesPending;
KQueueRecvByteAllocatorHandle(RecvByteBufAllocator.ExtendedHandle handle) { KQueueRecvByteAllocatorHandle(ExtendedHandle handle) {
delegate = ObjectUtil.checkNotNull(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
@ -66,44 +60,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() {