View Javadoc
1   /*
2    * Copyright 2021 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.handler.codec.quic;
17  
18  import io.netty.util.internal.ObjectUtil;
19  
20  /**
21   * Allows to configure a strategy for when flushes should be happening.
22   */
23  public interface FlushStrategy {
24  
25      /**
26       * Default {@link FlushStrategy} implementation.
27       */
28      FlushStrategy DEFAULT = afterNumBytes(20 * Quic.MAX_DATAGRAM_SIZE);
29  
30      /**
31       * Returns {@code true} if a flush should happen now, {@code false} otherwise.
32       *
33       * @param numPackets    the number of packets that were written since the last flush.
34       * @param numBytes      the number of bytes that were written since the last flush.
35       * @return              {@code true} if a flush should be done now, {@code false} otherwise.
36       */
37      boolean shouldFlushNow(int numPackets, int numBytes);
38  
39      /**
40       * Implementation that flushes after a number of bytes.
41       *
42       * @param bytes the number of bytes after which we should issue a flush.
43       * @return the {@link FlushStrategy}.
44       */
45      static FlushStrategy afterNumBytes(int bytes) {
46          ObjectUtil.checkPositive(bytes, "bytes");
47          return (numPackets, numBytes) -> numBytes > bytes;
48      }
49  
50      /**
51       * Implementation that flushes after a number of packets.
52       *
53       * @param packets the number of packets after which we should issue a flush.
54       * @return the {@link FlushStrategy}.
55       */
56      static FlushStrategy afterNumPackets(int packets) {
57          ObjectUtil.checkPositive(packets, "packets");
58          return (numPackets, numBytes) -> numPackets > packets;
59      }
60  }