tideway 0.7.17

A batteries-included Rust web framework built on Axum for building SaaS applications quickly
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
use crate::error::Result;
use async_trait::async_trait;
use std::collections::HashSet;
use std::sync::Arc;
use tokio::sync::RwLock;

/// Trait for storing webhook processing claims to prevent duplicate work.
///
/// Recommended flow for handlers:
/// 1. Call `claim_event(event_id)` before any side effects.
/// 2. If it returns `false`, treat as duplicate and return success (idempotent skip).
/// 3. If processing fails with a retryable/transient error, call `release_claim(event_id)`.
/// 4. If processing succeeds (or permanently failed and should not retry), keep claim recorded.
///
/// Implementations should make `claim_event` atomic when possible.
#[async_trait]
pub trait IdempotencyStore: Send + Sync {
    /// Atomically claim an event for processing.
    ///
    /// Returns:
    /// - `Ok(true)` if this caller successfully claimed the event
    /// - `Ok(false)` if the event is already claimed/processed elsewhere
    ///
    /// Default implementation falls back to check-then-mark for compatibility.
    async fn claim_event(&self, event_id: &str) -> Result<bool> {
        if self.is_processed(event_id).await? {
            return Ok(false);
        }
        self.mark_processed(event_id.to_string()).await?;
        Ok(true)
    }

    /// Check whether an event is already claimed/processed.
    ///
    /// Primarily useful for diagnostics and compatibility paths.
    /// Prefer `claim_event` for the processing gate.
    async fn is_processed(&self, event_id: &str) -> Result<bool>;

    /// Mark an event as processed
    async fn mark_processed(&self, event_id: String) -> Result<()>;

    /// Release a previously claimed event so it can be retried.
    ///
    /// Default implementation is a no-op.
    async fn release_claim(&self, _event_id: &str) -> Result<()> {
        Ok(())
    }

    /// Clean up old entries (optional)
    async fn cleanup_old_entries(&self) -> Result<()> {
        Ok(())
    }
}

/// In-memory idempotency store (for development/testing)
///
/// In production, use a database-backed store or Redis
pub struct MemoryIdempotencyStore {
    processed: Arc<RwLock<HashSet<String>>>,
}

impl MemoryIdempotencyStore {
    pub fn new() -> Self {
        Self {
            processed: Arc::new(RwLock::new(HashSet::new())),
        }
    }
}

impl Default for MemoryIdempotencyStore {
    fn default() -> Self {
        Self::new()
    }
}

#[async_trait]
impl IdempotencyStore for MemoryIdempotencyStore {
    async fn claim_event(&self, event_id: &str) -> Result<bool> {
        let mut processed = self.processed.write().await;
        Ok(processed.insert(event_id.to_string()))
    }

    async fn is_processed(&self, event_id: &str) -> Result<bool> {
        let processed = self.processed.read().await;
        Ok(processed.contains(event_id))
    }

    async fn mark_processed(&self, event_id: String) -> Result<()> {
        let mut processed = self.processed.write().await;
        processed.insert(event_id);
        Ok(())
    }

    async fn release_claim(&self, event_id: &str) -> Result<()> {
        let mut processed = self.processed.write().await;
        processed.remove(event_id);
        Ok(())
    }
}

/// Database-backed idempotency store (when using SeaORM)
#[cfg(feature = "database")]
pub struct DatabaseIdempotencyStore {
    db: sea_orm::DatabaseConnection,
}

#[cfg(feature = "database")]
impl DatabaseIdempotencyStore {
    pub fn new(db: sea_orm::DatabaseConnection) -> Self {
        Self { db }
    }
}

#[cfg(feature = "database")]
#[async_trait]
impl IdempotencyStore for DatabaseIdempotencyStore {
    async fn claim_event(&self, event_id: &str) -> Result<bool> {
        use sea_orm::sea_query::OnConflict;
        use sea_orm::{EntityTrait, Set};

        let model = db_entity::ActiveModel {
            event_id: Set(event_id.to_string()),
            processed_at: Set(chrono::Utc::now().into()),
        };

        let result = db_entity::Entity::insert(model)
            .on_conflict(
                OnConflict::column(db_entity::Column::EventId)
                    .do_nothing()
                    .to_owned(),
            )
            .exec(&self.db)
            .await;

        match result {
            Ok(_) => Ok(true),
            Err(sea_orm::DbErr::RecordNotInserted) => Ok(false),
            Err(e) => Err(crate::error::TidewayError::internal(format!(
                "Failed to claim webhook event for processing: {}",
                e
            ))),
        }
    }

    async fn is_processed(&self, event_id: &str) -> Result<bool> {
        use sea_orm::EntityTrait;

        let model = db_entity::Entity::find_by_id(event_id.to_string())
            .one(&self.db)
            .await
            .map_err(|e| {
                crate::error::TidewayError::internal(format!(
                    "Failed to check webhook idempotency state: {}",
                    e
                ))
            })?;

        Ok(model.is_some())
    }

    async fn mark_processed(&self, event_id: String) -> Result<()> {
        use sea_orm::sea_query::OnConflict;
        use sea_orm::{EntityTrait, Set};

        let model = db_entity::ActiveModel {
            event_id: Set(event_id),
            processed_at: Set(chrono::Utc::now().into()),
        };

        let result = db_entity::Entity::insert(model)
            .on_conflict(
                OnConflict::column(db_entity::Column::EventId)
                    .do_nothing()
                    .to_owned(),
            )
            .exec(&self.db)
            .await;

        match result {
            Ok(_) => Ok(()),
            // Conflict + DO NOTHING path: already marked as processed.
            Err(sea_orm::DbErr::RecordNotInserted) => Ok(()),
            Err(e) => Err(crate::error::TidewayError::internal(format!(
                "Failed to mark webhook event as processed: {}",
                e
            ))),
        }?;

        Ok(())
    }

    async fn release_claim(&self, event_id: &str) -> Result<()> {
        use sea_orm::EntityTrait;

        db_entity::Entity::delete_by_id(event_id.to_string())
            .exec(&self.db)
            .await
            .map_err(|e| {
                crate::error::TidewayError::internal(format!(
                    "Failed to release webhook event claim: {}",
                    e
                ))
            })?;

        Ok(())
    }

    async fn cleanup_old_entries(&self) -> Result<()> {
        use sea_orm::{ColumnTrait, EntityTrait, QueryFilter};

        const RETENTION_DAYS: i64 = 30;
        let cutoff = chrono::Utc::now() - chrono::Duration::days(RETENTION_DAYS);

        db_entity::Entity::delete_many()
            .filter(db_entity::Column::ProcessedAt.lt(cutoff))
            .exec(&self.db)
            .await
            .map_err(|e| {
                crate::error::TidewayError::internal(format!(
                    "Failed to cleanup old webhook idempotency entries: {}",
                    e
                ))
            })?;

        Ok(())
    }
}

#[cfg(feature = "database")]
mod db_entity {
    use sea_orm::entity::prelude::*;

    #[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
    #[sea_orm(table_name = "webhook_processed_events")]
    pub struct Model {
        #[sea_orm(primary_key, auto_increment = false)]
        pub event_id: String,
        pub processed_at: DateTimeWithTimeZone,
    }

    #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
    pub enum Relation {}

    impl ActiveModelBehavior for ActiveModel {}
}

#[cfg(test)]
mod tests {
    use super::*;

    // ============ MemoryIdempotencyStore tests ============

    #[test]
    fn test_memory_store_new() {
        let store = MemoryIdempotencyStore::new();
        // Just verify it can be created
        assert!(std::mem::size_of_val(&store) > 0);
    }

    #[test]
    fn test_memory_store_default() {
        let store = MemoryIdempotencyStore::default();
        assert!(std::mem::size_of_val(&store) > 0);
    }

    #[tokio::test]
    async fn test_memory_store_is_processed_new_event() {
        let store = MemoryIdempotencyStore::new();

        // New event should not be processed
        let result = store.is_processed("event-123").await;
        assert!(result.is_ok());
        assert!(!result.unwrap());
    }

    #[tokio::test]
    async fn test_memory_store_mark_processed() {
        let store = MemoryIdempotencyStore::new();

        // Mark event as processed
        let result = store.mark_processed("event-456".to_string()).await;
        assert!(result.is_ok());

        // Now it should be marked as processed
        let is_processed = store.is_processed("event-456").await.unwrap();
        assert!(is_processed);
    }

    #[tokio::test]
    async fn test_memory_store_multiple_events() {
        let store = MemoryIdempotencyStore::new();

        // Mark multiple events
        store.mark_processed("event-1".to_string()).await.unwrap();
        store.mark_processed("event-2".to_string()).await.unwrap();
        store.mark_processed("event-3".to_string()).await.unwrap();

        // All should be processed
        assert!(store.is_processed("event-1").await.unwrap());
        assert!(store.is_processed("event-2").await.unwrap());
        assert!(store.is_processed("event-3").await.unwrap());

        // Unprocessed event should return false
        assert!(!store.is_processed("event-4").await.unwrap());
    }

    #[tokio::test]
    async fn test_memory_store_idempotent_mark() {
        let store = MemoryIdempotencyStore::new();

        // Mark same event multiple times
        store.mark_processed("event-xyz".to_string()).await.unwrap();
        store.mark_processed("event-xyz".to_string()).await.unwrap();
        store.mark_processed("event-xyz".to_string()).await.unwrap();

        // Should still be processed
        assert!(store.is_processed("event-xyz").await.unwrap());
    }

    #[tokio::test]
    async fn test_memory_store_cleanup_noop() {
        let store = MemoryIdempotencyStore::new();

        // Cleanup should succeed (default no-op implementation)
        let result = store.cleanup_old_entries().await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_memory_store_claim_event() {
        let store = MemoryIdempotencyStore::new();

        assert!(store.claim_event("event-claim").await.unwrap());
        assert!(!store.claim_event("event-claim").await.unwrap());
    }

    #[tokio::test]
    async fn test_memory_store_release_claim() {
        let store = MemoryIdempotencyStore::new();

        assert!(store.claim_event("event-release").await.unwrap());
        store.release_claim("event-release").await.unwrap();
        assert!(store.claim_event("event-release").await.unwrap());
    }

    #[tokio::test]
    async fn test_memory_store_concurrent_access() {
        use std::sync::Arc;

        let store = Arc::new(MemoryIdempotencyStore::new());

        // Spawn multiple tasks that access the store concurrently
        let mut handles = vec![];

        for i in 0..10 {
            let store_clone = Arc::clone(&store);
            let handle = tokio::spawn(async move {
                let event_id = format!("concurrent-event-{}", i);
                store_clone.mark_processed(event_id.clone()).await.unwrap();
                store_clone.is_processed(&event_id).await.unwrap()
            });
            handles.push(handle);
        }

        // All should complete successfully
        for handle in handles {
            let result = handle.await.unwrap();
            assert!(result);
        }
    }

    #[tokio::test]
    async fn test_memory_store_different_event_ids() {
        let store = MemoryIdempotencyStore::new();
        let long_id = "a".repeat(1000);

        let event_ids = [
            "evt_1234567890",
            "webhook_abc_def",
            "stripe:pi_xyz",
            "123",
            "event-with-special-chars-!@#$%",
            "",               // Empty string is valid
            long_id.as_str(), // Long event ID
        ];

        for event_id in event_ids {
            assert!(!store.is_processed(event_id).await.unwrap());
            store.mark_processed(event_id.to_string()).await.unwrap();
            assert!(store.is_processed(event_id).await.unwrap());
        }
    }

    // ============ IdempotencyStore trait tests ============

    struct CustomIdempotencyStore {
        always_processed: bool,
    }

    #[async_trait]
    impl IdempotencyStore for CustomIdempotencyStore {
        async fn is_processed(&self, _event_id: &str) -> Result<bool> {
            Ok(self.always_processed)
        }

        async fn mark_processed(&self, _event_id: String) -> Result<()> {
            Ok(())
        }
    }

    #[tokio::test]
    async fn test_custom_store_trait_impl() {
        let always_true = CustomIdempotencyStore {
            always_processed: true,
        };
        let always_false = CustomIdempotencyStore {
            always_processed: false,
        };

        assert!(always_true.is_processed("any").await.unwrap());
        assert!(!always_false.is_processed("any").await.unwrap());
    }

    #[tokio::test]
    async fn test_store_as_dyn_trait() {
        let store: Box<dyn IdempotencyStore> = Box::new(MemoryIdempotencyStore::new());

        assert!(!store.is_processed("test").await.unwrap());
        store.mark_processed("test".to_string()).await.unwrap();
        assert!(store.is_processed("test").await.unwrap());
    }

    #[tokio::test]
    async fn test_store_in_arc() {
        let store: Arc<dyn IdempotencyStore> = Arc::new(MemoryIdempotencyStore::new());

        // Clone arc and use from multiple "tasks"
        let store2 = Arc::clone(&store);

        store
            .mark_processed("shared-event".to_string())
            .await
            .unwrap();
        assert!(store2.is_processed("shared-event").await.unwrap());
    }

    #[cfg(feature = "database")]
    mod database_tests {
        use super::super::*;
        use sea_orm::{ConnectionTrait, Database, Statement};

        async fn setup_db() -> sea_orm::DatabaseConnection {
            let db = Database::connect("sqlite::memory:")
                .await
                .expect("sqlite in-memory db should connect");

            db.execute(Statement::from_string(
                sea_orm::DatabaseBackend::Sqlite,
                "CREATE TABLE webhook_processed_events (
                    event_id TEXT PRIMARY KEY NOT NULL,
                    processed_at TEXT NOT NULL
                )"
                .to_string(),
            ))
            .await
            .expect("should create webhook_processed_events table");

            db
        }

        #[tokio::test]
        async fn test_database_store_mark_and_check_processed() {
            let db = setup_db().await;
            let store = DatabaseIdempotencyStore::new(db);

            assert!(!store.is_processed("evt_1").await.unwrap());
            store.mark_processed("evt_1".to_string()).await.unwrap();
            assert!(store.is_processed("evt_1").await.unwrap());
        }

        #[tokio::test]
        async fn test_database_store_mark_is_idempotent() {
            let db = setup_db().await;
            let store = DatabaseIdempotencyStore::new(db);

            store.mark_processed("evt_dup".to_string()).await.unwrap();
            store.mark_processed("evt_dup".to_string()).await.unwrap();

            assert!(store.is_processed("evt_dup").await.unwrap());
        }

        #[tokio::test]
        async fn test_database_store_claim_event_is_atomic() {
            let db = setup_db().await;
            let store = DatabaseIdempotencyStore::new(db);

            assert!(store.claim_event("evt_claim").await.unwrap());
            assert!(!store.claim_event("evt_claim").await.unwrap());
        }

        #[tokio::test]
        async fn test_database_store_release_claim() {
            let db = setup_db().await;
            let store = DatabaseIdempotencyStore::new(db);

            assert!(store.claim_event("evt_release").await.unwrap());
            store.release_claim("evt_release").await.unwrap();
            assert!(store.claim_event("evt_release").await.unwrap());
        }
    }

    // ============ Default cleanup behavior test ============

    struct StoreWithoutCleanup;

    #[async_trait]
    impl IdempotencyStore for StoreWithoutCleanup {
        async fn is_processed(&self, _event_id: &str) -> Result<bool> {
            Ok(false)
        }

        async fn mark_processed(&self, _event_id: String) -> Result<()> {
            Ok(())
        }
        // cleanup_old_entries uses default implementation
    }

    #[tokio::test]
    async fn test_default_cleanup_implementation() {
        let store = StoreWithoutCleanup;

        // Default cleanup should be a no-op that succeeds
        let result = store.cleanup_old_entries().await;
        assert!(result.is_ok());
    }
}