コード例 #1
0
  protected void decreaseCounter(Runnable task) {
    if (!shouldCount(task)) {
      return;
    }

    Settings settings = this.settings;
    long maxChannelMemorySize = settings.maxChannelMemorySize;

    int increment;
    if (task instanceof ChannelEventRunnable) {
      increment = ((ChannelEventRunnable) task).estimatedSize;
    } else {
      increment = ((MemoryAwareRunnable) task).estimatedSize;
    }

    if (totalLimiter != null) {
      totalLimiter.decrease(increment);
    }

    if (task instanceof ChannelEventRunnable) {
      ChannelEventRunnable eventTask = (ChannelEventRunnable) task;
      Channel channel = eventTask.getEvent().getChannel();
      long channelCounter = getChannelCounter(channel).addAndGet(-increment);
      // System.out.println("DC: " + channelCounter + ", " + increment);
      if (maxChannelMemorySize != 0 && channelCounter < maxChannelMemorySize && channel.isOpen()) {
        if (!channel.isReadable()) {
          // System.out.println("READABLE");
          ChannelHandlerContext ctx = eventTask.getContext();
          if (ctx.getHandler() instanceof ExecutionHandler) {
            // check if the attachment was set as this means that we suspend the channel from reads.
            // This only works when
            // this pool is used with ExecutionHandler but I guess thats good enough for us.
            //
            // See #215
            if (ctx.getAttachment() != null) {
              // readSuspended = false;
              ctx.setAttachment(null);
              channel.setReadable(true);
            }
          } else {
            channel.setReadable(true);
          }
        }
      }
    }
  }
コード例 #2
0
ファイル: ExecutionHandler.java プロジェクト: rkroll/netty
  /** Handle suspended reads */
  protected boolean handleReadSuspend(ChannelHandlerContext ctx, ChannelEvent e) {
    if (e instanceof ChannelStateEvent) {
      ChannelStateEvent cse = (ChannelStateEvent) e;
      if (cse.getState() == ChannelState.INTEREST_OPS
          && (((Integer) cse.getValue()).intValue() & Channel.OP_READ) != 0) {

        // setReadable(true) requested
        boolean readSuspended = ctx.getAttachment() != null;
        if (readSuspended) {
          // Drop the request silently if MemoryAwareThreadPool has
          // set the flag.
          e.getFuture().setSuccess();
          return true;
        }
      }
    }

    return false;
  }