ruststream-sqs-sns 0.6.1

Amazon SQS broker implementation for the RustStream messaging framework, with SNS fan-out publishing.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
//! The broker ladder: [`SqsBroker`] -> [`ConnectedSqsBroker`].
//!
//! Construction is synchronous and I/O-free; credential resolution (env, profile, IMDS, SSO)
//! happens in the consuming [`Broker::connect`], and the connected form holds the live SDK
//! clients directly. One shared cell remains so publishers can be handed out while the
//! application is still being assembled, before `connect` runs.

use std::collections::HashMap;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::time::Duration;

use aws_config::{BehaviorVersion, Region, SdkConfig};
use aws_sdk_sqs::types::QueueAttributeName;
use ruststream::{Broker, ConnectedBroker, DefaultPublish, DescribeServer, ServerSpec, Subscribe};
use tokio::sync::{Mutex, OnceCell};

use crate::error::{SqsError, sdk_err};
use crate::publisher::{SnsPublisher, SqsPublish, SqsPublisher};
use crate::queue::SqsQueue;
use crate::subscriber::SqsSubscriber;

/// The live client state shared by the connected form and every handle derived from it.
///
/// Why runtime checks exist here at all: the SDK clients have no shutdown and keep working
/// forever, and publishers may be handed out before `connect` and outlive `shutdown`
/// (aliasing) - so the dead-connection path must be an explicit runtime flag, or a stale
/// handle would silently succeed against a "closed" broker.
pub(crate) struct Core {
    pub(crate) sqs: aws_sdk_sqs::Client,
    pub(crate) sns: aws_sdk_sns::Client,
    pub(crate) endpoint: Option<String>,
    pub(crate) closed: AtomicBool,
    /// Queue-name -> URL cache shared by publishers and subscriptions.
    pub(crate) queue_urls: Mutex<HashMap<String, String>>,
    /// Topic-name -> ARN cache for the SNS publisher.
    pub(crate) topic_arns: Mutex<HashMap<String, String>>,
}

impl Core {
    pub(crate) fn ensure_open(&self) -> Result<(), SqsError> {
        if self.closed.load(Ordering::Acquire) {
            return Err(SqsError::NotConnected);
        }
        Ok(())
    }

    /// Resolves a queue name to its URL through the shared cache; URLs pass through. When a
    /// custom endpoint is configured, the returned URL's authority is rewritten onto it, so
    /// the adapter is immune to the host-rewriting strategies local stacks use.
    // The cache guard intentionally spans the resolve so two callers cannot race a double
    // lookup for the same name.
    #[allow(clippy::significant_drop_tightening)]
    pub(crate) async fn queue_url(&self, queue: &str) -> Result<String, SqsError> {
        if queue.starts_with("http://") || queue.starts_with("https://") {
            return Ok(queue.to_owned());
        }
        let mut cache = self.queue_urls.lock().await;
        if let Some(url) = cache.get(queue) {
            return Ok(url.clone());
        }
        let resolved = self
            .sqs
            .get_queue_url()
            .queue_name(queue_name(queue))
            .send()
            .await
            .map_err(|e| SqsError::Queue {
                name: queue.to_owned(),
                source: sdk_err(&e),
            })?
            .queue_url()
            .ok_or_else(|| SqsError::Queue {
                name: queue.to_owned(),
                source: Box::from("GetQueueUrl returned no URL"),
            })?
            .to_owned();
        let url = self.rebase_url(resolved);
        cache.insert(queue.to_owned(), url.clone());
        Ok(url)
    }

    /// Resolves a topic name to its ARN through the shared cache; ARNs pass through.
    /// `CreateTopic` is documented idempotent: an existing topic's ARN is returned as-is.
    // The cache guard intentionally spans the resolve so two callers cannot race a double
    // create for the same name.
    #[allow(clippy::significant_drop_tightening)]
    pub(crate) async fn topic_arn(&self, topic: &str) -> Result<String, SqsError> {
        if topic.starts_with("arn:") {
            return Ok(topic.to_owned());
        }
        let mut cache = self.topic_arns.lock().await;
        if let Some(arn) = cache.get(topic) {
            return Ok(arn.clone());
        }
        let created = self
            .sns
            .create_topic()
            .name(topic)
            .send()
            .await
            .map_err(|e| SqsError::Admin {
                name: topic.to_owned(),
                source: sdk_err(&e),
            })?;
        let arn = created
            .topic_arn()
            .ok_or_else(|| SqsError::Admin {
                name: topic.to_owned(),
                source: Box::from("CreateTopic returned no ARN"),
            })?
            .to_owned();
        cache.insert(topic.to_owned(), arn.clone());
        Ok(arn)
    }

    pub(crate) fn rebase_url(&self, url: String) -> String {
        let Some(endpoint) = &self.endpoint else {
            return url;
        };
        // Keep the path (account/queue), replace scheme+authority with the configured
        // endpoint.
        url.find("//")
            .and_then(|scheme_end| url[scheme_end + 2..].find('/').map(|p| scheme_end + 2 + p))
            .map_or_else(
                || url.clone(),
                |path_start| format!("{}{}", endpoint.trim_end_matches('/'), &url[path_start..]),
            )
    }
}

impl std::fmt::Debug for Core {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Core")
            .field("endpoint", &self.endpoint)
            .field("closed", &self.closed.load(Ordering::Relaxed))
            .finish_non_exhaustive()
    }
}

pub(crate) type CoreCell = Arc<OnceCell<Arc<Core>>>;

/// Maps a logical destination name onto a valid SQS queue name: characters outside
/// `[A-Za-z0-9_-]` become `-` (SQS forbids them), and a `.fifo` suffix survives. Subscribers
/// and publishers share this mapping, so dotted framework names stay routable.
pub(crate) fn queue_name(logical: &str) -> String {
    let (stem, fifo) = logical
        .strip_suffix(".fifo")
        .map_or((logical, ""), |stem| (stem, ".fifo"));
    let mapped: String = stem
        .chars()
        .map(|c| {
            if c.is_ascii_alphanumeric() || c == '-' || c == '_' {
                c
            } else {
                '-'
            }
        })
        .collect();
    format!("{mapped}{fifo}")
}

/// An Amazon SQS broker (with SNS fan-out publishing) for the `RustStream` messaging
/// framework.
///
/// `new` is synchronous and records only configuration; the runtime resolves credentials and
/// builds the clients once at startup via the consuming [`Broker::connect`]. That is what lets
/// a service compose with the synchronous `#[ruststream::app]` builder.
///
/// # Examples
///
/// ```
/// use ruststream_sqs_sns::SqsBroker;
///
/// let broker = SqsBroker::new(); // region and credentials from the environment
/// let local = SqsBroker::new()
///     .endpoint("http://localhost:4566")
///     .test_credentials()
///     .region("us-east-1");
/// # let _ = (broker, local);
/// ```
#[derive(Debug, Clone, Default)]
#[must_use]
pub struct SqsBroker {
    endpoint: Option<String>,
    region: Option<String>,
    test_credentials: bool,
    sdk_config: Option<SdkConfig>,
    cell: CoreCell,
}

impl SqsBroker {
    /// Records configuration only; region and credentials resolve from the environment on
    /// `connect`. No I/O.
    pub fn new() -> Self {
        Self::default()
    }

    /// Uses an already built AWS config instead of resolving one from the environment.
    pub fn from_config(config: SdkConfig) -> Self {
        Self {
            sdk_config: Some(config),
            ..Self::default()
        }
    }

    /// Overrides the service endpoint (a local stack for development). Queue URLs returned by
    /// the service are rebased onto this endpoint.
    pub fn endpoint(mut self, endpoint: impl Into<String>) -> Self {
        self.endpoint = Some(endpoint.into());
        self
    }

    /// Overrides the region.
    pub fn region(mut self, region: impl Into<String>) -> Self {
        self.region = Some(region.into());
        self
    }

    /// Uses dummy static credentials, for local stacks that require credentials to be present
    /// but ignore their values.
    pub fn test_credentials(mut self) -> Self {
        self.test_credentials = true;
        self
    }

    /// A publisher sending directly to SQS queues, sharing this broker's connection cell;
    /// buildable before `connect`.
    #[must_use]
    pub fn publisher(&self) -> SqsPublisher {
        SqsPublisher::new(Arc::clone(&self.cell))
    }
}

impl Broker for SqsBroker {
    type Error = SqsError;
    type Connected = ConnectedSqsBroker;

    async fn connect(self) -> Result<Self::Connected, Self::Error> {
        let core = self
            .cell
            .get_or_try_init(async || {
                let config = if let Some(config) = self.sdk_config.clone() {
                    config
                } else {
                    // BehaviorVersion::latest(): every pinned version eventually deprecates
                    // (which -D warnings turns into a build failure); the consumer who needs a
                    // frozen behaviour passes a prebuilt config via from_config.
                    let mut loader = aws_config::defaults(BehaviorVersion::latest());
                    if let Some(endpoint) = &self.endpoint {
                        loader = loader.endpoint_url(endpoint.clone());
                    }
                    if let Some(region) = &self.region {
                        loader = loader.region(Region::new(region.clone()));
                    }
                    if self.test_credentials {
                        loader = loader.test_credentials();
                    }
                    // A long poll waits up to 20s; the per-attempt timeout must exceed it or
                    // every receive dies (the SDK maintainers' recommended pairing).
                    loader = loader.timeout_config(
                        aws_config::timeout::TimeoutConfig::builder()
                            .operation_attempt_timeout(Duration::from_secs(25))
                            .build(),
                    );
                    loader.load().await
                };
                let sqs = aws_sdk_sqs::Client::new(&config);
                let sns = aws_sdk_sns::Client::new(&config);
                Ok::<_, SqsError>(Arc::new(Core {
                    sqs,
                    sns,
                    endpoint: self
                        .endpoint
                        .clone()
                        .or_else(|| config.endpoint_url().map(str::to_owned)),
                    closed: AtomicBool::new(false),
                    queue_urls: Mutex::new(HashMap::new()),
                    topic_arns: Mutex::new(HashMap::new()),
                }))
            })
            .await?
            .clone();
        Ok(ConnectedSqsBroker {
            core,
            cell: self.cell,
        })
    }
}

impl DescribeServer for SqsBroker {
    fn describe_server(&self) -> ServerSpec {
        let host = self
            .endpoint
            .clone()
            .unwrap_or_else(|| "sqs.amazonaws.com".to_owned());
        ServerSpec::new(host, "sqs")
    }
}

/// The typed witness that `connect` succeeded: holds the live SDK clients directly.
#[derive(Debug)]
pub struct ConnectedSqsBroker {
    pub(crate) core: Arc<Core>,
    // Keeps the cell of publishers handed out before connect alive and filled.
    cell: CoreCell,
}

impl ConnectedSqsBroker {
    /// A publisher from the connected form. It rides the same cell-backed publisher type as
    /// the early path; by now `connect` has filled the cell, so it resolves immediately.
    #[must_use]
    pub fn publisher(&self) -> SqsPublisher {
        SqsPublisher::new(Arc::clone(&self.cell))
    }

    /// An SNS fan-out publisher from the connected form.
    #[must_use]
    pub fn sns_publisher(&self) -> SnsPublisher {
        SnsPublisher::new(Arc::clone(&self.cell))
    }

    /// Subscribes `queue` to `topic` (both names or ARN/URL) with raw message delivery, so
    /// payloads and headers arrive unwrapped as plain SQS messages.
    ///
    /// # Errors
    ///
    /// Returns [`SqsError`] when the topic or queue cannot be resolved or the subscription
    /// call fails.
    pub async fn subscribe_queue_to_topic(&self, topic: &str, queue: &str) -> Result<(), SqsError> {
        self.core.ensure_open()?;
        let topic_arn = self.core.topic_arn(topic).await?;
        let queue_url = self.core.queue_url(queue).await?;
        let attributes = self
            .core
            .sqs
            .get_queue_attributes()
            .queue_url(&queue_url)
            .attribute_names(QueueAttributeName::QueueArn)
            .send()
            .await
            .map_err(|e| SqsError::Queue {
                name: queue.to_owned(),
                source: sdk_err(&e),
            })?;
        let queue_arn = attributes
            .attributes()
            .and_then(|map| map.get(&QueueAttributeName::QueueArn))
            .ok_or_else(|| SqsError::Queue {
                name: queue.to_owned(),
                source: Box::from("GetQueueAttributes returned no QueueArn"),
            })?
            .clone();
        self.core
            .sns
            .subscribe()
            .topic_arn(&topic_arn)
            .protocol("sqs")
            .endpoint(queue_arn)
            .attributes("RawMessageDelivery", "true")
            .send()
            .await
            .map(|_| ())
            .map_err(|e| SqsError::Admin {
                name: topic.to_owned(),
                source: sdk_err(&e),
            })
    }

    /// Opens the subscription described by `queue`.
    ///
    /// # Errors
    ///
    /// Returns [`SqsError`] when the descriptor is invalid, the queue cannot be resolved (or
    /// created, when opted in), or the broker is shut down.
    pub async fn subscribe_queue(&self, queue: SqsQueue) -> Result<SqsSubscriber, SqsError> {
        queue.validate()?;
        self.core.ensure_open()?;

        let url = if queue.create_value() {
            self.ensure_queue(queue.queue()).await?
        } else {
            self.core.queue_url(queue.queue()).await?
        };
        Ok(SqsSubscriber::open(&self.core, url, &queue))
    }

    /// Resolves the queue, creating it when missing. A `.fifo` name creates a FIFO queue with
    /// content-based deduplication.
    async fn ensure_queue(&self, queue: &str) -> Result<String, SqsError> {
        if let Ok(url) = self.core.queue_url(queue).await {
            return Ok(url);
        }
        let mut create = self.core.sqs.create_queue().queue_name(queue_name(queue));
        if queue.to_ascii_lowercase().ends_with(".fifo") {
            create = create
                .attributes(QueueAttributeName::FifoQueue, "true")
                .attributes(QueueAttributeName::ContentBasedDeduplication, "true");
        }
        let created = create.send().await.map_err(|e| SqsError::Queue {
            name: queue.to_owned(),
            source: sdk_err(&e),
        })?;
        let url = created.queue_url().ok_or_else(|| SqsError::Queue {
            name: queue.to_owned(),
            source: Box::from("CreateQueue returned no URL"),
        })?;
        let url = self.core.rebase_url(url.to_owned());
        self.core
            .queue_urls
            .lock()
            .await
            .insert(queue.to_owned(), url.clone());
        Ok(url)
    }
}

impl ConnectedBroker for ConnectedSqsBroker {
    type Error = SqsError;
    type Closed = ();

    async fn shutdown(self) -> Result<(), Self::Error> {
        // The SDK clients have no close/flush; requests are synchronous per call, so marking
        // the shared state closed is the whole teardown. Aliased handles error afterwards.
        self.core.closed.store(true, Ordering::Release);
        Ok(())
    }
}

impl Subscribe for ConnectedSqsBroker {
    type Subscriber = SqsSubscriber;

    async fn subscribe(&self, name: &str) -> Result<Self::Subscriber, Self::Error> {
        self.subscribe_queue(SqsQueue::new(name)).await
    }
}

impl DefaultPublish for ConnectedSqsBroker {
    type Policy = SqsPublish;
}