crates/edge/proto/src/job.rs 4.6 KiB 159 lines Raw
1 //! One job: open, active, closed.2 //!3 //! Protocol.md section 7 as a state machine, plus the credit of section 8 and4 //! the cancel flag of section 9. There is no I/O here, so every rule is a test.5 //!6 //! A job is open when the caller may still be refused for free. It is active7 //! when the server has started to spend. That boundary is the only place where8 //! the two terminal frames differ, and it is why `reject` after `open` is a9 //! protocol error and not a slower `done`.10 11 use std::fmt;12 13 use crate::credit::{Credit, CreditError};14 15 /// How far one job has got.16 #[derive(Clone, Copy, PartialEq, Eq, Debug)]17 pub enum JobState {18     Open,19     Active,20     Closed,21 }22 23 /// The answer to one attempt to send an event.24 #[derive(Clone, Copy, PartialEq, Eq, Debug)]25 pub enum Emit {26     /// The bytes are on the wire. The credit was counted.27     Sent,28     /// The peer has not granted that many bytes. Wait for a `window`.29     Blocked,30     /// The caller cancelled. Send the terminal frame and stop.31     Cancelled,32 }33 34 /// What a cancel obliges the server to send.35 #[derive(Clone, Copy, PartialEq, Eq, Debug)]36 pub enum Answer {37     /// The job was open: nothing was spent, so refuse.38     Reject,39     /// The job was active: work was spent, so end it.40     Done,41     /// The job was already closed: send nothing.42     Nothing,43 }44 45 /// A state change that the job does not allow.46 #[derive(Clone, Copy, PartialEq, Eq, Debug)]47 pub enum JobError {48     AcceptWhile(JobState),49     DoneWhile(JobState),50     Credit(CreditError),51 }52 53 impl fmt::Display for JobError {54     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {55         match self {56             JobError::AcceptWhile(s) => write!(f, "cannot accept while {s:?}"),57             JobError::DoneWhile(s) => write!(f, "cannot finish while {s:?}"),58             JobError::Credit(e) => write!(f, "{e}"),59         }60     }61 }62 63 impl From<CreditError> for JobError {64     fn from(e: CreditError) -> JobError {65         JobError::Credit(e)66     }67 }68 69 /// One stream of work.70 #[derive(Clone, Copy, Debug)]71 pub struct Job {72     state: JobState,73     credit: Credit,74     cancelled: bool,75 }76 77 impl Job {78     /// A job that the caller opened with `credit` bytes of initial window.79     pub fn new(credit: u64) -> Job {80         Job {81             state: JobState::Open,82             credit: Credit::new(credit),83             cancelled: false,84         }85     }86 87     pub fn state(self) -> JobState {88         self.state89     }90 91     pub fn credit(self) -> Credit {92         self.credit93     }94 95     /// True once the caller has cancelled. The worker checks this at the top of96     /// its own loop, not only inside `emit`.97     pub fn is_cancelled(self) -> bool {98         self.cancelled99     }100 101     /// The server has started to spend. Only `done` can follow.102     pub fn accept(&mut self) -> Result<(), JobError> {103         match self.state {104             JobState::Open => {105                 self.state = JobState::Active;106                 Ok(())107             }108             other => Err(JobError::AcceptWhile(other)),109         }110     }111 112     /// A terminal frame has been sent. Both terminal frames land here.113     ///114     /// The outcome travels in the body, not in the state. A closed job has no115     /// further transitions, which is the fact this machine needs.116     pub fn close(&mut self) {117         self.state = JobState::Closed;118     }119 120     /// The caller sent `cancel`.121     ///122     /// A cancel is not credit and it is not monotonic. It is recorded once and123     /// the first answer stands, because a second `cancel` on a closed job must124     /// not produce a second terminal frame.125     pub fn cancel(&mut self) -> Answer {126         match self.state {127             JobState::Open => {128                 self.state = JobState::Closed;129                 Answer::Reject130             }131             JobState::Active => {132                 self.cancelled = true;133                 Answer::Done134             }135             JobState::Closed => Answer::Nothing,136         }137     }138 139     /// Applies a `window`. A watermark below what was sent is an error.140     pub fn window(&mut self, watermark: u64) -> Result<(), JobError> {141         self.credit.grant(watermark)?;142         Ok(())143     }144 145     /// Offers one event body of `len` bytes to the wire.146     ///147     /// `Emit::Blocked` is not an error and not a wait: the SDK waits, then148     /// calls again. That is the one place where a worker blocks.149     pub fn emit(&mut self, len: u64) -> Result<Emit, JobError> {150         if self.cancelled || self.state == JobState::Closed {151             return Ok(Emit::Cancelled);152         }153         if self.credit.may_send() < len {154             return Ok(Emit::Blocked);155         }156         self.credit.take(len)?;157         Ok(Emit::Sent)158     }159 }