import { Logger , SavedObjectsErrorHelpers } from '../../../../../src/core/server' ; type RetryableForConflicts < T > = ( ) => Promise < T > ; export const RetryForConflictsAttempts = 2 ; const RetryForConflictsDelay = 250 ; export async function retryIfConflicts < T > ( logger , name , operation , retries = RetryForConflictsAttempts ) { try { return await operation ( ) ; } catch ( err ) { if ( ! SavedObjectsErrorHelpers . isConflictError ( err ) ) { throw err ; } if ( retries <= 0 ) { logger . warn ( `${ name } conflict, exceeded retries` ) ; throw err ; } logger . debug ( `${ name } conflict, retrying ...` ) ; await waitBeforeNextRetry ( ) ; return await retryIfConflicts ( logger , name , operation , retries - 1 ) ; } } async function waitBeforeNextRetry ( ) { await new Promise ( ( resolve ) => setTimeout ( resolve , RetryForConflictsDelay ) ) ; }