fix: per-handler claim lease in ledger, nats max_deliver, pin handler names

This commit is contained in:
2026-10-26 00:24:56 +01:00
parent 5681d0c034
commit 53892847f2
14 changed files with 489 additions and 118 deletions

View File

@@ -11,10 +11,15 @@ pub struct NatsConfig {
pub subject_prefix: String,
pub stream_name: String,
pub consumer_name: String,
/// JetStream redelivery bound, same as the DB queue's attempt limit.
pub max_deliver: i64,
}
impl NatsConfig {
pub fn from_config(config: &::config::NatsConfig) -> anyhow::Result<Self> {
pub fn from_config(
config: &::config::NatsConfig,
events: &::config::EventsConfig,
) -> anyhow::Result<Self> {
let url = config
.url
.clone()
@@ -32,6 +37,7 @@ impl NatsConfig {
subject_prefix: config.subject_prefix.clone(),
stream_name: config.stream_name.clone(),
consumer_name: config.consumer_name.clone(),
max_deliver: i64::from(events.queue_max_attempts),
})
}
}

View File

@@ -108,8 +108,9 @@ fn consume_from_rx(rx: EnvelopeRx) -> BoxStream<'static, Result<EventEnvelope, D
/// How long JetStream remembers `Nats-Msg-Id`s. A row re-relayed within this
/// window (crash between publish and mark-done, or a lease reclaim) is dropped
/// by the broker; beyond it the consumer ledger is the backstop. Set
/// explicitly so it does not silently follow the server default.
/// by the broker; a duplicate arriving later is caught by the processed-event
/// ledger, which keeps its rows for 30 days. Set explicitly so it does not
/// silently follow the server default.
const DEDUP_WINDOW: std::time::Duration = std::time::Duration::from_secs(60 * 60);
// ── JetStream consumer ────────────────────────────────────────────────────────
@@ -140,6 +141,7 @@ impl NatsJetStreamConsumer {
durable_name: Some(cfg.consumer_name.clone()),
filter_subject: subject_filter,
ack_wait: std::time::Duration::from_secs(600),
max_deliver: cfg.max_deliver,
..Default::default()
},
)

View File

@@ -10,15 +10,24 @@ fn section(url: Option<&str>, mode: &str) -> config::NatsConfig {
#[test]
fn errors_without_nats_url() {
assert!(NatsConfig::from_config(&config::NatsConfig::default()).is_err());
assert!(
NatsConfig::from_config(
&config::NatsConfig::default(),
&config::EventsConfig::default()
)
.is_err()
);
}
#[test]
fn defaults_with_only_url() {
let cfg = NatsConfig::from_config(&config::NatsConfig {
url: Some("nats://localhost:4222".into()),
..Default::default()
})
let cfg = NatsConfig::from_config(
&config::NatsConfig {
url: Some("nats://localhost:4222".into()),
..Default::default()
},
&config::EventsConfig::default(),
)
.unwrap();
assert_eq!(cfg.url, "nats://localhost:4222");
assert_eq!(cfg.mode, NatsMode::JetStream);
@@ -29,12 +38,33 @@ fn defaults_with_only_url() {
#[test]
fn core_mode_parsed() {
let cfg = NatsConfig::from_config(&section(Some("nats://test:4222"), "core")).unwrap();
let cfg = NatsConfig::from_config(
&section(Some("nats://test:4222"), "core"),
&config::EventsConfig::default(),
)
.unwrap();
assert_eq!(cfg.url, "nats://test:4222");
assert_eq!(cfg.mode, NatsMode::Core);
}
#[test]
fn invalid_mode_errors() {
assert!(NatsConfig::from_config(&section(Some("nats://test:4222"), "kafka")).is_err());
assert!(
NatsConfig::from_config(
&section(Some("nats://test:4222"), "kafka"),
&config::EventsConfig::default()
)
.is_err()
);
}
#[test]
fn max_deliver_follows_the_queue_attempt_limit() {
let events = config::EventsConfig {
queue_max_attempts: 9,
..Default::default()
};
let cfg =
NatsConfig::from_config(&section(Some("nats://test:4222"), "jetstream"), &events).unwrap();
assert_eq!(cfg.max_deliver, 9);
}