public static < T > Optional < T > tryAmazonAction ( String actionLabel , Supplier < T > action , AtomicLong durationBetweenRequests ) { try { return of ( action . get ( ) ) ; } catch ( LimitExceededException | ProvisionedThroughputExceededException | KMSThrottlingException e ) { int durationRandomModifier = 1 + RANDOM . nextInt ( 64 ) ; // random duration to make readers out of sync, avoiding simultaneous readings long updatedDuration = durationBetweenRequests . updateAndGet ( duration -> duration * 2 // twice the duration + duration * 2 / durationRandomModifier ) ; // add random duration to avoid simultaneous reads LOG . debug ( "Update of minimal duration between two get shard iterator requests" , args -> args . add ( "actionLabel" , actionLabel ) . add ( "new minimalDurationBetweenTwoGetShardIteratorRequests" , updatedDuration ) ) ; } return empty ( ) ; }
Try to perform an Amazon action and increase the duration between requests if some exception is exceeding resource usage exception is thrown .