Java Code Examples for io.netty.channel.Channel#config()

The following examples show how to use io.netty.channel.Channel#config() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DefaultRegistryServer.java    From Jupiter with Apache License 2.0 6 votes vote down vote up
@Override
public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {
    Channel ch = ctx.channel();
    ChannelConfig config = ch.config();

    // 高水位线: ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK
    // 低水位线: ChannelOption.WRITE_BUFFER_LOW_WATER_MARK
    if (!ch.isWritable()) {
        // 当前channel的缓冲区(OutboundBuffer)大小超过了WRITE_BUFFER_HIGH_WATER_MARK
        if (logger.isWarnEnabled()) {
            logger.warn("{} is not writable, high water mask: {}, the number of flushed entries that are not written yet: {}.",
                    ch, config.getWriteBufferHighWaterMark(), ch.unsafe().outboundBuffer().size());
        }

        config.setAutoRead(false);
    } else {
        // 曾经高于高水位线的OutboundBuffer现在已经低于WRITE_BUFFER_LOW_WATER_MARK了
        if (logger.isWarnEnabled()) {
            logger.warn("{} is writable(rehabilitate), low water mask: {}, the number of flushed entries that are not written yet: {}.",
                    ch, config.getWriteBufferLowWaterMark(), ch.unsafe().outboundBuffer().size());
        }

        config.setAutoRead(true);
    }
}
 
Example 2
Source File: AcceptorHandler.java    From Jupiter with Apache License 2.0 6 votes vote down vote up
@Override
public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {
    Channel ch = ctx.channel();
    ChannelConfig config = ch.config();

    // 高水位线: ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK
    // 低水位线: ChannelOption.WRITE_BUFFER_LOW_WATER_MARK
    if (!ch.isWritable()) {
        // 当前channel的缓冲区(OutboundBuffer)大小超过了WRITE_BUFFER_HIGH_WATER_MARK
        if (logger.isWarnEnabled()) {
            logger.warn("{} is not writable, high water mask: {}, the number of flushed entries that are not written yet: {}.",
                    ch, config.getWriteBufferHighWaterMark(), ch.unsafe().outboundBuffer().size());
        }

        config.setAutoRead(false);
    } else {
        // 曾经高于高水位线的OutboundBuffer现在已经低于WRITE_BUFFER_LOW_WATER_MARK了
        if (logger.isWarnEnabled()) {
            logger.warn("{} is writable(rehabilitate), low water mask: {}, the number of flushed entries that are not written yet: {}.",
                    ch, config.getWriteBufferLowWaterMark(), ch.unsafe().outboundBuffer().size());
        }

        config.setAutoRead(true);
    }
}
 
Example 3
Source File: ConnectorHandler.java    From Jupiter with Apache License 2.0 6 votes vote down vote up
@Override
public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {
    Channel ch = ctx.channel();
    ChannelConfig config = ch.config();

    // 高水位线: ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK
    // 低水位线: ChannelOption.WRITE_BUFFER_LOW_WATER_MARK
    if (!ch.isWritable()) {
        // 当前channel的缓冲区(OutboundBuffer)大小超过了WRITE_BUFFER_HIGH_WATER_MARK
        if (logger.isWarnEnabled()) {
            logger.warn("{} is not writable, high water mask: {}, the number of flushed entries that are not written yet: {}.",
                    ch, config.getWriteBufferHighWaterMark(), ch.unsafe().outboundBuffer().size());
        }

        config.setAutoRead(false);
    } else {
        // 曾经高于高水位线的OutboundBuffer现在已经低于WRITE_BUFFER_LOW_WATER_MARK了
        if (logger.isWarnEnabled()) {
            logger.warn("{} is writable(rehabilitate), low water mask: {}, the number of flushed entries that are not written yet: {}.",
                    ch, config.getWriteBufferLowWaterMark(), ch.unsafe().outboundBuffer().size());
        }

        config.setAutoRead(true);
    }
}
 
Example 4
Source File: Utils.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
static InternalChannelz.SocketOptions getSocketOptions(Channel channel) {
  ChannelConfig config = channel.config();
  InternalChannelz.SocketOptions.Builder b = new InternalChannelz.SocketOptions.Builder();

  // The API allows returning null but not sure if it can happen in practice.
  // Let's be paranoid and do null checking just in case.
  Integer lingerSeconds = config.getOption(SO_LINGER);
  if (lingerSeconds != null) {
    b.setSocketOptionLingerSeconds(lingerSeconds);
  }

  Integer timeoutMillis = config.getOption(SO_TIMEOUT);
  if (timeoutMillis != null) {
    // in java, SO_TIMEOUT only applies to receiving
    b.setSocketOptionTimeoutMillis(timeoutMillis);
  }

  for (Entry<ChannelOption<?>, Object> opt : config.getOptions().entrySet()) {
    ChannelOption<?> key = opt.getKey();
    // Constants are pooled, so there should only be one instance of each constant
    if (key.equals(SO_LINGER) || key.equals(SO_TIMEOUT)) {
      continue;
    }
    Object value = opt.getValue();
    // zpencer: Can a netty option be null?
    b.addOption(key.name(), String.valueOf(value));
  }

  NativeSocketOptions nativeOptions
      = NettySocketSupport.getNativeSocketOptions(channel);
  if (nativeOptions != null) {
    b.setTcpInfo(nativeOptions.tcpInfo); // may be null
    for (Entry<String, String> entry : nativeOptions.otherInfo.entrySet()) {
      b.addOption(entry.getKey(), entry.getValue());
    }
  }
  return b.build();
}
 
Example 5
Source File: AbstractTrafficShapingHandler.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Override
public void run() {
    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));
        }
        channel.attr(READ_SUSPENDED).set(false);
    } else {
        // Anything else allows the handler to reset the AutoRead
        if (logger.isDebugEnabled()) {
            if (config.isAutoRead() && !isHandlerActive(ctx)) {
                logger.debug("Unsuspend: " + config.isAutoRead() + ':' +
                        isHandlerActive(ctx));
            } else {
                logger.debug("Normal unsuspend: " + config.isAutoRead() + ':'
                        + isHandlerActive(ctx));
            }
        }
        channel.attr(READ_SUSPENDED).set(false);
        config.setAutoRead(true);
        channel.read();
    }
    if (logger.isDebugEnabled()) {
        logger.debug("Unsuspend final status => " + config.isAutoRead() + ':'
                + isHandlerActive(ctx));
    }
}
 
Example 6
Source File: Utils.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
static InternalChannelz.SocketOptions getSocketOptions(Channel channel) {
  ChannelConfig config = channel.config();
  InternalChannelz.SocketOptions.Builder b = new InternalChannelz.SocketOptions.Builder();

  // The API allows returning null but not sure if it can happen in practice.
  // Let's be paranoid and do null checking just in case.
  Integer lingerSeconds = config.getOption(SO_LINGER);
  if (lingerSeconds != null) {
    b.setSocketOptionLingerSeconds(lingerSeconds);
  }

  Integer timeoutMillis = config.getOption(SO_TIMEOUT);
  if (timeoutMillis != null) {
    // in java, SO_TIMEOUT only applies to receiving
    b.setSocketOptionTimeoutMillis(timeoutMillis);
  }

  for (Entry<ChannelOption<?>, Object> opt : config.getOptions().entrySet()) {
    ChannelOption<?> key = opt.getKey();
    // Constants are pooled, so there should only be one instance of each constant
    if (key.equals(SO_LINGER) || key.equals(SO_TIMEOUT)) {
      continue;
    }
    Object value = opt.getValue();
    // zpencer: Can a netty option be null?
    b.addOption(key.name(), String.valueOf(value));
  }

  NativeSocketOptions nativeOptions
      = NettySocketSupport.getNativeSocketOptions(channel);
  if (nativeOptions != null) {
    b.setTcpInfo(nativeOptions.tcpInfo); // may be null
    for (Entry<String, String> entry : nativeOptions.otherInfo.entrySet()) {
      b.addOption(entry.getKey(), entry.getValue());
    }
  }
  return b.build();
}
 
Example 7
Source File: InboundTrafficController.java    From armeria with Apache License 2.0 4 votes vote down vote up
private InboundTrafficController(@Nullable Channel channel, int highWatermark, int lowWatermark) {
    cfg = channel != null ? channel.config() : null;
    this.highWatermark = highWatermark;
    this.lowWatermark = lowWatermark;
}