private void handleResponse ( RestRequest < ? > request , RestRequestResult result , RatelimitBucket bucket , long responseTimestamp ) { if ( result == null || result . getResponse ( ) == null ) { return ; } Response response = result . getResponse ( ) ; boolean global = response . header ( "X-RateLimit-Global" , "false" ) . equalsIgnoreCase ( "true" ) ; int remaining = Integer . valueOf ( response . header ( "X-RateLimit-Remaining" , "1" ) ) ; long reset = request . getEndpoint ( ) . getHardcodedRatelimit ( ) . map ( ratelimit -> responseTimestamp + api . getTimeOffset ( ) + ratelimit ) . orElseGet ( ( ) -> Long . parseLong ( response . header ( "X-RateLimit-Reset" , "0" ) ) * 1000 ) ; // Check if we received a 429 response if ( result . getResponse ( ) . code ( ) == 429 ) { int retryAfter = result . getJsonBody ( ) . isNull ( ) ? 0 : result . getJsonBody ( ) . get ( "retry_after" ) . asInt ( ) ; if ( global ) { // We hit a global ratelimit. Time to panic! logger . warn ( "Hit a global ratelimit! This means you were sending a very large " + "amount within a very short time frame." ) ; RatelimitBucket . setGlobalRatelimitResetTimestamp ( api , responseTimestamp + retryAfter ) ; } else { logger . debug ( "Received a 429 response from Discord! Recalculating time offset..." ) ; // Setting the offset to null causes a recalculate for the next request api . setTimeOffset ( null ) ; // Update the bucket information bucket . setRatelimitRemaining ( 0 ) ; bucket . setRatelimitResetTimestamp ( responseTimestamp + retryAfter ) ; } } else { // Check if we didn't already complete it exceptionally. CompletableFuture < RestRequestResult > requestResult = request . getResult ( ) ; if ( ! requestResult . isDone ( ) ) { requestResult . complete ( result ) ; } // Update bucket information bucket . setRatelimitRemaining ( remaining ) ; bucket . setRatelimitResetTimestamp ( reset ) ; } }
Updates the ratelimit information and sets the result if the request was successful .