/**
 * Batch get query wrapper with exponential backoff in case of exceeded throughput
 */
Batch get query wrapper with exponential backoff in case of exceeded throughput
Batch get query wrapper with exponential backoff in case of exceeded throughput
pub fn execute_conversations_batch_get_query(
    db: &mut DynamoDbClient,
    input: BatchGetItemInput,
) -> Result<Vec<Conversation>, EngineError> {
    let mut retry_times = 1;
    let mut rng = rand::thread_rng();
    let now = time::Instant::now();
    loop {
        match db.runtime.block_on(db.client.batch_get_item(input.clone())) {
            Ok(output) => {
                let items = match output.responses {
                    None => return Ok(vec![]),
                    Some(items) if items.len() == 0 => return Ok(vec![]),
                    Some(items) => items.clone(),
                };
                let mut conversations = vec![];
                for (_, item) in items {
                    for item in item {
                        let conversation: Conversation = serde_dynamodb::from_hashmap(item)?;
                        conversations.push(conversation)
                    }
                }
                return Ok(conversations);
            }
            // request rate is too high, reduce the frequency of requests and use exponential backoff. "https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Programming.Errors.html#Programming.Errors.RetryAndBackoff"
            Err(RusotoError::Service(BatchGetItemError::ProvisionedThroughputExceeded(err))) => {
                let interval = std::cmp::min(MAX_INTERVAL_LIMIT, RETRY_BASE * 2 * retry_times);
                let interval_jitter = rng.gen_range(0..interval);
                let duration = time::Duration::from_millis(interval_jitter);
                thread::sleep(duration);
                if now.elapsed() >= time::Duration::from_millis(MAX_ELAPSED_TIME_MILLIS) {
                    // if time elapsed reach the MAX_ELAPSED_TIME_MILLIS return error
                    return Err(RusotoError::Service(
                        BatchGetItemError::ProvisionedThroughputExceeded(err),
                    )
                    .into());
                }
            }
            Err(err) => return Err(err.into()),
        }
        retry_times += 1;
    }
}
pub fn execute_conversations_batch_get_query ( db : & mut DynamoDbClient , input : BatchGetItemInput , ) -> Result < Vec < Conversation > , EngineError > { let mut retry_times = 1 ; let mut rng = rand :: thread_rng ( ) ; let now = time :: Instant :: now ( ) ; loop { match db . runtime . block_on ( db . client . batch_get_item ( input . clone ( ) ) ) { Ok ( output ) => { let items = match output . responses { None => return Ok ( vec ! [ ] ) , Some ( items ) if items . len ( ) == 0 => return Ok ( vec ! [ ] ) , Some ( items ) => items . clone ( ) , } ; let mut conversations = vec ! [ ] ; for ( _ , item ) in items { for item in item { let conversation : Conversation = serde_dynamodb :: from_hashmap ( item ) ? ; conversations . push ( conversation ) } } return Ok ( conversations ) ; } // request rate is too high, reduce the frequency of requests and use exponential backoff. "https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Programming.Errors.html#Programming.Errors.RetryAndBackoff" Err ( RusotoError :: Service ( BatchGetItemError :: ProvisionedThroughputExceeded ( err ) ) ) => { let interval = std :: cmp :: min ( MAX_INTERVAL_LIMIT , RETRY_BASE * 2 * retry_times ) ; let interval_jitter = rng . gen_range ( 0 .. interval ) ; let duration = time :: Duration :: from_millis ( interval_jitter ) ; thread :: sleep ( duration ) ; if now . elapsed ( ) >= time :: Duration :: from_millis ( MAX_ELAPSED_TIME_MILLIS ) { // if time elapsed reach the MAX_ELAPSED_TIME_MILLIS return error return Err ( RusotoError :: Service ( BatchGetItemError :: ProvisionedThroughputExceeded ( err ) , ) . into ( ) ) ; } } Err ( err ) => return Err ( err . into ( ) ) , } retry_times += 1 ; } }
Batch get query wrapper with exponential backoff in case of exceeded throughput
Batch get query wrapper with exponential backoff in case of exceeded throughput
db
&mut DynamoDbClient
input
BatchGetItemInput