public boolean shouldRetry ( int previousAttempts , long totalElapsedTimeMillis , HttpResponse response ) { String contentType = response . getEntity ( ) . getContentType ( ) ; if ( contentType != null && ! contentType . startsWith ( JSON_MIME_TYPE ) ) { // it's not JSON; don't even try to parse it return false ; } Error error ; try { error = jsonSerializer . deserialize ( Error . class , response . getEntity ( ) . getContent ( ) ) ; } catch ( IOException e ) { return false ; } switch ( error . getErrorCode ( ) ) { case 4001 : /** Smartsheet.com is currently offline for system maintenance. Please check back again shortly. */ case 4002 : /** Server timeout exceeded. Request has failed */ case 4003 : /** Rate limit exceeded. */ case 4004 : /** An unexpected error has occurred. Please retry your request. * If you encounter this error repeatedly, please contact api@smartsheet.com for assistance. */ break ; default : return false ; } long backoffMillis = calcBackoff ( previousAttempts , totalElapsedTimeMillis , error ) ; if ( backoffMillis < 0 ) return false ; logger . info ( "HttpError StatusCode=" + response . getStatusCode ( ) + ": Retrying in " + backoffMillis + " milliseconds" ) ; try { Thread . sleep ( backoffMillis ) ; } catch ( InterruptedException e ) { logger . warn ( "sleep interrupted" , e ) ; return false ; } return true ; }
Called when an API request fails to determine if it can retry the request . Calls calcBackoff to determine the time to wait in between retries .