Skip to content

Commit ef04e51

Browse files
authored
bridge: More config cleanup (#1330)
Follow-up to #1327 / further cleanup before adding kafka because the things were still plenty confusing.
2 parents 333f591 + 61c22cd commit ef04e51

File tree

14 files changed

+155
-164
lines changed

14 files changed

+155
-164
lines changed

bridge/svix-bridge-plugin-queue/src/config.rs

Lines changed: 48 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -12,45 +12,38 @@ pub use crate::{
1212
sqs::{SqsInputOpts, SqsOutputOpts},
1313
};
1414

15-
#[derive(Deserialize)]
16-
pub struct QueueSenderConfig {
17-
pub name: String,
18-
pub input: SenderInputOpts,
19-
#[serde(default)]
20-
pub transformation: Option<TransformationConfig>,
21-
pub output: SenderOutputOpts,
22-
}
23-
24-
impl QueueSenderConfig {
25-
pub fn into_sender_input(self) -> Result<Box<dyn SenderInput>, &'static str> {
26-
// FIXME: see if this check is still needed. String transforms worked for the omniqueue redis receiver, I think?
27-
if matches!(self.input, SenderInputOpts::Redis(_))
28-
&& self
29-
.transformation
30-
.as_ref()
31-
.map(|t| t.format() != TransformerInputFormat::Json)
32-
.unwrap_or_default()
33-
{
34-
return Err("redis only supports json formatted transformations");
35-
}
36-
37-
Ok(Box::new(QueueSender::new(
38-
self.name,
39-
self.input,
40-
self.transformation,
41-
self.output,
42-
)))
15+
pub fn into_sender_input(
16+
name: String,
17+
input_opts: QueueInputOpts,
18+
transformation: Option<TransformationConfig>,
19+
output: SenderOutputOpts,
20+
) -> Result<Box<dyn SenderInput>, &'static str> {
21+
// FIXME: see if this check is still needed. String transforms worked for the omniqueue redis receiver, I think?
22+
if matches!(input_opts, QueueInputOpts::Redis(_))
23+
&& transformation
24+
.as_ref()
25+
.map(|t| t.format() != TransformerInputFormat::Json)
26+
.unwrap_or_default()
27+
{
28+
return Err("redis only supports json formatted transformations");
4329
}
30+
31+
Ok(Box::new(QueueSender::new(
32+
name,
33+
input_opts,
34+
transformation,
35+
output,
36+
)))
4437
}
4538

4639
pub async fn into_receiver_output(
4740
name: String,
48-
opts: ReceiverOutputOpts,
41+
opts: QueueOutputOpts,
4942
// Annoying to have to pass this, but certain backends (redis) only work with certain transformations (json).
5043
transformation: Option<&TransformationConfig>,
5144
) -> Result<Box<dyn ReceiverOutput>, crate::Error> {
5245
// FIXME: see if this check is still needed. String transforms worked for the omniqueue redis receiver, I think?
53-
if matches!(opts, ReceiverOutputOpts::Redis(_))
46+
if matches!(opts, QueueOutputOpts::Redis(_))
5447
&& transformation
5548
.as_ref()
5649
.map(|t| t.format() != TransformerInputFormat::Json)
@@ -68,7 +61,7 @@ pub async fn into_receiver_output(
6861
// TODO: feature flag the variants, thread the features down through to generic-queue
6962
#[derive(Debug, Deserialize)]
7063
#[serde(tag = "type", rename_all = "lowercase")]
71-
pub enum SenderInputOpts {
64+
pub enum QueueInputOpts {
7265
#[serde(rename = "gcp-pubsub")]
7366
GCPPubSub(GCPPubSubInputOpts),
7467
RabbitMQ(RabbitMqInputOpts),
@@ -78,7 +71,7 @@ pub enum SenderInputOpts {
7871

7972
#[derive(Clone, Debug, Deserialize)]
8073
#[serde(tag = "type", rename_all = "lowercase")]
81-
pub enum ReceiverOutputOpts {
74+
pub enum QueueOutputOpts {
8275
#[serde(rename = "gcp-pubsub")]
8376
GCPPubSub(GCPPubSubOutputOpts),
8477
RabbitMQ(RabbitMqOutputOpts),
@@ -92,51 +85,50 @@ mod tests {
9285
SenderOutputOpts, SvixSenderOutputOpts, TransformationConfig, TransformerInputFormat,
9386
};
9487

95-
use super::{into_receiver_output, QueueSenderConfig};
88+
use super::{into_receiver_output, into_sender_input};
9689
use crate::{
97-
config::{ReceiverOutputOpts, SenderInputOpts},
90+
config::{QueueInputOpts, QueueOutputOpts},
9891
redis::{RedisInputOpts, RedisOutputOpts},
9992
};
10093

10194
// FIXME: can't support raw payload access for redis because it requires JSON internally.
10295
// Revisit after `omniqueue` adoption.
10396
#[test]
10497
fn redis_sender_with_string_transformation_is_err() {
105-
let cfg = QueueSenderConfig {
106-
name: "redis-with-string-transformation".to_string(),
107-
input: SenderInputOpts::Redis(RedisInputOpts {
108-
dsn: "".to_string(),
109-
max_connections: 0,
110-
reinsert_on_nack: false,
111-
queue_key: "".to_string(),
112-
delayed_queue_key: None,
113-
consumer_group: "".to_string(),
114-
consumer_name: "".to_string(),
115-
ack_deadline_ms: 2_000,
116-
}),
117-
transformation: Some(TransformationConfig::Explicit {
98+
let input_opts = QueueInputOpts::Redis(RedisInputOpts {
99+
dsn: "".to_string(),
100+
max_connections: 0,
101+
reinsert_on_nack: false,
102+
queue_key: "".to_string(),
103+
delayed_queue_key: None,
104+
consumer_group: "".to_string(),
105+
consumer_name: "".to_string(),
106+
ack_deadline_ms: 2_000,
107+
});
108+
109+
let err = into_sender_input(
110+
"redis-with-string-transformation".to_owned(),
111+
input_opts,
112+
Some(TransformationConfig::Explicit {
118113
format: TransformerInputFormat::String,
119114
src: String::new(),
120115
}),
121-
output: SenderOutputOpts::Svix(SvixSenderOutputOpts {
116+
SenderOutputOpts::Svix(SvixSenderOutputOpts {
122117
token: "".to_string(),
123118
options: None,
124119
}),
125-
};
126-
127-
assert_eq!(
128-
cfg.into_sender_input()
129-
.err()
130-
.expect("invalid config didn't result in error"),
131-
"redis only supports json formatted transformations"
132120
)
121+
.err()
122+
.expect("invalid config didn't result in error");
123+
124+
assert_eq!(err, "redis only supports json formatted transformations")
133125
}
134126

135127
// FIXME: can't support raw payload access for redis because it requires JSON internally.
136128
// Revisit after `omniqueue` adoption.
137129
#[tokio::test]
138130
async fn test_redis_receiver_string_transform_is_err() {
139-
let redis_out = ReceiverOutputOpts::Redis(RedisOutputOpts {
131+
let redis_out = QueueOutputOpts::Redis(RedisOutputOpts {
140132
dsn: "".to_string(),
141133
max_connections: 0,
142134
queue_key: "".to_string(),

bridge/svix-bridge-plugin-queue/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ mod redis;
1818
pub mod sender_input;
1919
mod sqs;
2020

21-
use error::Error;
21+
pub use self::config::{into_receiver_output, into_sender_input};
22+
use self::error::Error;
2223

2324
/// Newtype for [`omniqueue::queue::Delivery`].
2425
///

bridge/svix-bridge-plugin-queue/src/receiver_output/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::sync::Arc;
33
use omniqueue::DynProducer;
44
use svix_bridge_types::{async_trait, ForwardRequest, ReceiverOutput};
55

6-
use crate::{config::ReceiverOutputOpts, error::Result};
6+
use crate::{config::QueueOutputOpts, error::Result};
77

88
#[derive(Clone)]
99
pub struct QueueForwarder {
@@ -16,13 +16,13 @@ pub struct QueueForwarder {
1616
impl QueueForwarder {
1717
pub async fn from_receiver_output_opts(
1818
name: String,
19-
opts: ReceiverOutputOpts,
19+
opts: QueueOutputOpts,
2020
) -> Result<QueueForwarder> {
2121
let sender = match opts {
22-
ReceiverOutputOpts::GCPPubSub(cfg) => crate::gcp_pubsub::producer(&cfg).await?,
23-
ReceiverOutputOpts::RabbitMQ(cfg) => crate::rabbitmq::producer(&cfg).await?,
24-
ReceiverOutputOpts::Redis(cfg) => crate::redis::producer(&cfg).await?,
25-
ReceiverOutputOpts::SQS(cfg) => crate::sqs::producer(&cfg).await?,
22+
QueueOutputOpts::GCPPubSub(cfg) => crate::gcp_pubsub::producer(&cfg).await?,
23+
QueueOutputOpts::RabbitMQ(cfg) => crate::rabbitmq::producer(&cfg).await?,
24+
QueueOutputOpts::Redis(cfg) => crate::redis::producer(&cfg).await?,
25+
QueueOutputOpts::SQS(cfg) => crate::sqs::producer(&cfg).await?,
2626
};
2727
Ok(QueueForwarder {
2828
name,

bridge/svix-bridge-plugin-queue/src/sender_input/mod.rs

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@ use svix_bridge_types::{
44
TransformerTx,
55
};
66

7-
use crate::{
8-
config::SenderInputOpts, error::Error, gcp_pubsub, rabbitmq, run_inner, sqs, Consumer,
9-
};
7+
use crate::{config::QueueInputOpts, error::Error, gcp_pubsub, rabbitmq, run_inner, sqs, Consumer};
108

119
pub struct QueueSender {
1210
name: String,
1311
source: String,
1412
system: String,
15-
input_opts: SenderInputOpts,
13+
input_opts: QueueInputOpts,
1614
transformation: Option<TransformationConfig>,
1715
transformer_tx: Option<TransformerTx>,
1816
svix_client: Svix,
@@ -24,28 +22,28 @@ impl std::fmt::Debug for QueueSender {
2422
}
2523
}
2624

27-
fn system_name(opts: &SenderInputOpts) -> &'static str {
25+
fn system_name(opts: &QueueInputOpts) -> &'static str {
2826
match opts {
29-
SenderInputOpts::GCPPubSub(_) => "gcp-pubsub",
30-
SenderInputOpts::RabbitMQ(_) => "rabbitmq",
31-
SenderInputOpts::Redis(_) => "redis",
32-
SenderInputOpts::SQS(_) => "sqs",
27+
QueueInputOpts::GCPPubSub(_) => "gcp-pubsub",
28+
QueueInputOpts::RabbitMQ(_) => "rabbitmq",
29+
QueueInputOpts::Redis(_) => "redis",
30+
QueueInputOpts::SQS(_) => "sqs",
3331
}
3432
}
3533

36-
fn source_name(opts: &SenderInputOpts) -> &str {
34+
fn source_name(opts: &QueueInputOpts) -> &str {
3735
match opts {
38-
SenderInputOpts::GCPPubSub(opts) => &opts.subscription_id,
39-
SenderInputOpts::RabbitMQ(opts) => &opts.queue_name,
40-
SenderInputOpts::Redis(opts) => &opts.queue_key,
41-
SenderInputOpts::SQS(opts) => &opts.queue_dsn,
36+
QueueInputOpts::GCPPubSub(opts) => &opts.subscription_id,
37+
QueueInputOpts::RabbitMQ(opts) => &opts.queue_name,
38+
QueueInputOpts::Redis(opts) => &opts.queue_key,
39+
QueueInputOpts::SQS(opts) => &opts.queue_dsn,
4240
}
4341
}
4442

4543
impl QueueSender {
4644
pub fn new(
4745
name: String,
48-
input: SenderInputOpts,
46+
input: QueueInputOpts,
4947
transformation: Option<TransformationConfig>,
5048
output: SenderOutputOpts,
5149
) -> Self {
@@ -89,10 +87,10 @@ impl Consumer for QueueSender {
8987

9088
async fn consumer(&self) -> std::io::Result<DynConsumer> {
9189
Ok(match &self.input_opts {
92-
SenderInputOpts::GCPPubSub(cfg) => gcp_pubsub::consumer(cfg).await,
93-
SenderInputOpts::RabbitMQ(cfg) => rabbitmq::consumer(cfg).await,
94-
SenderInputOpts::Redis(cfg) => crate::redis::consumer(cfg).await,
95-
SenderInputOpts::SQS(cfg) => sqs::consumer(cfg).await,
90+
QueueInputOpts::GCPPubSub(cfg) => gcp_pubsub::consumer(cfg).await,
91+
QueueInputOpts::RabbitMQ(cfg) => rabbitmq::consumer(cfg).await,
92+
QueueInputOpts::Redis(cfg) => crate::redis::consumer(cfg).await,
93+
QueueInputOpts::SQS(cfg) => sqs::consumer(cfg).await,
9694
}
9795
.map_err(Error::from)?)
9896
}

bridge/svix-bridge-plugin-queue/tests/it/gcp_pubsub_consumer.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use google_cloud_pubsub::{
1313
};
1414
use serde_json::json;
1515
use svix_bridge_plugin_queue::{
16-
config::{GCPPubSubInputOpts, SenderInputOpts},
16+
config::{GCPPubSubInputOpts, QueueInputOpts},
1717
sender_input::QueueSender,
1818
};
1919
use svix_bridge_types::{
@@ -35,7 +35,7 @@ fn get_test_plugin(
3535
) -> QueueSender {
3636
QueueSender::new(
3737
"test".into(),
38-
SenderInputOpts::GCPPubSub(GCPPubSubInputOpts {
38+
QueueInputOpts::GCPPubSub(GCPPubSubInputOpts {
3939
subscription_id,
4040
credentials_file: None,
4141
}),

bridge/svix-bridge-plugin-queue/tests/it/rabbitmq_consumer.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use lapin::{
1010
};
1111
use serde_json::json;
1212
use svix_bridge_plugin_queue::{
13-
config::{RabbitMqInputOpts, SenderInputOpts},
13+
config::{QueueInputOpts, RabbitMqInputOpts},
1414
sender_input::QueueSender,
1515
};
1616
use svix_bridge_types::{
@@ -31,7 +31,7 @@ fn get_test_plugin(
3131
) -> QueueSender {
3232
QueueSender::new(
3333
"test".into(),
34-
SenderInputOpts::RabbitMQ(RabbitMqInputOpts {
34+
QueueInputOpts::RabbitMQ(RabbitMqInputOpts {
3535
uri: mq_uri.to_string(),
3636
queue_name: queue_name.to_string(),
3737
consumer_tag: None,

bridge/svix-bridge-plugin-queue/tests/it/redis_stream_consumer.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::time::Duration;
66
use redis::{AsyncCommands, Client};
77
use serde_json::json;
88
use svix_bridge_plugin_queue::{
9-
config::{RedisInputOpts, SenderInputOpts},
9+
config::{QueueInputOpts, RedisInputOpts},
1010
sender_input::QueueSender,
1111
};
1212
use svix_bridge_types::{
@@ -26,7 +26,7 @@ fn get_test_plugin(
2626
) -> QueueSender {
2727
QueueSender::new(
2828
"test".into(),
29-
SenderInputOpts::Redis(RedisInputOpts {
29+
QueueInputOpts::Redis(RedisInputOpts {
3030
dsn: "redis://localhost/".to_owned(),
3131
max_connections: 8,
3232
reinsert_on_nack: false,

bridge/svix-bridge-plugin-queue/tests/it/sqs_consumer.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::time::Duration;
88
use aws_sdk_sqs::Client;
99
use serde_json::json;
1010
use svix_bridge_plugin_queue::{
11-
config::{SenderInputOpts, SqsInputOpts},
11+
config::{QueueInputOpts, SqsInputOpts},
1212
sender_input::QueueSender,
1313
};
1414
use svix_bridge_types::{
@@ -35,7 +35,7 @@ fn get_test_plugin(
3535
) -> QueueSender {
3636
QueueSender::new(
3737
"test".into(),
38-
SenderInputOpts::SQS(SqsInputOpts {
38+
QueueInputOpts::SQS(SqsInputOpts {
3939
queue_dsn,
4040
override_endpoint: true,
4141
}),

bridge/svix-bridge/Cargo.toml

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ serde = { version = "1", features = ["derive"] }
1919
serde_json = "1"
2020
serde_yaml = "0.9"
2121
svix-ksuid = "0.7.0"
22-
svix-bridge-plugin-queue = { optional=true, path = "../svix-bridge-plugin-queue" }
22+
svix-bridge-plugin-queue = { path = "../svix-bridge-plugin-queue" }
2323
svix-bridge-types = { path = "../svix-bridge-types" }
2424
tokio = { version = "1", features = ["full"] }
2525
tracing = "0.1"
@@ -41,11 +41,5 @@ chrono = "0.4"
4141
tower = "0.4"
4242

4343
[features]
44-
default = ["gcp-pubsub", "rabbitmq", "redis", "sqs", "jemalloc"]
45-
46-
gcp-pubsub = ["generic-queue"]
47-
generic-queue = ["dep:svix-bridge-plugin-queue"]
48-
rabbitmq = ["generic-queue"]
49-
redis = ["generic-queue"]
50-
sqs = ["generic-queue"]
44+
default = ["jemalloc"]
5145
jemalloc = ["tikv-jemallocator", "tikv-jemalloc-ctl"]

0 commit comments

Comments
 (0)