From e8f284d377ebe6930e147cd36494020d4ae96f47 Mon Sep 17 00:00:00 2001 From: arkon Date: Sun, 28 Nov 2021 14:41:46 -0500 Subject: [PATCH] Add convenience extension functions for rate limit interceptors To be included in extension-lib 1.3 as a replacement for the lib that's currently compiled in tachiyomi-extensions. --- .../tachiyomi/data/track/anilist/AnilistApi.kt | 6 +++--- .../network/interceptor/RateLimitInterceptor.kt | 13 ++++++++++--- .../SpecificHostRateLimitInterceptor.kt | 14 +++++++++++--- 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/track/anilist/AnilistApi.kt b/app/src/main/java/eu/kanade/tachiyomi/data/track/anilist/AnilistApi.kt index 06d4a6e341..f05a0dc06b 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/track/anilist/AnilistApi.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/track/anilist/AnilistApi.kt @@ -6,7 +6,7 @@ import eu.kanade.tachiyomi.data.database.models.Track import eu.kanade.tachiyomi.data.track.model.TrackSearch import eu.kanade.tachiyomi.network.POST import eu.kanade.tachiyomi.network.await -import eu.kanade.tachiyomi.network.interceptor.RateLimitInterceptor +import eu.kanade.tachiyomi.network.interceptor.rateLimit import eu.kanade.tachiyomi.network.jsonMime import eu.kanade.tachiyomi.network.parseAs import eu.kanade.tachiyomi.util.lang.withIOContext @@ -25,13 +25,13 @@ import kotlinx.serialization.json.putJsonObject import okhttp3.OkHttpClient import okhttp3.RequestBody.Companion.toRequestBody import java.util.Calendar -import java.util.concurrent.TimeUnit.MINUTES +import java.util.concurrent.TimeUnit class AnilistApi(val client: OkHttpClient, interceptor: AnilistInterceptor) { private val authClient = client.newBuilder() .addInterceptor(interceptor) - .addInterceptor(RateLimitInterceptor(85, 1, MINUTES)) + .rateLimit(permits = 85, period = 1, unit = TimeUnit.MINUTES) .build() suspend fun addLibManga(track: Track): Track { diff --git a/app/src/main/java/eu/kanade/tachiyomi/network/interceptor/RateLimitInterceptor.kt b/app/src/main/java/eu/kanade/tachiyomi/network/interceptor/RateLimitInterceptor.kt index b212c2f11d..241e4923ce 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/network/interceptor/RateLimitInterceptor.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/network/interceptor/RateLimitInterceptor.kt @@ -2,6 +2,7 @@ package eu.kanade.tachiyomi.network.interceptor import android.os.SystemClock import okhttp3.Interceptor +import okhttp3.OkHttpClient import okhttp3.Response import java.util.concurrent.TimeUnit @@ -17,10 +18,16 @@ import java.util.concurrent.TimeUnit * @param period {Long} The limiting duration. Defaults to 1. * @param unit {TimeUnit} The unit of time for the period. Defaults to seconds. */ -class RateLimitInterceptor( +fun OkHttpClient.Builder.rateLimit( + permits: Int, + period: Long = 1, + unit: TimeUnit = TimeUnit.SECONDS, +) = addInterceptor(RateLimitInterceptor(permits, period, unit)) + +private class RateLimitInterceptor( private val permits: Int, - private val period: Long = 1, - private val unit: TimeUnit = TimeUnit.SECONDS + period: Long, + unit: TimeUnit, ) : Interceptor { private val requestQueue = ArrayList(permits) diff --git a/app/src/main/java/eu/kanade/tachiyomi/network/interceptor/SpecificHostRateLimitInterceptor.kt b/app/src/main/java/eu/kanade/tachiyomi/network/interceptor/SpecificHostRateLimitInterceptor.kt index 88f97e7453..bce9b3f2df 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/network/interceptor/SpecificHostRateLimitInterceptor.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/network/interceptor/SpecificHostRateLimitInterceptor.kt @@ -3,6 +3,7 @@ package eu.kanade.tachiyomi.network.interceptor import android.os.SystemClock import okhttp3.HttpUrl import okhttp3.Interceptor +import okhttp3.OkHttpClient import okhttp3.Response import java.util.concurrent.TimeUnit @@ -19,11 +20,18 @@ import java.util.concurrent.TimeUnit * @param period {Long} The limiting duration. Defaults to 1. * @param unit {TimeUnit} The unit of time for the period. Defaults to seconds. */ +fun OkHttpClient.Builder.rateLimitHost( + httpUrl: HttpUrl, + permits: Int, + period: Long = 1, + unit: TimeUnit = TimeUnit.SECONDS, +) = addInterceptor(SpecificHostRateLimitInterceptor(httpUrl, permits, period, unit)) + class SpecificHostRateLimitInterceptor( - private val httpUrl: HttpUrl, + httpUrl: HttpUrl, private val permits: Int, - private val period: Long = 1, - private val unit: TimeUnit = TimeUnit.SECONDS + period: Long, + unit: TimeUnit, ) : Interceptor { private val requestQueue = ArrayList(permits)