Skip to content

Commit 62efa59

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 62efa59

16 files changed

Lines changed: 271 additions & 108 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-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 & 18 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() {
@@ -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>(
@@ -153,7 +154,7 @@ async fn handle_webhook_inner<P: JetStreamPublisher, S: ObjectStorePut, R: Reque
153154
&state.subject_prefix,
154155
"invalid_json",
155156
body,
156-
state.nats_ack_timeout,
157+
state.nats_ack_timeout.into(),
157158
)
158159
.await;
159160
return StatusCode::BAD_REQUEST.into_response();
@@ -166,7 +167,7 @@ async fn handle_webhook_inner<P: JetStreamPublisher, S: ObjectStorePut, R: Reque
166167
&state.subject_prefix,
167168
"missing_type",
168169
body,
169-
state.nats_ack_timeout,
170+
state.nats_ack_timeout.into(),
170171
)
171172
.await;
172173
return StatusCode::BAD_REQUEST.into_response();
@@ -184,7 +185,7 @@ async fn handle_webhook_inner<P: JetStreamPublisher, S: ObjectStorePut, R: Reque
184185
&state.subject_prefix,
185186
"unknown_interaction_type",
186187
body,
187-
state.nats_ack_timeout,
188+
state.nats_ack_timeout.into(),
188189
)
189190
.await;
190191
return StatusCode::BAD_REQUEST.into_response();
@@ -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);
@@ -304,8 +305,8 @@ mod tests {
304305
subject_prefix: NatsToken::new("discord").unwrap(),
305306
stream_name: NatsToken::new("DISCORD").unwrap(),
306307
stream_max_age: Duration::from_secs(3600),
307-
nats_ack_timeout: Duration::from_secs(10),
308-
nats_request_timeout: Duration::from_secs(2),
308+
nats_ack_timeout: NonZeroDuration::from_secs(10).unwrap(),
309+
nats_request_timeout: NonZeroDuration::from_secs(2).unwrap(),
309310
}
310311
}
311312

@@ -518,7 +519,7 @@ mod tests {
518519
nats.hang_next_request();
519520

520521
let mut config = test_config(vk);
521-
config.nats_request_timeout = Duration::from_millis(10);
522+
config.nats_request_timeout = NonZeroDuration::from_millis(10).unwrap();
522523

523524
let app = router(wrap_publisher(publisher.clone()), nats, vk, &config);
524525
let body = br#"{"type":4,"id":"auto-1","data":{"name":"cmd"}}"#;
@@ -741,8 +742,8 @@ mod tests {
741742
nats: AdvancedMockNatsClient::new(),
742743
public_key: vk,
743744
subject_prefix: NatsToken::new("custom").unwrap(),
744-
nats_ack_timeout: Duration::from_secs(10),
745-
nats_request_timeout: Duration::from_secs(2),
745+
nats_ack_timeout: NonZeroDuration::from_secs(10).unwrap(),
746+
nats_request_timeout: NonZeroDuration::from_secs(2).unwrap(),
746747
};
747748

748749
let app =
@@ -811,8 +812,8 @@ mod tests {
811812
nats: AdvancedMockNatsClient::new(),
812813
public_key: vk,
813814
subject_prefix: NatsToken::new("discord").unwrap(),
814-
nats_ack_timeout: Duration::from_secs(10),
815-
nats_request_timeout: Duration::from_secs(2),
815+
nats_ack_timeout: NonZeroDuration::from_secs(10).unwrap(),
816+
nats_request_timeout: NonZeroDuration::from_secs(2).unwrap(),
816817
};
817818

818819
let app =
@@ -930,8 +931,8 @@ mod tests {
930931
nats: AdvancedMockNatsClient::new(),
931932
public_key: vk,
932933
subject_prefix: NatsToken::new("discord").unwrap(),
933-
nats_ack_timeout: Duration::from_secs(10),
934-
nats_request_timeout: Duration::from_secs(2),
934+
nats_ack_timeout: NonZeroDuration::from_secs(10).unwrap(),
935+
nats_request_timeout: NonZeroDuration::from_secs(2).unwrap(),
935936
};
936937

937938
Router::new()
@@ -956,8 +957,8 @@ mod tests {
956957
nats: AdvancedMockNatsClient::new(),
957958
public_key: vk,
958959
subject_prefix: NatsToken::new("discord").unwrap(),
959-
nats_ack_timeout: Duration::from_millis(10),
960-
nats_request_timeout: Duration::from_secs(2),
960+
nats_ack_timeout: NonZeroDuration::from_millis(10).unwrap(),
961+
nats_request_timeout: NonZeroDuration::from_secs(2).unwrap(),
961962
};
962963

963964
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)