Skip to content

Commit

Permalink
Merge pull request #792 from rnbguy/master
Browse files Browse the repository at this point in the history
feat: support project level `rate_limit`
  • Loading branch information
epage committed Jun 21, 2024
2 parents f0b5af4 + ee1d90c commit ac7281c
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 10 deletions.
2 changes: 2 additions & 0 deletions docs/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ Workspace configuration is read from the following (in precedence order)
| `target` | \- | string | \- | Target triple to use for the verification build |
| `dependent-version` | \- | `upgrade`, `fix`, `error`, `warn`, `ignore` | `upgrade` | Policy for upgrading path dependency versions within the workspace |
| `metadata` | \- | `optional`, `required`, `ignore`, `persistent` | `optional` | Policy for presence of absence of `--metadata` flag when changing the version |
| `rate-limit.new-packages` | \- | integer | `5` | `optional` | Rate limit for publishing new packages |
| `rate-limit.existing-packages` | \- | integer | `30` | `optional` | Rate limit for publishing existing packages |


Note: fields are from the package-configuration unless otherwise specified.
Expand Down
43 changes: 43 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub struct Config {
pub dependent_version: Option<DependentVersion>,
pub metadata: Option<MetadataPolicy>,
pub target: Option<String>,
pub rate_limit: RateLimit,
}

impl Config {
Expand Down Expand Up @@ -85,6 +86,7 @@ impl Config {
dependent_version: Some(empty.dependent_version()),
metadata: Some(empty.metadata()),
target: None,
rate_limit: RateLimit::from_defaults(),
}
}

Expand Down Expand Up @@ -164,6 +166,8 @@ impl Config {
if let Some(target) = source.target.as_deref() {
self.target = Some(target.to_owned());
}

self.rate_limit.update(&source.rate_limit);
}

pub fn allow_branch(&self) -> impl Iterator<Item = &str> {
Expand Down Expand Up @@ -452,6 +456,45 @@ struct CargoMetadata {
release: Option<Config>,
}

#[derive(Debug, Default, Clone, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct RateLimit {
#[serde(default)]
pub new_packages: Option<usize>,
#[serde(default)]
pub existing_packages: Option<usize>,
}

impl RateLimit {
pub fn new() -> Self {
Default::default()
}

pub fn from_defaults() -> Self {
Self {
new_packages: Some(5),
existing_packages: Some(30),
}
}

pub fn update(&mut self, source: &RateLimit) {
if source.new_packages.is_some() {
self.new_packages = source.new_packages;
}
if source.existing_packages.is_some() {
self.existing_packages = source.existing_packages;
}
}

pub fn new_packages(&self) -> usize {
self.new_packages.unwrap_or(5)
}

pub fn existing_packages(&self) -> usize {
self.existing_packages.unwrap_or(30)
}
}

pub fn load_workspace_config(
args: &ConfigArgs,
ws_meta: &cargo_metadata::Metadata,
Expand Down
15 changes: 9 additions & 6 deletions src/steps/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ pub fn verify_monotonically_increasing(
pub fn verify_rate_limit(
pkgs: &[plan::PackageRelease],
index: &mut crate::ops::index::CratesIoIndex,
rate_limit: &crate::config::RateLimit,
dry_run: bool,
level: log::Level,
) -> Result<bool, crate::error::CliError> {
Expand All @@ -236,26 +237,28 @@ pub fn verify_rate_limit(
}
}

if 5 < new {
if rate_limit.new_packages() < new {
// "The rate limit for creating new crates is 1 crate every 10 minutes, with a burst of 5 crates."
success = false;
let _ = crate::ops::shell::log(
level,
format!(
"attempting to publish {} new crates which is above the crates.io rate limit",
new
"attempting to publish {} new crates which is above the rate limit: {}",
new,
rate_limit.new_packages()
),
);
}

if 30 < existing {
if rate_limit.existing_packages() < existing {
// "The rate limit for new versions of existing crates is 1 per minute, with a burst of 30 crates, so when releasing new versions of these crates, you shouldn't hit the limit."
success = false;
let _ = crate::ops::shell::log(
level,
format!(
"attempting to publish {} existing crates which is above the crates.io rate limit",
existing
"attempting to publish {} existing crates which is above the rate limit: {}",
existing,
rate_limit.existing_packages()
),
);
}
Expand Down
9 changes: 7 additions & 2 deletions src/steps/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,13 @@ impl PublishStep {
)?;

failed |= !super::verify_metadata(&selected_pkgs, dry_run, log::Level::Error)?;
failed |=
!super::verify_rate_limit(&selected_pkgs, &mut index, dry_run, log::Level::Error)?;
failed |= !super::verify_rate_limit(
&selected_pkgs,
&mut index,
&ws_config.rate_limit,
dry_run,
log::Level::Error,
)?;

// STEP 1: Release Confirmation
super::confirm("Publish", &selected_pkgs, self.no_confirm, dry_run)?;
Expand Down
9 changes: 7 additions & 2 deletions src/steps/release.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,13 @@ impl ReleaseStep {
)?;

failed |= !super::verify_metadata(&selected_pkgs, dry_run, log::Level::Error)?;
failed |=
!super::verify_rate_limit(&selected_pkgs, &mut index, dry_run, log::Level::Error)?;
failed |= !super::verify_rate_limit(
&selected_pkgs,
&mut index,
&ws_config.rate_limit,
dry_run,
log::Level::Error,
)?;

// STEP 1: Release Confirmation
super::confirm("Release", &selected_pkgs, self.no_confirm, dry_run)?;
Expand Down

0 comments on commit ac7281c

Please sign in to comment.