1 //! Credit: how much one side may still send on one stream.2 //!3 //! A `window` frame carries an absolute watermark. "You may send up to byte N4 //! of this stream." It is not a delta, so a lost or doubled frame cannot drift5 //! the count: the receiver states the position again and both sides agree.6 //!7 //! Only `event` frames are counted. Every other frame is control. A control8 //! frame that needed credit could not grant credit or end a job, so the pair9 //! would deadlock.10 11 use std::fmt;12 13 use crate::frame::Kind;14 15 /// The send position of one stream.16 #[derive(Clone, Copy, PartialEq, Eq, Debug)]17 pub struct Credit {18 limit: u64,19 sent: u64,20 }21 22 impl Credit {23 /// A stream whose peer has already granted `limit` bytes.24 pub fn new(limit: u64) -> Credit {25 Credit { limit, sent: 0 }26 }27 28 /// Bytes that this side may still send.29 pub fn may_send(self) -> u64 {30 self.limit - self.sent31 }32 33 /// Bytes that this side has sent.34 pub fn sent(self) -> u64 {35 self.sent36 }37 38 /// The peer's latest absolute watermark.39 pub fn limit(self) -> u64 {40 self.limit41 }42 43 /// Applies a `window` frame.44 ///45 /// A watermark is monotonic. A value below the current limit is a protocol46 /// error even when it is still above the send position: the peer may not47 /// shrink a window that it already granted. Equal is not shrink.48 pub fn grant(&mut self, window: u64) -> Result<(), CreditError> {49 if window < self.limit {50 return Err(CreditError::BelowLimit {51 window,52 limit: self.limit,53 });54 }55 self.limit = window;56 Ok(())57 }58 59 /// Counts one frame. Call this after the frame is on the wire.60 ///61 /// A terminal frame is not counted. Use [`credit_limited`] to decide.62 pub fn take(&mut self, bytes: u64) -> Result<(), CreditError> {63 let have = self.may_send();64 if bytes > have {65 return Err(CreditError::Overdraft { want: bytes, have });66 }67 self.sent += bytes;68 Ok(())69 }70 }71 72 /// True when a frame must wait for credit.73 ///74 /// Only `event` frames are counted. Protocol.md section 8: the server tracks75 /// the bytes of `event` bodies on one stream. Every other frame is control, and76 /// a control frame that needs credit cannot grant credit or end a job. Stream 077 /// is the session and carries no events.78 pub fn credit_limited(kind: Kind, stream: u16) -> bool {79 stream != 0 && matches!(kind, Kind::Event)80 }81 82 /// A credit accounting error. Both cases are protocol errors.83 #[derive(Clone, Copy, PartialEq, Eq, Debug)]84 pub enum CreditError {85 /// The peer lowered a watermark that it had already granted.86 BelowLimit { window: u64, limit: u64 },87 /// This side tried to send more than the peer granted.88 Overdraft { want: u64, have: u64 },89 }90 91 impl fmt::Display for CreditError {92 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {93 match *self {94 CreditError::BelowLimit { window, limit } => {95 write!(f, "window {window} is below the granted limit {limit}")96 }97 CreditError::Overdraft { want, have } => {98 write!(f, "send of {want} bytes with {have} bytes of credit")99 }100 }101 }102 }103 104 impl std::error::Error for CreditError {}