Implement ExponentialBackOff to GoogleDrive to hopefully workaround #323

We now retry a request if e.g. a status code 500 was received for max 15 seconds with exponential back off
This commit is contained in:
Julian Raufelder 2021-09-01 09:32:39 +02:00
parent 218b5c0b77
commit d4bf3ff668
No known key found for this signature in database
GPG Key ID: 17EE71F6634E381D

View File

@ -1,8 +1,11 @@
package org.cryptomator.data.cloud.googledrive
import android.content.Context
import com.google.api.client.http.HttpBackOffIOExceptionHandler
import com.google.api.client.http.HttpBackOffUnsuccessfulResponseHandler
import com.google.api.client.http.javanet.NetHttpTransport
import com.google.api.client.json.gson.GsonFactory
import com.google.api.client.util.ExponentialBackOff
import com.google.api.services.drive.Drive
import com.google.api.services.drive.DriveScopes
import org.cryptomator.data.BuildConfig
@ -30,12 +33,7 @@ class GoogleDriveClientFactory internal constructor() {
Logger.getLogger("com.google.api.client").level = Level.CONFIG
Logger.getLogger("com.google.api.client").addHandler(object : Handler() {
override fun publish(record: LogRecord) {
if (record.message.startsWith("-------------- RESPONSE --------------") //
|| record.message.startsWith("-------------- REQUEST --------------") //
|| record.message.startsWith("{\n \"files\": [\n")
) {
Timber.tag("GoogleDriveClient").d(record.message)
}
Timber.tag("GoogleDriveClient").d(record.message)
}
override fun flush() {}
@ -48,8 +46,20 @@ class GoogleDriveClientFactory internal constructor() {
val credential = FixedGoogleAccountCredential.usingOAuth2(context, setOf(DriveScopes.DRIVE)).also { it.setAccountName(accountName) }
return Drive.Builder(NetHttpTransport(), GsonFactory.getDefaultInstance(), credential) //
.setApplicationName("Cryptomator-Android/" + BuildConfig.VERSION_NAME) //
.build()
.setHttpRequestInitializer { request ->
credential.initialize(request)
val exponentialBackOff = ExponentialBackOff.Builder().setMaxElapsedTimeMillis(15 * 1000).build()
request.unsuccessfulResponseHandler = HttpBackOffUnsuccessfulResponseHandler(exponentialBackOff).setBackOffRequired { response ->
response.statusCode == 403 || response.statusCode / 100 == 5
}
request.ioExceptionHandler = HttpBackOffIOExceptionHandler(exponentialBackOff)
// trim down logging
request.isCurlLoggingEnabled = false
request.contentLoggingLimit = 0
}
.build()
}
}
}