Skip to content

Commit 0f058ca

Browse files
committed
refactor: replace raw Duration with NonZeroDuration for timeout fields
Signed-off-by: Yordis Prieto <yordis.prieto@gmail.com>
1 parent f2c0815 commit 0f058ca

17 files changed

Lines changed: 261 additions & 96 deletions

File tree

rsworkspace/crates/trogon-gateway/src/config.rs

Lines changed: 74 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use trogon_source_gitlab::config::GitLabWebhookSecret;
1010
use trogon_source_linear::config::LinearWebhookSecret;
1111
use trogon_source_slack::config::SlackSigningSecret;
1212
use trogon_source_telegram::config::TelegramWebhookSecret;
13+
use trogon_std::NonZeroDuration;
1314

1415
#[derive(Debug)]
1516
pub enum ConfigError {
@@ -304,12 +305,20 @@ fn resolve_github(
304305
}
305306
};
306307

308+
let nats_ack_timeout = match NonZeroDuration::from_secs(section.nats_ack_timeout_secs) {
309+
Ok(d) => d,
310+
Err(_) => {
311+
errors.push("github: nats_ack_timeout_secs must not be zero".to_string());
312+
return None;
313+
}
314+
};
315+
307316
Some(trogon_source_github::GithubConfig {
308317
webhook_secret,
309318
subject_prefix,
310319
stream_name,
311320
stream_max_age: Duration::from_secs(section.stream_max_age_secs),
312-
nats_ack_timeout: Duration::from_secs(section.nats_ack_timeout_secs),
321+
nats_ack_timeout,
313322
})
314323
}
315324

@@ -389,13 +398,29 @@ fn resolve_discord(
389398
}
390399
};
391400

401+
let nats_ack_timeout = match NonZeroDuration::from_secs(section.nats_ack_timeout_secs) {
402+
Ok(d) => d,
403+
Err(_) => {
404+
errors.push("discord: nats_ack_timeout_secs must not be zero".to_string());
405+
return None;
406+
}
407+
};
408+
409+
let nats_request_timeout = match NonZeroDuration::from_secs(section.nats_request_timeout_secs) {
410+
Ok(d) => d,
411+
Err(_) => {
412+
errors.push("discord: nats_request_timeout_secs must not be zero".to_string());
413+
return None;
414+
}
415+
};
416+
392417
Some(trogon_source_discord::DiscordConfig {
393418
mode,
394419
subject_prefix,
395420
stream_name,
396421
stream_max_age: Duration::from_secs(section.stream_max_age_secs),
397-
nats_ack_timeout: Duration::from_secs(section.nats_ack_timeout_secs),
398-
nats_request_timeout: Duration::from_secs(section.nats_request_timeout_secs),
422+
nats_ack_timeout,
423+
nats_request_timeout,
399424
})
400425
}
401426

@@ -429,13 +454,29 @@ fn resolve_slack(
429454
}
430455
};
431456

457+
let nats_ack_timeout = match NonZeroDuration::from_secs(section.nats_ack_timeout_secs) {
458+
Ok(d) => d,
459+
Err(_) => {
460+
errors.push("slack: nats_ack_timeout_secs must not be zero".to_string());
461+
return None;
462+
}
463+
};
464+
465+
let timestamp_max_drift = match NonZeroDuration::from_secs(section.timestamp_max_drift_secs) {
466+
Ok(d) => d,
467+
Err(_) => {
468+
errors.push("slack: timestamp_max_drift_secs must not be zero".to_string());
469+
return None;
470+
}
471+
};
472+
432473
Some(trogon_source_slack::SlackConfig {
433474
signing_secret,
434475
subject_prefix,
435476
stream_name,
436477
stream_max_age: Duration::from_secs(section.stream_max_age_secs),
437-
nats_ack_timeout: Duration::from_secs(section.nats_ack_timeout_secs),
438-
timestamp_max_drift: Duration::from_secs(section.timestamp_max_drift_secs),
478+
nats_ack_timeout,
479+
timestamp_max_drift,
439480
})
440481
}
441482

@@ -469,12 +510,20 @@ fn resolve_telegram(
469510
}
470511
};
471512

513+
let nats_ack_timeout = match NonZeroDuration::from_secs(section.nats_ack_timeout_secs) {
514+
Ok(d) => d,
515+
Err(_) => {
516+
errors.push("telegram: nats_ack_timeout_secs must not be zero".to_string());
517+
return None;
518+
}
519+
};
520+
472521
Some(trogon_source_telegram::TelegramSourceConfig {
473522
webhook_secret,
474523
subject_prefix,
475524
stream_name,
476525
stream_max_age: Duration::from_secs(section.stream_max_age_secs),
477-
nats_ack_timeout: Duration::from_secs(section.nats_ack_timeout_secs),
526+
nats_ack_timeout,
478527
})
479528
}
480529

@@ -508,12 +557,20 @@ fn resolve_gitlab(
508557
}
509558
};
510559

560+
let nats_ack_timeout = match NonZeroDuration::from_secs(section.nats_ack_timeout_secs) {
561+
Ok(d) => d,
562+
Err(_) => {
563+
errors.push("gitlab: nats_ack_timeout_secs must not be zero".to_string());
564+
return None;
565+
}
566+
};
567+
511568
Some(trogon_source_gitlab::GitlabConfig {
512569
webhook_secret,
513570
subject_prefix,
514571
stream_name,
515572
stream_max_age: Duration::from_secs(section.stream_max_age_secs),
516-
nats_ack_timeout: Duration::from_secs(section.nats_ack_timeout_secs),
573+
nats_ack_timeout,
517574
})
518575
}
519576

@@ -547,13 +604,20 @@ fn resolve_linear(
547604
}
548605
};
549606

607+
let nats_ack_timeout = match NonZeroDuration::from_secs(section.nats_ack_timeout_secs) {
608+
Ok(d) => d,
609+
Err(_) => {
610+
errors.push("linear: nats_ack_timeout_secs must not be zero".to_string());
611+
return None;
612+
}
613+
};
614+
550615
Some(trogon_source_linear::LinearConfig {
551616
webhook_secret,
552617
subject_prefix,
553618
stream_name,
554619
stream_max_age: Duration::from_secs(section.stream_max_age_secs),
555-
timestamp_tolerance: (section.timestamp_tolerance_secs > 0)
556-
.then(|| Duration::from_secs(section.timestamp_tolerance_secs)),
557-
nats_ack_timeout: Duration::from_secs(section.nats_ack_timeout_secs),
620+
timestamp_tolerance: NonZeroDuration::from_secs(section.timestamp_tolerance_secs).ok(),
621+
nats_ack_timeout,
558622
})
559623
}

rsworkspace/crates/trogon-gateway/src/serve.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ where
4848

4949
acp_telemetry::init_logger(
5050
acp_telemetry::ServiceName::TrogonGateway,
51-
"source",
51+
"gateway",
5252
&env,
5353
&fs,
5454
);

rsworkspace/crates/trogon-source-discord/src/config.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::time::Duration;
33

44
use ed25519_dalek::VerifyingKey;
55
use trogon_nats::NatsToken;
6-
use trogon_std::{EmptySecret, SecretString};
6+
use trogon_std::{EmptySecret, NonZeroDuration, SecretString};
77

88
#[derive(Clone)]
99
pub struct DiscordBotToken(SecretString);
@@ -40,8 +40,8 @@ pub struct DiscordConfig {
4040
pub subject_prefix: NatsToken,
4141
pub stream_name: NatsToken,
4242
pub stream_max_age: Duration,
43-
pub nats_ack_timeout: Duration,
44-
pub nats_request_timeout: Duration,
43+
pub nats_ack_timeout: NonZeroDuration,
44+
pub nats_request_timeout: NonZeroDuration,
4545
}
4646

4747
const PRIVILEGED_INTENTS: Intents = Intents::from_bits_truncate(

rsworkspace/crates/trogon-source-discord/src/gateway_runner.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub async fn run<
2222
let bridge = GatewayBridge::new(
2323
publisher,
2424
config.subject_prefix.clone(),
25-
config.nats_ack_timeout,
25+
config.nats_ack_timeout.into(),
2626
);
2727

2828
let mut shard = Shard::new(ShardId::ONE, bot_token.to_owned(), intents);

rsworkspace/crates/trogon-source-discord/src/server.rs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use trogon_nats::jetstream::{
1919
ClaimCheckPublisher, JetStreamContext, JetStreamPublisher, ObjectStorePut, PublishOutcome,
2020
};
2121
use trogon_nats::{NatsToken, RequestClient};
22+
use trogon_std::NonZeroDuration;
2223

2324
fn outcome_to_status<E: fmt::Display>(outcome: PublishOutcome<E>) -> StatusCode {
2425
if outcome.is_ok() {
@@ -35,15 +36,15 @@ async fn publish_unroutable<P: JetStreamPublisher, S: ObjectStorePut>(
3536
subject_prefix: &str,
3637
reason: &str,
3738
body: Bytes,
38-
ack_timeout: Duration,
39+
ack_timeout: NonZeroDuration,
3940
) {
4041
let subject = format!("{subject_prefix}.unroutable");
4142
let mut headers = async_nats::HeaderMap::new();
4243
headers.insert(NATS_HEADER_REJECT_REASON, reason);
4344
headers.insert(NATS_HEADER_PAYLOAD_KIND, "unroutable");
4445

4546
let outcome = publisher
46-
.publish_event(subject, headers, body, ack_timeout)
47+
.publish_event(subject, headers, body, ack_timeout.into())
4748
.await;
4849
outcome.log_on_error("discord.unroutable");
4950
}
@@ -64,8 +65,8 @@ struct AppState<P: JetStreamPublisher, S: ObjectStorePut, R: RequestClient> {
6465
nats: R,
6566
public_key: VerifyingKey,
6667
subject_prefix: NatsToken,
67-
nats_ack_timeout: Duration,
68-
nats_request_timeout: Duration,
68+
nats_ack_timeout: NonZeroDuration,
69+
nats_request_timeout: NonZeroDuration,
6970
}
7071

7172
pub async fn provision<C: JetStreamContext>(
@@ -214,14 +215,14 @@ async fn handle_webhook_inner<P: JetStreamPublisher, S: ObjectStorePut, R: Reque
214215
subject,
215216
nats_headers,
216217
body,
217-
state.nats_request_timeout,
218+
state.nats_request_timeout.into(),
218219
)
219220
.await;
220221
}
221222

222223
let outcome = state
223224
.publisher
224-
.publish_event(subject, nats_headers, body, state.nats_ack_timeout)
225+
.publish_event(subject, nats_headers, body, state.nats_ack_timeout.into())
225226
.await;
226227

227228
let status = outcome_to_status(outcome);
@@ -272,6 +273,7 @@ fn empty_autocomplete_response() -> Response {
272273
#[cfg(test)]
273274
mod tests {
274275
use super::*;
276+
275277
use axum::body::Body;
276278
use axum::http::Request;
277279
use ed25519_dalek::{Signer, SigningKey};
@@ -304,8 +306,8 @@ mod tests {
304306
subject_prefix: NatsToken::new("discord").unwrap(),
305307
stream_name: NatsToken::new("DISCORD").unwrap(),
306308
stream_max_age: Duration::from_secs(3600),
307-
nats_ack_timeout: Duration::from_secs(10),
308-
nats_request_timeout: Duration::from_secs(2),
309+
nats_ack_timeout: NonZeroDuration::from_secs(10).unwrap(),
310+
nats_request_timeout: NonZeroDuration::from_secs(2).unwrap(),
309311
}
310312
}
311313

@@ -518,7 +520,7 @@ mod tests {
518520
nats.hang_next_request();
519521

520522
let mut config = test_config(vk);
521-
config.nats_request_timeout = Duration::from_millis(10);
523+
config.nats_request_timeout = NonZeroDuration::from_millis(10).unwrap();
522524

523525
let app = router(wrap_publisher(publisher.clone()), nats, vk, &config);
524526
let body = br#"{"type":4,"id":"auto-1","data":{"name":"cmd"}}"#;
@@ -741,8 +743,8 @@ mod tests {
741743
nats: AdvancedMockNatsClient::new(),
742744
public_key: vk,
743745
subject_prefix: NatsToken::new("custom").unwrap(),
744-
nats_ack_timeout: Duration::from_secs(10),
745-
nats_request_timeout: Duration::from_secs(2),
746+
nats_ack_timeout: NonZeroDuration::from_secs(10).unwrap(),
747+
nats_request_timeout: NonZeroDuration::from_secs(2).unwrap(),
746748
};
747749

748750
let app =
@@ -811,8 +813,8 @@ mod tests {
811813
nats: AdvancedMockNatsClient::new(),
812814
public_key: vk,
813815
subject_prefix: NatsToken::new("discord").unwrap(),
814-
nats_ack_timeout: Duration::from_secs(10),
815-
nats_request_timeout: Duration::from_secs(2),
816+
nats_ack_timeout: NonZeroDuration::from_secs(10).unwrap(),
817+
nats_request_timeout: NonZeroDuration::from_secs(2).unwrap(),
816818
};
817819

818820
let app =
@@ -930,8 +932,8 @@ mod tests {
930932
nats: AdvancedMockNatsClient::new(),
931933
public_key: vk,
932934
subject_prefix: NatsToken::new("discord").unwrap(),
933-
nats_ack_timeout: Duration::from_secs(10),
934-
nats_request_timeout: Duration::from_secs(2),
935+
nats_ack_timeout: NonZeroDuration::from_secs(10).unwrap(),
936+
nats_request_timeout: NonZeroDuration::from_secs(2).unwrap(),
935937
};
936938

937939
Router::new()
@@ -956,8 +958,8 @@ mod tests {
956958
nats: AdvancedMockNatsClient::new(),
957959
public_key: vk,
958960
subject_prefix: NatsToken::new("discord").unwrap(),
959-
nats_ack_timeout: Duration::from_millis(10),
960-
nats_request_timeout: Duration::from_secs(2),
961+
nats_ack_timeout: NonZeroDuration::from_millis(10).unwrap(),
962+
nats_request_timeout: NonZeroDuration::from_secs(2).unwrap(),
961963
};
962964

963965
Router::new()

rsworkspace/crates/trogon-source-github/src/config.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::fmt;
22
use std::time::Duration;
33

44
use trogon_nats::NatsToken;
5-
use trogon_std::{EmptySecret, SecretString};
5+
use trogon_std::{EmptySecret, NonZeroDuration, SecretString};
66

77
#[derive(Clone)]
88
pub struct GitHubWebhookSecret(SecretString);
@@ -28,5 +28,5 @@ pub struct GithubConfig {
2828
pub subject_prefix: NatsToken,
2929
pub stream_name: NatsToken,
3030
pub stream_max_age: Duration,
31-
pub nats_ack_timeout: Duration,
31+
pub nats_ack_timeout: NonZeroDuration,
3232
}

0 commit comments

Comments
 (0)