1use crate::codec::Codec;
2use crate::frame::{self, Reason, StreamId};
34use bytes::Buf;
5use std::io;
6use std::task::{Context, Poll};
7use tokio::io::AsyncWrite;
89/// Manages our sending of GOAWAY frames.
10#[derive(Debug)]
11pub(super) struct GoAway {
12/// Whether the connection should close now, or wait until idle.
13close_now: bool,
14/// Records if we've sent any GOAWAY before.
15going_away: Option<GoingAway>,
16/// Whether the user started the GOAWAY by calling `abrupt_shutdown`.
17is_user_initiated: bool,
18/// A GOAWAY frame that must be buffered in the Codec immediately.
19pending: Option<frame::GoAway>,
20}
2122/// Keeps a memory of any GOAWAY frames we've sent before.
23///
24/// This looks very similar to a `frame::GoAway`, but is a separate type. Why?
25/// Mostly for documentation purposes. This type is to record status. If it
26/// were a `frame::GoAway`, it might appear like we eventually wanted to
27/// serialize it. We **only** want to be able to look up these fields at a
28/// later time.
29#[derive(Debug)]
30pub(crate) struct GoingAway {
31/// Stores the highest stream ID of a GOAWAY that has been sent.
32 ///
33 /// It's illegal to send a subsequent GOAWAY with a higher ID.
34last_processed_id: StreamId,
3536/// Records the error code of any GOAWAY frame sent.
37reason: Reason,
38}
3940impl GoAway {
41pub fn new() -> Self {
42 GoAway {
43 close_now: false,
44 going_away: None,
45 is_user_initiated: false,
46 pending: None,
47 }
48 }
4950/// Enqueue a GOAWAY frame to be written.
51 ///
52 /// The connection is expected to continue to run until idle.
53pub fn go_away(&mut self, f: frame::GoAway) {
54if let Some(ref going_away) = self.going_away {
55assert!(
56 f.last_stream_id() <= going_away.last_processed_id,
57"GOAWAY stream IDs shouldn't be higher; \
58 last_processed_id = {:?}, f.last_stream_id() = {:?}",
59 going_away.last_processed_id,
60 f.last_stream_id(),
61 );
62 }
6364self.going_away = Some(GoingAway {
65 last_processed_id: f.last_stream_id(),
66 reason: f.reason(),
67 });
68self.pending = Some(f);
69 }
7071pub fn go_away_now(&mut self, f: frame::GoAway) {
72self.close_now = true;
73if let Some(ref going_away) = self.going_away {
74// Prevent sending the same GOAWAY twice.
75if going_away.last_processed_id == f.last_stream_id() && going_away.reason == f.reason()
76 {
77return;
78 }
79 }
80self.go_away(f);
81 }
8283pub fn go_away_from_user(&mut self, f: frame::GoAway) {
84self.is_user_initiated = true;
85self.go_away_now(f);
86 }
8788/// Return if a GOAWAY has ever been scheduled.
89pub fn is_going_away(&self) -> bool {
90self.going_away.is_some()
91 }
9293pub fn is_user_initiated(&self) -> bool {
94self.is_user_initiated
95 }
9697/// Returns the going away info, if any.
98pub fn going_away(&self) -> Option<&GoingAway> {
99self.going_away.as_ref()
100 }
101102/// Returns if the connection should close now, or wait until idle.
103pub fn should_close_now(&self) -> bool {
104self.pending.is_none() && self.close_now
105 }
106107/// Returns if the connection should be closed when idle.
108pub fn should_close_on_idle(&self) -> bool {
109 !self.close_now
110 && self
111.going_away
112 .as_ref()
113 .map(|g| g.last_processed_id != StreamId::MAX)
114 .unwrap_or(false)
115 }
116117/// Try to write a pending GOAWAY frame to the buffer.
118 ///
119 /// If a frame is written, the `Reason` of the GOAWAY is returned.
120pub fn send_pending_go_away<T, B>(
121&mut self,
122 cx: &mut Context,
123 dst: &mut Codec<T, B>,
124 ) -> Poll<Option<io::Result<Reason>>>
125where
126T: AsyncWrite + Unpin,
127 B: Buf,
128 {
129if let Some(frame) = self.pending.take() {
130if !dst.poll_ready(cx)?.is_ready() {
131self.pending = Some(frame);
132return Poll::Pending;
133 }
134135let reason = frame.reason();
136 dst.buffer(frame.into()).expect("invalid GOAWAY frame");
137138return Poll::Ready(Some(Ok(reason)));
139 } else if self.should_close_now() {
140return match self.going_away().map(|going_away| going_away.reason) {
141Some(reason) => Poll::Ready(Some(Ok(reason))),
142None => Poll::Ready(None),
143 };
144 }
145146 Poll::Ready(None)
147 }
148}
149150impl GoingAway {
151pub(crate) fn reason(&self) -> Reason {
152self.reason
153 }
154}