Skip to content

Commit

Permalink
Cleanup: replaced deprecated ctx.attr() and ctx.hasAttr() methods usa…
Browse files Browse the repository at this point in the history
…ge with ch.attr() and ch.hasAttr().

Motivation:

Will allow easy removal of deprecated methods in future.

Modification:

Replaced ctx.attr(), ctx.hasAttr() with ctx.channel().attr(), ctx.channel().hasAttr().

Result:

No deprecated ctx.attr(), ctx.hasAttr() methods usage.
  • Loading branch information
doom369 authored and normanmaurer committed Jan 18, 2018
1 parent 6ff48dc commit e6c9ac9
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public ContextBoundUnmarshallerProvider(MarshallerFactory factory, MarshallingCo

@Override
public Unmarshaller getUnmarshaller(ChannelHandlerContext ctx) throws Exception {
Attribute<Unmarshaller> attr = ctx.attr(UNMARSHALLER);
Attribute<Unmarshaller> attr = ctx.channel().attr(UNMARSHALLER);
Unmarshaller unmarshaller = attr.get();
if (unmarshaller == null) {
unmarshaller = super.getUnmarshaller(ctx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufHolder;
import io.netty.channel.Channel;
import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelConfig;
import io.netty.channel.ChannelHandlerContext;
Expand Down Expand Up @@ -426,15 +427,16 @@ static final class ReopenReadTimerTask implements Runnable {

@Override
public void run() {
ChannelConfig config = ctx.channel().config();
Channel channel = ctx.channel();
ChannelConfig config = channel.config();
if (!config.isAutoRead() && isHandlerActive(ctx)) {
// If AutoRead is False and Active is True, user make a direct setAutoRead(false)
// Then Just reset the status
if (logger.isDebugEnabled()) {
logger.debug("Not unsuspend: " + config.isAutoRead() + ':' +
isHandlerActive(ctx));
}
ctx.attr(READ_SUSPENDED).set(false);
channel.attr(READ_SUSPENDED).set(false);
} else {
// Anything else allows the handler to reset the AutoRead
if (logger.isDebugEnabled()) {
Expand All @@ -446,9 +448,9 @@ public void run() {
+ isHandlerActive(ctx));
}
}
ctx.attr(READ_SUSPENDED).set(false);
channel.attr(READ_SUSPENDED).set(false);
config.setAutoRead(true);
ctx.channel().read();
channel.read();
}
if (logger.isDebugEnabled()) {
logger.debug("Unsuspend final status => " + config.isAutoRead() + ':'
Expand All @@ -461,8 +463,9 @@ public void run() {
* Release the Read suspension
*/
void releaseReadSuspended(ChannelHandlerContext ctx) {
ctx.attr(READ_SUSPENDED).set(false);
ctx.channel().config().setAutoRead(true);
Channel channel = ctx.channel();
channel.attr(READ_SUSPENDED).set(false);
channel.config().setAutoRead(true);
}

@Override
Expand All @@ -476,17 +479,18 @@ public void channelRead(final ChannelHandlerContext ctx, final Object msg) throw
if (wait >= MINIMAL_WAIT) { // At least 10ms seems a minimal
// time in order to try to limit the traffic
// Only AutoRead AND HandlerActive True means Context Active
ChannelConfig config = ctx.channel().config();
Channel channel = ctx.channel();
ChannelConfig config = channel.config();
if (logger.isDebugEnabled()) {
logger.debug("Read suspend: " + wait + ':' + config.isAutoRead() + ':'
+ isHandlerActive(ctx));
}
if (config.isAutoRead() && isHandlerActive(ctx)) {
config.setAutoRead(false);
ctx.attr(READ_SUSPENDED).set(true);
channel.attr(READ_SUSPENDED).set(true);
// Create a Runnable to reactive the read if needed. If one was create before it will just be
// reused to limit object creation
Attribute<Runnable> attr = ctx.attr(REOPEN_TASK);
Attribute<Runnable> attr = channel.attr(REOPEN_TASK);
Runnable reopenTask = attr.get();
if (reopenTask == null) {
reopenTask = new ReopenReadTimerTask(ctx);
Expand Down Expand Up @@ -524,7 +528,7 @@ void informReadOperation(final ChannelHandlerContext ctx, final long now) {
}

protected static boolean isHandlerActive(ChannelHandlerContext ctx) {
Boolean suspended = ctx.attr(READ_SUSPENDED).get();
Boolean suspended = ctx.channel().attr(READ_SUSPENDED).get();
return suspended == null || Boolean.FALSE.equals(suspended);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -547,17 +547,18 @@ public void channelRead(final ChannelHandlerContext ctx, final Object msg) throw
if (wait >= MINIMAL_WAIT) { // At least 10ms seems a minimal
// time in order to try to limit the traffic
// Only AutoRead AND HandlerActive True means Context Active
ChannelConfig config = ctx.channel().config();
Channel channel = ctx.channel();
ChannelConfig config = channel.config();
if (logger.isDebugEnabled()) {
logger.debug("Read Suspend: " + wait + ':' + config.isAutoRead() + ':'
+ isHandlerActive(ctx));
}
if (config.isAutoRead() && isHandlerActive(ctx)) {
config.setAutoRead(false);
ctx.attr(READ_SUSPENDED).set(true);
channel.attr(READ_SUSPENDED).set(true);
// Create a Runnable to reactive the read if needed. If one was create before it will just be
// reused to limit object creation
Attribute<Runnable> attr = ctx.attr(REOPEN_TASK);
Attribute<Runnable> attr = channel.attr(REOPEN_TASK);
Runnable reopenTask = attr.get();
if (reopenTask == null) {
reopenTask = new ReopenReadTimerTask(ctx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -581,12 +581,12 @@ public ChannelPromise voidPromise() {

@Override
public <T> Attribute<T> attr(AttributeKey<T> key) {
return ctx.attr(key);
return ctx.channel().attr(key);
}

@Override
public <T> boolean hasAttr(AttributeKey<T> key) {
return ctx.hasAttr(key);
return ctx.channel().hasAttr(key);
}

final void remove() {
Expand Down

0 comments on commit e6c9ac9

Please sign in to comment.