This repository has no description
0

Configure Feed

Select the types of activity you want to include in your feed.

refactor: improve lock acquisition strategy with more robust retry mechanism

Co-authored-by: aider (anthropic/claude-sonnet-4-20250514) <aider@aider.chat>

+13 -4
+13 -4
src/modules/feeds/application/sagas/CardCollectionSaga.ts
··· 29 29 async handleCardEvent( 30 30 event: CardAddedToLibraryEvent | CardAddedToCollectionEvent, 31 31 ): Promise<Result<void>> { 32 + console.log('Handling card event:', event); 32 33 const aggregationKey = this.createKey(event); 33 34 34 - // Retry lock acquisition with exponential backoff 35 - const maxRetries = 5; 36 - const baseDelay = 50; // ms 35 + // Retry lock acquisition with longer delays and more attempts for high concurrency 36 + const maxRetries = 15; // Increased from 5 37 + const baseDelay = 100; // Increased from 50ms 38 + const maxDelay = 2000; // Cap the maximum delay 37 39 38 40 for (let attempt = 0; attempt < maxRetries; attempt++) { 39 41 const lockAcquired = await this.acquireLock(aggregationKey); ··· 43 45 const existing = await this.getPendingActivity(aggregationKey); 44 46 45 47 if (existing && this.isWithinWindow(existing)) { 48 + console.log(`Merging event into existing activity for ${aggregationKey}`); 46 49 this.mergeActivity(existing, event); 47 50 await this.setPendingActivity(aggregationKey, existing); 48 51 } else { 52 + console.log(`Creating new pending activity for ${aggregationKey}`); 49 53 const newActivity = this.createNewPendingActivity(event); 50 54 await this.setPendingActivity(aggregationKey, newActivity); 51 55 await this.scheduleFlush(aggregationKey); ··· 59 63 60 64 // Lock not acquired, wait and retry 61 65 if (attempt < maxRetries - 1) { 62 - const delay = baseDelay * Math.pow(2, attempt); // Exponential backoff 66 + // Use exponential backoff with jitter and cap at maxDelay 67 + const exponentialDelay = baseDelay * Math.pow(1.5, attempt); 68 + const jitter = Math.random() * 50; // Add randomness to prevent thundering herd 69 + const delay = Math.min(exponentialDelay + jitter, maxDelay); 70 + 71 + console.log(`Lock acquisition failed for ${aggregationKey}, retrying in ${Math.round(delay)}ms (attempt ${attempt + 1}/${maxRetries})`); 63 72 await new Promise((resolve) => setTimeout(resolve, delay)); 64 73 } 65 74 }