I have a drive application that requests all files that aren't trashed. But sometimes it throws a IOexception with read timeout. Is there a way to avoid this?
This is the error I get:
An error occurred: java.net.SocketTimeoutException: Read timed out
Maybe my exponential backoff is implemented wrong.
Here's the code I use to get the files:
private static List<File> retrieveAllNoTrashFiles(Drive service) throws IOException, InterruptedException {
List<File> result = new ArrayList<File>();
Files.List request = service.files().list().setQ("trashed = false").setMaxResults(1000);
do {
try {
FileList files =executeRequest(service,request);
result.addAll(files.getItems());
request.setPageToken(files.getNextPageToken());
} catch (IOException e) { //here I sometimes get the read timeout
System.out.println("An error occurred: " + e);
request.setPageToken(null);
}
} while (request.getPageToken() != null
&& request.getPageToken().length() > 0);
return result;
}
private static FileList executeRequest(Drive service,Files.List request) throws IOException, InterruptedException {
Random randomGenerator = new Random();
for (int n = 0; n < 5; ++n) {
try {
return(request.execute());
} catch (GoogleJsonResponseException e) {
if (e.getDetails().getCode() == 403
&& (e.getDetails().getErrors().get(0).getReason().equals("rateLimitExceeded")
|| e.getDetails().getErrors().get(0).getReason().equals("userRateLimitExceeded"))) {
// Apply exponential backoff.
Thread.sleep((1 << n) * 1000 + randomGenerator.nextInt(1001));
}
//else {
// Other error, re-throw.
// throw e;
// }
}
}catch(SocketTimeoutException e){
Thread.sleep((1 << n) * 1000 + randomGenerator.nextInt(1001));
}
System.err.println("There has been an error, the request never succeeded.");
return null;
}