What is the "elegant" method of using BackOffPolicy

I sometimes noticed that my SDK calls on my computer periodically interrupted with a 500 error, retrying the request usually fixes the error immediately. Looking at the Google documentation, it is recommended to use an exponential return technique, I wrote my own implementation that works fine, however, during debugging, I accidentally ran into the BackOffPolicy interface, with a lot of research, I even found an implementation of ExponentialBackOffPolicy.

So instead of managing it myself, I decided to better remove my implementation and let the SDK do it for me. The delay policy (according to my information) can be configured using the built-in HTTP request, so it is quite easy to use it when creating requests manually or when using batch requests, however I cannot find the easiest place to enter this value when using the main SDK, for example if I want to set a deferral policy for:

drive.files().get(id).execute(); 

I can not do something like:

 drive.files().get(id) .setBackOffPolicy(new ExponentialBackOffPolicy()) .execute(); 

I would need to do this:

 drive.files().get(id) .buildHttpRequest() .setBackOffPolicy(new ExponentialBackOffPolicy()) .execute(); 

However, if I do this, I will also need to reproduce the syntax logic of the execute method from Drive.Files.Get and convert the result to a File object, which is obviously not an ideal approach. Is there an easier way to “add” this as a whole to the request, or perhaps specify a way to override the default policy for all requests?

If not, maybe this could be introduced in a future version of the Drive SDK?

Thanks David

+4
source share
3 answers

AFAIK Google, made by Exponential Backoff, is used only when loading or downloading content in the Java interface of the drive. For other queries, you need your own implementation.

What I did to implement it for any request was to complete Callable after the request and let the wrapper class handle exceptions and retry the request.

0
source

The Google Drive SDK documentation includes an example showing how to correctly implement exponential deferral using the Drive API:

https://developers.google.com/drive/handle-errors

+2
source

The code for exponential delay is used here:

 boolean thisIsARetry = false; boolean keepRetrying = true; int delayTime = 2; int maxDelayTime = 4; while(keepRetrying && delayTime <= maxDelayTime) { if(thisIsARetry) { //sleep for delayTime seconds try { Thread.sleep(1000 * delayTime); delayTime *= 2; } catch (Exception e) { //print exception delayTime *= 2; continue; } } else { thisIsARetry = true; } //the actual network request or some other operation here if(ACTUAL_OPERATION_SUCCESS) { keepRetrying = false; } } 
+2
source

Source: https://habr.com/ru/post/1438313/


All Articles