closer to flow control

This commit is contained in:
Oliver Gould
2017-07-12 21:04:58 +00:00
parent b9f3556070
commit 41ffd1d44f
11 changed files with 337 additions and 179 deletions

View File

@@ -2,7 +2,7 @@ use Frame;
use client::Client;
use error::{self, ConnectionError};
use frame::{self, StreamId};
use proto::{self, Peer, ReadySink, State, PeerState, WindowUpdate, FlowController};
use proto::{self, Peer, ReadySink, State, FlowController};
use server::Server;
use tokio_io::{AsyncRead, AsyncWrite};
@@ -15,12 +15,9 @@ use futures::*;
use ordermap::OrderMap;
use fnv::FnvHasher;
use std::collections::VecDeque;
use std::hash::BuildHasherDefault;
use std::marker::PhantomData;
// TODO get window size from `inner`.
/// An H2 connection
#[derive(Debug)]
pub struct Connection<T, P, B: IntoBuf = Bytes> {
@@ -30,95 +27,89 @@ pub struct Connection<T, P, B: IntoBuf = Bytes> {
/// Tracks connection-level flow control.
local_flow_controller: FlowController,
initial_local_window_size: u32,
pending_local_window_updates: VecDeque<WindowUpdate>,
remote_flow_controller: FlowController,
initial_remote_window_size: u32,
pending_remote_window_updates: VecDeque<WindowUpdate>,
blocked_remote_window_update: Option<task::Task>
pending_local_window_update: Option<frame::WindowUpdate>,
blocked_remote_window_update: Option<task::Task>,
}
type StreamMap<T> = OrderMap<StreamId, T, BuildHasherDefault<FnvHasher>>;
pub fn new<T, P, B>(transport: proto::Inner<T, B::Buf>,
initial_local_window_size: u32,
initial_remote_window_size: u32)
pub fn new<T, P, B>(transport: proto::Inner<T, B::Buf>)
-> Connection<T, P, B>
where T: AsyncRead + AsyncWrite,
P: Peer,
B: IntoBuf,
{
let local_window_size = transport.local_settings().initial_window_size();
let remote_window_size = transport.remote_settings().initial_window_size();
Connection {
inner: transport,
streams: StreamMap::default(),
peer: PhantomData,
local_flow_controller: FlowController::new(initial_local_window_size),
initial_local_window_size,
pending_local_window_updates: VecDeque::default(),
local_flow_controller: FlowController::new(local_window_size),
remote_flow_controller: FlowController::new(remote_window_size),
remote_flow_controller: FlowController::new(initial_remote_window_size),
initial_remote_window_size,
pending_remote_window_updates: VecDeque::default(),
pending_local_window_update: None,
blocked_remote_window_update: None,
}
}
impl<T, P, B: IntoBuf> Connection<T, P, B> {
pub fn poll_remote_window_update(&mut self, id: StreamId) -> Poll<u32, ConnectionError> {
if id.is_zero() {
return match self.local_flow_controller.take_window_update() {
Some(incr) => Ok(Async::Ready(incr)),
None => {
self.blocked_remote_window_update = Some(task::current());
Ok(Async::NotReady)
}
};
}
match self.streams.get_mut(&id).and_then(|mut s| s.take_remote_window_update()) {
Some(incr) => Ok(Async::Ready(incr)),
None => {
self.blocked_remote_window_update = Some(task::current());
Ok(Async::NotReady)
}
}
}
/// Publishes local stream window updates to the remote.
///
/// Connection window updates (StreamId=0) and stream window must be published
/// distinctly.
pub fn increment_local_window(&mut self, up: WindowUpdate) {
let added = match &up {
&WindowUpdate::Connection { increment } => {
if increment == 0 {
false
} else {
self.local_flow_controller.increment(increment);
true
}
}
&WindowUpdate::Stream { id, increment } => {
if increment == 0 {
false
} else {
match self.streams.get_mut(&id) {
Some(&mut State::Open { local: PeerState::Data(ref mut fc), .. }) |
Some(&mut State::HalfClosedRemote(PeerState::Data(ref mut fc))) => {
fc.increment(increment);
true
}
_ => false,
}
}
}
pub fn init_send_window_update(&mut self, id: StreamId, incr: u32) {
assert!(self.pending_local_window_update.is_none());
let added = if id.is_zero() {
self.remote_flow_controller.add_to_window(incr);
self.remote_flow_controller.take_window_update()
} else {
self.streams.get_mut(&id).and_then(|mut s| s.send_window_update(incr))
};
if added {
self.pending_local_window_updates.push_back(up);
if let Some(added) = added {
self.pending_local_window_update = Some(frame::WindowUpdate::new(id, added));
}
}
/// Advertises the remote's stream window updates.
///
/// Connection window updates (StreamId=0) and stream window updates are advertised
/// Connection window updates (id=0) and stream window updates are advertised
/// distinctly.
fn increment_remote_window(&mut self, id: StreamId, incr: u32) {
fn recv_window_update(&mut self, id: StreamId, incr: u32) {
if id.is_zero() {
self.remote_flow_controller.increment(incr);
} else {
match self.streams.get_mut(&id) {
Some(&mut State::Open { remote: PeerState::Data(ref mut fc), .. }) |
Some(&mut State::HalfClosedLocal(PeerState::Data(ref mut fc))) => {
fc.increment(incr);
}
_ => {}
}
return self.remote_flow_controller.add_to_window(incr);
}
if let Some(mut s) = self.streams.get_mut(&id) {
s.recv_window_update(incr);
}
unimplemented!()
}
}
@@ -127,16 +118,28 @@ impl<T, P, B> Connection<T, P, B>
P: Peer,
B: IntoBuf,
{
fn poll_send_window_update(&mut self) -> Poll<(), ConnectionError> {
if let Some(f) = self.pending_local_window_update.take() {
if self.inner.start_send(f.into())?.is_not_ready() {
self.pending_local_window_update = Some(f);
return Ok(Async::NotReady);
}
}
Ok(Async::Ready(()))
}
pub fn send_data(self,
id: StreamId,
data: B,
data_len: usize,
end_of_stream: bool)
-> sink::Send<Self>
{
self.send(Frame::Data {
id: id,
data: data,
end_of_stream: end_of_stream,
id,
data_len,
data,
end_of_stream,
})
}
}
@@ -201,15 +204,13 @@ impl<T, P, B> Stream for Connection<T, P, B>
};
trace!("received; frame={:?}", frame);
let frame = match frame {
Some(Headers(v)) => {
// TODO: Update stream state
let stream_id = v.stream_id();
let end_of_stream = v.is_end_stream();
// TODO load window size from settings.
let init_window_size = 65_535;
let init_window_size = self.inner.local_settings().initial_window_size();
let stream_initialized = try!(self.streams.entry(stream_id)
.or_insert(State::default())
@@ -231,26 +232,27 @@ impl<T, P, B> Stream for Connection<T, P, B>
end_of_stream: end_of_stream,
}
}
Some(Data(v)) => {
// TODO: Validate frame
Some(Data(v)) => {
let stream_id = v.stream_id();
let end_of_stream = v.is_end_stream();
match self.streams.get_mut(&stream_id) {
None => return Err(error::Reason::ProtocolError.into()),
Some(state) => try!(state.recv_data(end_of_stream)),
Some(state) => try!(state.recv_data(end_of_stream, v.len())),
}
Frame::Data {
id: stream_id,
data_len: v.len(),
data: v.into_payload(),
end_of_stream: end_of_stream,
end_of_stream,
}
}
Some(WindowUpdate(v)) => {
self.increment_remote_window(v.stream_id(), v.size_increment());
self.recv_window_update(v.stream_id(), v.size_increment());
continue;
}
Some(frame) => panic!("unexpected frame; frame={:?}", frame),
None => return Ok(Async::Ready(None)),
};
@@ -268,24 +270,31 @@ impl<T, P, B> Sink for Connection<T, P, B>
type SinkItem = Frame<P::Send, B>;
type SinkError = ConnectionError;
/// Sends a frame to the remote.
fn start_send(&mut self, item: Self::SinkItem)
-> StartSend<Self::SinkItem, Self::SinkError>
{
use frame::Frame::Headers;
// First ensure that the upstream can process a new item
if !try!(self.poll_ready()).is_ready() {
// First ensure that the upstream can process a new item. This ensures, for
// instance, that any pending local window updates have been sent to the remote
// before sending any other frames.
if try!(self.poll_ready()).is_not_ready() {
return Ok(AsyncSink::NotReady(item));
}
assert!(self.pending_local_window_update.is_none());
match item {
Frame::Headers { id, headers, end_of_stream } => {
// TODO load window size from settings.
let init_window_size = 65_535;
let init_window_size = self.inner.remote_settings().initial_window_size();
// Transition the stream state, creating a new entry if needed
// TODO: Response can send multiple headers frames before body
// (1xx responses).
//
// TODO: Response can send multiple headers frames before body (1xx
// responses).
//
// ACTUALLY(ver), maybe not?
// https://github.com/http2/http2-spec/commit/c83c8d911e6b6226269877e446a5cad8db921784
let stream_initialized = try!(self.streams.entry(id)
.or_insert(State::default())
.send_headers::<P>(end_of_stream, init_window_size));
@@ -294,7 +303,6 @@ impl<T, P, B> Sink for Connection<T, P, B>
// TODO: Ensure available capacity for a new stream
// This won't be as simple as self.streams.len() as closed
// connections should not be factored.
//
if !P::is_valid_local_stream_id(id) {
// TODO: clear state
return Err(error::User::InvalidStreamId.into());
@@ -314,25 +322,26 @@ impl<T, P, B> Sink for Connection<T, P, B>
Ok(AsyncSink::Ready)
}
Frame::Data { id, data, end_of_stream } => {
Frame::Data { id, data, data_len, end_of_stream } => {
// The stream must be initialized at this point
match self.streams.get_mut(&id) {
None => return Err(error::User::InactiveStreamId.into()),
Some(state) => try!(state.send_data(end_of_stream)),
Some(state) => try!(state.send_data(end_of_stream, data_len)),
}
let mut frame = frame::Data::new(id, data.into_buf());
let mut frame = frame::Data::from_buf(id, data.into_buf());
if end_of_stream {
frame.set_end_stream();
}
let res = try!(self.inner.start_send(frame.into()));
// poll_ready has already been called.
assert!(res.is_ready());
Ok(AsyncSink::Ready)
}
/*
Frame::Trailers { id, headers } => {
unimplemented!();
@@ -352,6 +361,7 @@ impl<T, P, B> Sink for Connection<T, P, B>
}
fn poll_complete(&mut self) -> Poll<(), ConnectionError> {
try_ready!(self.poll_send_window_update());
self.inner.poll_complete()
}
}
@@ -362,6 +372,7 @@ impl<T, P, B> ReadySink for Connection<T, P, B>
B: IntoBuf,
{
fn poll_ready(&mut self) -> Poll<(), Self::SinkError> {
try_ready!(self.poll_send_window_update());
self.inner.poll_ready()
}
}