qapinterface
Version:
Comprehensive API utilities for Node.js applications including authentication, security, request processing, and response handling with zero external dependencies
35 lines (30 loc) • 1.12 kB
JavaScript
/**
* Retry Handler Service
* Single Responsibility: Handle retry logic with exponential backoff
*/
const { DEFAULT_INITIAL_DELAY_MS } = require('../../config/localVars');
/**
* Validates a service connection with retry logic.
* @param {function} connectionTest - Function that returns a promise for the connection test.
* @param {object} [options={}] - Retry options.
* @returns {Promise<object>} - Connection validation result.
*/
async function validateServiceConnection(connectionTest, options = {}) {
const { maxRetries = 3, baseDelay = DEFAULT_INITIAL_DELAY_MS } = options;
for (let attempt = 1; attempt <= maxRetries; attempt++) {
try {
const result = await connectionTest();
return { success: true, result, attempt };
} catch (error) {
if (attempt === maxRetries) {
return { success: false, error: error.message, attempts: attempt };
}
// Exponential backoff
const delay = baseDelay * Math.pow(2, attempt - 1);
await new Promise(resolve => setTimeout(resolve, delay));
}
}
}
module.exports = {
validateServiceConnection
};