use std::collections::VecDeque;
use std::time::{Duration, SystemTime};
use tokio::time::Instant;
use super::listing_pace::ACTIVE_SHARE;
use crate::api::duration;
use crate::github::Spend;
const WINDOW: Duration = Duration::from_mins(10);
const MIN_SPAN: Duration = Duration::from_mins(2);
const MIN_RESERVE: u64 = 100;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Default)]
pub(super) enum Step {
#[default]
Steady,
JobReads2,
JobReads4,
ActiveListings,
Listings2,
Listings4,
}
impl Step {
const ORDER: [Step; 6] = [
Step::Steady,
Step::JobReads2,
Step::JobReads4,
Step::ActiveListings,
Step::Listings2,
Step::Listings4,
];
fn up(self) -> Option<Step> {
Step::ORDER.get(self as usize + 1).copied()
}
fn down(self) -> Option<Step> {
Step::ORDER.get((self as usize).checked_sub(1)?).copied()
}
fn undo_cost(self) -> u128 {
match self {
Step::ActiveListings => u128::from(ACTIVE_SHARE),
_ => 2,
}
}
pub(super) fn reads(self) -> u32 {
match self {
Step::Steady => 1,
Step::JobReads2 => 2,
_ => 4,
}
}
pub(super) fn listings(self) -> u32 {
match self {
Step::Listings2 => 2,
Step::Listings4 => 4,
_ => 1,
}
}
}
#[derive(Debug)]
struct Reading {
at: Instant,
step: Step,
remaining: u64,
reset: SystemTime,
mutations: u64,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) struct Outlook {
pub(super) projected: u64,
pub(super) room: u64,
}
impl Outlook {
pub(super) fn verdict(self, step: Step) -> Step {
if self.projected > self.room {
return step.up().unwrap_or(step);
}
let projected = u128::from(self.projected) * step.undo_cost() * 10;
if self.room > 0 && projected <= u128::from(self.room) * 9 {
return step.down().unwrap_or(step);
}
step
}
}
fn outlook(step: Step, readings: &VecDeque<Reading>, until_reset: Duration) -> Option<Outlook> {
let (oldest, newest) = (readings.front()?, readings.back()?);
let since = readings
.iter()
.rev()
.take_while(|r| r.step == step)
.last()?;
let span = newest.at.saturating_duration_since(since.at);
if span < MIN_SPAN {
return None;
}
let until = until_reset.as_millis();
let dropped = u128::from(since.remaining.saturating_sub(newest.remaining));
let own = u128::from(newest.mutations.saturating_sub(since.mutations));
let projected = dropped.saturating_sub(own) * until / span.as_millis();
let sent = u128::from(newest.mutations.saturating_sub(oldest.mutations));
let needed = sent * until / newest.at.saturating_duration_since(oldest.at).as_millis();
let reserve = needed.max(u128::from(MIN_RESERVE));
Some(Outlook {
projected: u64::try_from(projected).unwrap_or(u64::MAX),
room: u64::try_from(u128::from(newest.remaining).saturating_sub(reserve))
.unwrap_or(u64::MAX),
})
}
fn active_listings(step: Step, poll_interval: Duration) -> Duration {
if step >= Step::ActiveListings {
poll_interval * step.listings()
} else {
poll_interval / ACTIVE_SHARE
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum Cause {
Projected {
outlook: Outlook,
remaining: u64,
until_reset: Duration,
},
Reset,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) struct Change {
pub(super) from: Step,
pub(super) to: Step,
pub(super) cause: Cause,
}
impl Change {
pub(super) fn line(&self, poll_interval: Duration) -> String {
let pace = |step: Step| {
format!(
"running jobs every {}, listings every {} while active and {} while idle",
duration((poll_interval * step.reads()).as_secs()),
duration(active_listings(step, poll_interval).as_secs()),
duration((poll_interval * step.listings()).as_secs()),
)
};
let (way, why) = match self.cause {
Cause::Projected {
outlook,
remaining,
until_reset,
} => (
if self.to > self.from {
"slowed"
} else {
"eased"
},
format!(
"{remaining} requests left, resets in {}, projected spend {} against room for {}",
duration(until_reset.as_secs()),
outlook.projected,
outlook.room
),
),
Cause::Reset => ("eased", "the rate limit reset".to_owned()),
};
format!(
"GitHub reads {way}: {} (was {}); {why}",
pace(self.to),
pace(self.from)
)
}
}
#[derive(Debug, Default)]
pub(super) struct Governor {
readings: VecDeque<Reading>,
}
impl Governor {
pub(super) fn observe(
&mut self,
now: Instant,
wall: SystemTime,
spend: Spend,
) -> Option<Change> {
let limit = spend.rate_limit?;
let until_reset = limit
.reset
.duration_since(wall)
.ok()
.filter(|left| !left.is_zero())?;
let from = self.step();
let started_over = self
.readings
.back()
.is_some_and(|last| last.reset != limit.reset);
if started_over {
self.readings.clear();
}
self.readings.push_back(Reading {
at: now,
step: self.step(),
remaining: limit.remaining,
reset: limit.reset,
mutations: spend.mutations,
});
if let Some(cutoff) = now.checked_sub(WINDOW) {
while self.readings.get(1).is_some_and(|r| r.at <= cutoff) {
self.readings.pop_front();
}
}
let mut cause = started_over.then_some(Cause::Reset);
let step = self.step();
if let Some(outlook) = outlook(step, &self.readings, until_reset) {
let next = outlook.verdict(step);
if next != step {
if let Some(last) = self.readings.back_mut() {
last.step = next;
}
cause = Some(Cause::Projected {
outlook,
remaining: limit.remaining,
until_reset,
});
}
}
(self.step() != from).then_some(Change {
from,
to: self.step(),
cause: cause?,
})
}
pub(super) fn reads_every(&self, poll_interval: Duration) -> Duration {
poll_interval * self.step().reads()
}
pub(super) fn listings_every(&self, poll_interval: Duration) -> Duration {
poll_interval * self.step().listings()
}
pub(super) fn active_listings_every(&self, poll_interval: Duration) -> Duration {
active_listings(self.step(), poll_interval)
}
pub(super) fn step(&self) -> Step {
self.readings.back().map_or(Step::Steady, |r| r.step)
}
#[cfg(test)]
pub(super) fn testing_step(&mut self, step: Step) {
self.readings.push_back(Reading {
at: Instant::now(),
step,
remaining: 0,
reset: SystemTime::UNIX_EPOCH,
mutations: 0,
});
}
#[cfg(test)]
pub(super) fn outlook(&self, until_reset: Duration) -> Option<Outlook> {
outlook(self.step(), &self.readings, until_reset)
}
}