Skip to content

Instantly share code, notes, and snippets.

@yangl
Last active June 12, 2019 06:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yangl/a9b54fe41fc66a323661e801de6b059d to your computer and use it in GitHub Desktop.
Save yangl/a9b54fe41fc66a323661e801de6b059d to your computer and use it in GitHub Desktop.
Netty Epoll的LT、ET模式使用场景,详见注释。
/**
* Set the {@link EpollMode} used. Default is
* {@link EpollMode#EDGE_TRIGGERED}. If you want to use {@link #isAutoRead()} {@code false} or
* {@link #getMaxMessagesPerRead()} and have an accurate behaviour you should use
* {@link EpollMode#LEVEL_TRIGGERED}.
*
* <strong>Be aware this config setting can only be adjusted before the channel was registered.</strong>
*/
public EpollChannelConfig setEpollMode(EpollMode mode) {
if (mode == null) {
throw new NullPointerException("mode");
}
try {
switch (mode) {
case EDGE_TRIGGERED:
checkChannelNotRegistered();
((AbstractEpollChannel) channel).setFlag(Native.EPOLLET);
break;
case LEVEL_TRIGGERED:
checkChannelNotRegistered();
((AbstractEpollChannel) channel).clearFlag(Native.EPOLLET);
break;
default:
throw new Error();
}
} catch (IOException e) {
throw new ChannelException(e);
}
return this;
}
// pulsar ServerCnx
public void startSendOperation() {
if (++pendingSendRequest == MaxPendingSendRequests) {
// When the quota of pending send requests is reached, stop reading from socket to cause backpressure on
// client connection, possibly shared between multiple producers
ctx.channel().config().setAutoRead(false);
}
}
public void completedSendOperation(boolean isNonPersistentTopic) {
if (--pendingSendRequest == ResumeReadsThreshold) {
// Resume reading from socket
ctx.channel().config().setAutoRead(true);
}
if (isNonPersistentTopic) {
nonPersistentPendingMessages--;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment