Skip to content

Commit

Permalink
Retry fetches of resource index a few times with exponential backoff (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
chvp authored Jun 21, 2022
1 parent f404e91 commit 7314f35
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 96 deletions.
35 changes: 19 additions & 16 deletions app/src/main/java/me/vanpetegem/accentor/api/album/Album.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package me.vanpetegem.accentor.api.album

import com.github.kittinunf.fuel.httpGet
import me.vanpetegem.accentor.api.util.retry
import me.vanpetegem.accentor.data.albums.ApiAlbum
import me.vanpetegem.accentor.data.authentication.AuthenticationData
import me.vanpetegem.accentor.util.Result
Expand All @@ -10,22 +11,24 @@ fun index(server: String, authenticationData: AuthenticationData): Sequence<Resu
var page = 1

fun doFetch(): Result<List<ApiAlbum>>? {
return "$server/api/albums".httpGet(listOf(Pair("page", page)))
.set("Accept", "application/json")
.set("X-Secret", authenticationData.secret)
.set("X-Device-Id", authenticationData.deviceId)
.responseObject<List<ApiAlbum>>().third
.fold(
{ a: List<ApiAlbum> ->
if (a.isEmpty()) {
null
} else {
page++
Result.Success(a)
}
},
{ e: Throwable -> Result.Error(Exception("Error getting albums", e)) }
)
return retry(5) {
"$server/api/albums".httpGet(listOf(Pair("page", page)))
.set("Accept", "application/json")
.set("X-Secret", authenticationData.secret)
.set("X-Device-Id", authenticationData.deviceId)
.responseObject<List<ApiAlbum>>().third
.fold(
{ a: List<ApiAlbum> ->
if (a.isEmpty()) {
null
} else {
page++
Result.Success(a)
}
},
{ e: Throwable -> Result.Error(Exception("Error getting albums", e)) }
)
}
}

return generateSequence { doFetch() }
Expand Down
35 changes: 19 additions & 16 deletions app/src/main/java/me/vanpetegem/accentor/api/artist/Artist.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package me.vanpetegem.accentor.api.artist

import com.github.kittinunf.fuel.httpGet
import me.vanpetegem.accentor.api.util.retry
import me.vanpetegem.accentor.data.artists.ApiArtist
import me.vanpetegem.accentor.data.authentication.AuthenticationData
import me.vanpetegem.accentor.util.Result
Expand All @@ -10,22 +11,24 @@ fun index(server: String, authenticationData: AuthenticationData): Sequence<Resu
var page = 1

fun doFetch(): Result<List<ApiArtist>>? {
return "$server/api/artists".httpGet(listOf(Pair("page", page)))
.set("Accept", "application/json")
.set("X-Secret", authenticationData.secret)
.set("X-Device-Id", authenticationData.deviceId)
.responseObject<List<ApiArtist>>().third
.fold(
{ a: List<ApiArtist> ->
if (a.isEmpty()) {
null
} else {
page++
Result.Success(a)
}
},
{ e: Throwable -> Result.Error(Exception("Error getting artists", e)) }
)
return retry(5) {
"$server/api/artists".httpGet(listOf(Pair("page", page)))
.set("Accept", "application/json")
.set("X-Secret", authenticationData.secret)
.set("X-Device-Id", authenticationData.deviceId)
.responseObject<List<ApiArtist>>().third
.fold(
{ a: List<ApiArtist> ->
if (a.isEmpty()) {
null
} else {
page++
Result.Success(a)
}
},
{ e: Throwable -> Result.Error(Exception("Error getting artists", e)) }
)
}
}

return generateSequence { doFetch() }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package me.vanpetegem.accentor.api.codecconversion

import com.github.kittinunf.fuel.httpGet
import me.vanpetegem.accentor.api.util.retry
import me.vanpetegem.accentor.data.authentication.AuthenticationData
import me.vanpetegem.accentor.data.codecconversions.ApiCodecConversion
import me.vanpetegem.accentor.util.Result
Expand All @@ -10,22 +11,24 @@ fun index(server: String, authenticationData: AuthenticationData): Sequence<Resu
var page = 1

fun doFetch(): Result<List<ApiCodecConversion>>? {
return "$server/api/codec_conversions".httpGet(listOf(Pair("page", page)))
.set("Accept", "application/json")
.set("X-Secret", authenticationData.secret)
.set("X-Device-Id", authenticationData.deviceId)
.responseObject<List<ApiCodecConversion>>().third
.fold(
{ c: List<ApiCodecConversion> ->
if (c.isEmpty()) {
null
} else {
page++
Result.Success(c)
}
},
{ e: Throwable -> Result.Error(Exception("Error getting codec conversions", e)) }
)
return retry(5) {
"$server/api/codec_conversions".httpGet(listOf(Pair("page", page)))
.set("Accept", "application/json")
.set("X-Secret", authenticationData.secret)
.set("X-Device-Id", authenticationData.deviceId)
.responseObject<List<ApiCodecConversion>>().third
.fold(
{ c: List<ApiCodecConversion> ->
if (c.isEmpty()) {
null
} else {
page++
Result.Success(c)
}
},
{ e: Throwable -> Result.Error(Exception("Error getting codec conversions", e)) }
)
}
}

return generateSequence { doFetch() }
Expand Down
35 changes: 19 additions & 16 deletions app/src/main/java/me/vanpetegem/accentor/api/plays/Play.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package me.vanpetegem.accentor.api.plays
import com.github.kittinunf.fuel.httpGet
import com.github.kittinunf.fuel.httpPost
import java.time.Instant
import me.vanpetegem.accentor.api.util.retry
import me.vanpetegem.accentor.data.authentication.AuthenticationData
import me.vanpetegem.accentor.data.plays.ApiPlay
import me.vanpetegem.accentor.util.Result
Expand All @@ -29,22 +30,24 @@ fun index(server: String, authenticationData: AuthenticationData): Sequence<Resu
var page = 1

fun doFetch(): Result<List<ApiPlay>>? {
return "$server/api/plays".httpGet(listOf(Pair("page", page)))
.set("Accept", "application/json")
.set("X-Secret", authenticationData.secret)
.set("X-Device-Id", authenticationData.deviceId)
.responseObject<List<ApiPlay>>().third
.fold(
{ p: List<ApiPlay> ->
if (p.isEmpty()) {
null
} else {
page++
Result.Success(p)
}
},
{ e: Throwable -> Result.Error(Exception("Error getting plays", e)) }
)
return retry(5) {
"$server/api/plays".httpGet(listOf(Pair("page", page)))
.set("Accept", "application/json")
.set("X-Secret", authenticationData.secret)
.set("X-Device-Id", authenticationData.deviceId)
.responseObject<List<ApiPlay>>().third
.fold(
{ p: List<ApiPlay> ->
if (p.isEmpty()) {
null
} else {
page++
Result.Success(p)
}
},
{ e: Throwable -> Result.Error(Exception("Error getting plays", e)) }
)
}
}

return generateSequence { doFetch() }
Expand Down
35 changes: 19 additions & 16 deletions app/src/main/java/me/vanpetegem/accentor/api/track/Track.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package me.vanpetegem.accentor.api.track

import com.github.kittinunf.fuel.httpGet
import me.vanpetegem.accentor.api.util.retry
import me.vanpetegem.accentor.data.authentication.AuthenticationData
import me.vanpetegem.accentor.data.tracks.ApiTrack
import me.vanpetegem.accentor.util.Result
Expand All @@ -10,22 +11,24 @@ fun index(server: String, authenticationData: AuthenticationData): Sequence<Resu
var page = 1

fun doFetch(): Result<List<ApiTrack>>? {
return "$server/api/tracks".httpGet(listOf(Pair("page", page)))
.set("Accept", "application/json")
.set("X-Secret", authenticationData.secret)
.set("X-Device-Id", authenticationData.deviceId)
.responseObject<List<ApiTrack>>().third
.fold(
{ a: List<ApiTrack> ->
if (a.isEmpty()) {
null
} else {
page++
Result.Success(a)
}
},
{ e: Throwable -> Result.Error(Exception("Error getting tracks", e)) },
)
return retry(5) {
"$server/api/tracks".httpGet(listOf(Pair("page", page)))
.set("Accept", "application/json")
.set("X-Secret", authenticationData.secret)
.set("X-Device-Id", authenticationData.deviceId)
.responseObject<List<ApiTrack>>().third
.fold(
{ a: List<ApiTrack> ->
if (a.isEmpty()) {
null
} else {
page++
Result.Success(a)
}
},
{ e: Throwable -> Result.Error(Exception("Error getting tracks", e)) },
)
}
}

return generateSequence { doFetch() }
Expand Down
35 changes: 19 additions & 16 deletions app/src/main/java/me/vanpetegem/accentor/api/user/User.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package me.vanpetegem.accentor.api.user

import com.github.kittinunf.fuel.httpGet
import me.vanpetegem.accentor.api.util.retry
import me.vanpetegem.accentor.data.authentication.AuthenticationData
import me.vanpetegem.accentor.data.users.ApiUser
import me.vanpetegem.accentor.util.Result
Expand All @@ -10,22 +11,24 @@ fun index(server: String, authenticationData: AuthenticationData): Sequence<Resu
var page = 1

fun doFetch(): Result<List<ApiUser>>? {
return "$server/api/users".httpGet(listOf(Pair("page", page)))
.set("Accept", "application/json")
.set("X-Secret", authenticationData.secret)
.set("X-Device-Id", authenticationData.deviceId)
.responseObject<List<ApiUser>>().third
.fold(
{ u: List<ApiUser> ->
if (u.isEmpty()) {
null
} else {
page++
Result.Success(u)
}
},
{ e: Throwable -> Result.Error(Exception("Error getting users", e)) }
)
return retry(5) {
"$server/api/users".httpGet(listOf(Pair("page", page)))
.set("Accept", "application/json")
.set("X-Secret", authenticationData.secret)
.set("X-Device-Id", authenticationData.deviceId)
.responseObject<List<ApiUser>>().third
.fold(
{ u: List<ApiUser> ->
if (u.isEmpty()) {
null
} else {
page++
Result.Success(u)
}
},
{ e: Throwable -> Result.Error(Exception("Error getting users", e)) }
)
}
}

return generateSequence { doFetch() }
Expand Down
16 changes: 16 additions & 0 deletions app/src/main/java/me/vanpetegem/accentor/api/util/Util.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package me.vanpetegem.accentor.api.util

import me.vanpetegem.accentor.util.Result

fun <T> retry(n: Int, block: () -> Result<T>?): Result<T>? {
var tries = 0
var result: Result<T>?
do {
if (tries > 0) {
Thread.sleep((2 shl tries) * 1000L)
}
result = block()
tries++
} while (tries < n && result is Result.Error)
return result
}

0 comments on commit 7314f35

Please sign in to comment.