View Javadoc
1   /*
2    * Copyright 2014 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License, version 2.0 (the
5    * "License"); you may not use this file except in compliance with the License. You may obtain a
6    * 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 distributed under the License
11   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing permissions and limitations under
13   * the License.
14   */
15  package io.netty.handler.codec.http2;
16  
17  import io.netty.buffer.ByteBuf;
18  import io.netty.channel.ChannelFuture;
19  import io.netty.channel.ChannelHandlerContext;
20  import io.netty.channel.ChannelPromise;
21  
22  /**
23   * Manager for the life cycle of the HTTP/2 connection. Handles graceful shutdown of the channel,
24   * closing only after all of the streams have closed.
25   */
26  public interface Http2LifecycleManager {
27  
28      /**
29       * Closes the local side of the {@code stream}. Depending on the {@code stream} state this may result in
30       * {@code stream} being closed. See {@link #closeStream(Http2Stream, ChannelFuture)}.
31       * @param stream the stream to be half closed.
32       * @param future See {@link #closeStream(Http2Stream, ChannelFuture)}.
33       */
34      void closeStreamLocal(Http2Stream stream, ChannelFuture future);
35  
36      /**
37       * Closes the remote side of the {@code stream}. Depending on the {@code stream} state this may result in
38       * {@code stream} being closed. See {@link #closeStream(Http2Stream, ChannelFuture)}.
39       * @param stream the stream to be half closed.
40       * @param future See {@link #closeStream(Http2Stream, ChannelFuture)}.
41       */
42      void closeStreamRemote(Http2Stream stream, ChannelFuture future);
43  
44      /**
45       * Closes and deactivates the given {@code stream}. A listener is also attached to {@code future} and upon
46       * completion the underlying channel will be closed if {@link Http2Connection#numActiveStreams()} is 0.
47       * @param stream the stream to be closed and deactivated.
48       * @param future when completed if {@link Http2Connection#numActiveStreams()} is 0 then the underlying channel
49       * will be closed.
50       */
51      void closeStream(Http2Stream stream, ChannelFuture future);
52  
53      /**
54       * Ensure the stream identified by {@code streamId} is reset. If our local state does not indicate the stream has
55       * been reset yet then a {@code RST_STREAM} will be sent to the peer. If our local state indicates the stream
56       * has already been reset then the return status will indicate success without sending anything to the peer.
57       * @param ctx The context used for communication and buffer allocation if necessary.
58       * @param streamId The identifier of the stream to reset.
59       * @param errorCode Justification as to why this stream is being reset. See {@link Http2Error}.
60       * @param promise Used to indicate the return status of this operation.
61       * @return Will be considered successful when the connection and stream state has been updated, and a
62       * {@code RST_STREAM} frame has been sent to the peer. If the stream state has already been updated and a
63       * {@code RST_STREAM} frame has been sent then the return status may indicate success immediately.
64       */
65      ChannelFuture resetStream(ChannelHandlerContext ctx, int streamId, long errorCode,
66              ChannelPromise promise);
67  
68      /**
69       * Prevents the peer from creating streams and close the connection if {@code errorCode} is not
70       * {@link Http2Error#NO_ERROR}. After this call the peer is not allowed to create any new streams and the local
71       * endpoint will be limited to creating streams with {@code stream identifier <= lastStreamId}. This may result in
72       * sending a {@code GO_AWAY} frame (assuming we have not already sent one with
73       * {@code Last-Stream-ID <= lastStreamId}), or may just return success if a {@code GO_AWAY} has previously been
74       * sent.
75       * @param ctx The context used for communication and buffer allocation if necessary.
76       * @param lastStreamId The last stream that the local endpoint is claiming it will accept.
77       * @param errorCode The rational as to why the connection is being closed. See {@link Http2Error}.
78       * @param debugData For diagnostic purposes (carries no semantic value).
79       * @param promise Used to indicate the return status of this operation.
80       * @return Will be considered successful when the connection and stream state has been updated, and a
81       * {@code GO_AWAY} frame has been sent to the peer. If the stream state has already been updated and a
82       * {@code GO_AWAY} frame has been sent then the return status may indicate success immediately.
83       */
84      ChannelFuture goAway(ChannelHandlerContext ctx, int lastStreamId, long errorCode,
85              ByteBuf debugData, ChannelPromise promise);
86  
87      /**
88       * Processes the given error.
89       *
90       * @param ctx The context used for communication and buffer allocation if necessary.
91       * @param outbound {@code true} if the error was caused by an outbound operation and so the corresponding
92       * {@link ChannelPromise} was failed as well.
93       * @param cause the error.
94       */
95      void onError(ChannelHandlerContext ctx, boolean outbound, Throwable cause);
96  }