Radish alpha
r
Radicle CI broker
Radicle
Git (anonymous pull)
Log in to clone via SSH
refactor: use a typed Duration constant instead of integer
Lars Wirzenius committed 1 year ago
commit 8c055c1f3b695d48c82f86b2a6ef6b6fd6f92e53
parent 0511e7770235df3e4d84bed3e842d3d23f837312
2 files changed +12 -11
modified src/db.rs
@@ -23,7 +23,8 @@ use uuid::Uuid;

use crate::{event::BrokerEvent, msg::RunId, run::Run};

-
const MAX_WAIT: u64 = 1000; // how long to retry when SQL fails for busy database
+
// how long to retry when SQL fails for busy database
+
const MAX_WAIT: Duration = Duration::from_millis(1000);

/// The CI broker database. It stores the data that needs to be
/// persistent, even if the process terminates.
@@ -93,7 +94,7 @@ impl Db {
    // Prepare a statement for execution.
    fn prepare<'a>(&'a self, sql: &str) -> Result<Stmt<'a>, DbError> {
        let started = Instant::now();
-
        let max_wait = Duration::from_millis(MAX_WAIT);
+
        let max_wait = MAX_WAIT;
        loop {
            match self.conn.prepare(sql) {
                Ok(stmt) => {
@@ -124,7 +125,7 @@ impl Db {
        stmt.stmt.reset().map_err(DbError::reset)?;

        let started = Instant::now();
-
        let max_wait = Duration::from_millis(MAX_WAIT);
+
        let max_wait = MAX_WAIT;
        loop {
            match stmt.stmt.next() {
                Ok(_) => {
@@ -180,7 +181,7 @@ impl Db {
        let mut counter = None;

        let started = Instant::now();
-
        let max_wait = Duration::from_millis(MAX_WAIT);
+
        let max_wait = MAX_WAIT;

        loop {
            match select.stmt.next() {
@@ -210,7 +211,7 @@ impl Db {
        let mut select = self.prepare("SELECT id FROM event_queue")?;

        let started = Instant::now();
-
        let max_wait = Duration::from_millis(MAX_WAIT);
+
        let max_wait = MAX_WAIT;

        let mut ids = vec![];

@@ -250,7 +251,7 @@ impl Db {
            .map_err(|e| DbError::bind(&select.sql, e))?;

        let started = Instant::now();
-
        let max_wait = Duration::from_millis(MAX_WAIT);
+
        let max_wait = MAX_WAIT;

        let mut timestamp = None;
        let mut event = None;
@@ -353,7 +354,7 @@ impl Db {
        let mut select = self.prepare("SELECT run_id FROM ci_runs")?;

        let started = Instant::now();
-
        let max_wait = Duration::from_millis(MAX_WAIT);
+
        let max_wait = MAX_WAIT;

        let mut run_ids = vec![];

@@ -391,7 +392,7 @@ impl Db {
        let mut select = self.prepare("SELECT json FROM ci_runs")?;

        let started = Instant::now();
-
        let max_wait = Duration::from_millis(MAX_WAIT);
+
        let max_wait = MAX_WAIT;

        let mut runs = vec![];

@@ -434,7 +435,7 @@ impl Db {
            .map_err(|e| DbError::bind(&select.sql, e))?;

        let started = Instant::now();
-
        let max_wait = Duration::from_millis(MAX_WAIT);
+
        let max_wait = MAX_WAIT;

        let mut run = None;

modified src/queueproc.rs
@@ -18,7 +18,7 @@ use crate::{
    msg::{MessageError, RequestBuilder},
};

-
const WAIT_FOR_EVENTS_DURATION: u64 = 10_000;
+
const WAIT_FOR_EVENTS_DURATION: Duration = Duration::from_millis(10_000);

#[derive(Default)]
pub struct QueueProcessorBuilder {
@@ -125,7 +125,7 @@ impl QueueProcessor {
    }

    fn wait_for_events(&self) {
-
        sleep(Duration::from_millis(WAIT_FOR_EVENTS_DURATION));
+
        sleep(WAIT_FOR_EVENTS_DURATION);
    }
}