查看本类的 API文档回源码主页即时通讯网 - 即时通讯开发者社区!
1   /*
2    * Copyright 2016 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.channel;
17  
18  import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
19  
20  /**
21   * WriteBufferWaterMark is used to set low water mark and high water mark for the write buffer.
22   * <p>
23   * If the number of bytes queued in the write buffer exceeds the
24   * {@linkplain #high high water mark}, {@link Channel#isWritable()}
25   * will start to return {@code false}.
26   * <p>
27   * If the number of bytes queued in the write buffer exceeds the
28   * {@linkplain #high high water mark} and then
29   * dropped down below the {@linkplain #low low water mark},
30   * {@link Channel#isWritable()} will start to return
31   * {@code true} again.
32   * <p>
33   * Note that messages needs to be handled by the {@link MessageSizeEstimator}
34   * used by the channel for {@link Channel#isWritable()} to provide accurate back-pressure.
35   */
36  public final class WriteBufferWaterMark {
37  
38      private static final int DEFAULT_LOW_WATER_MARK = 32 * 1024;
39      private static final int DEFAULT_HIGH_WATER_MARK = 64 * 1024;
40  
41      public static final WriteBufferWaterMark DEFAULT =
42              new WriteBufferWaterMark(DEFAULT_LOW_WATER_MARK, DEFAULT_HIGH_WATER_MARK, false);
43  
44      private final int low;
45      private final int high;
46  
47      /**
48       * Create a new instance.
49       *
50       * @param low low water mark for write buffer.
51       * @param high high water mark for write buffer
52       */
53      public WriteBufferWaterMark(int low, int high) {
54          this(low, high, true);
55      }
56  
57      /**
58       * This constructor is needed to keep backward-compatibility.
59       */
60      WriteBufferWaterMark(int low, int high, boolean validate) {
61          if (validate) {
62              checkPositiveOrZero(low, "low");
63              if (high < low) {
64                  throw new IllegalArgumentException(
65                          "write buffer's high water mark cannot be less than " +
66                                  " low water mark (" + low + "): " +
67                                  high);
68              }
69          }
70          this.low = low;
71          this.high = high;
72      }
73  
74      /**
75       * Returns the low water mark for the write buffer.
76       */
77      public int low() {
78          return low;
79      }
80  
81      /**
82       * Returns the high water mark for the write buffer.
83       */
84      public int high() {
85          return high;
86      }
87  
88      @Override
89      public String toString() {
90          StringBuilder builder = new StringBuilder(55)
91              .append("WriteBufferWaterMark(low: ")
92              .append(low)
93              .append(", high: ")
94              .append(high)
95              .append(")");
96          return builder.toString();
97      }
98  
99  }