|
| 1 | +package me.proxer.library.internal.interceptor |
| 2 | + |
| 3 | +import com.squareup.moshi.Moshi |
| 4 | +import com.squareup.moshi.Types |
| 5 | +import me.proxer.library.ProxerException.ServerErrorType.RATE_LIMIT |
| 6 | +import me.proxer.library.internal.ProxerResponse |
| 7 | +import me.proxer.library.util.ProxerUrls |
| 8 | +import okhttp3.Interceptor |
| 9 | +import okhttp3.MediaType.Companion.toMediaTypeOrNull |
| 10 | +import okhttp3.Protocol |
| 11 | +import okhttp3.Request |
| 12 | +import okhttp3.Response |
| 13 | +import okhttp3.ResponseBody.Companion.toResponseBody |
| 14 | +import java.util.Date |
| 15 | +import java.util.concurrent.locks.ReentrantReadWriteLock |
| 16 | +import kotlin.concurrent.read |
| 17 | +import kotlin.concurrent.write |
| 18 | + |
| 19 | +internal class RateLimitInterceptor(private val moshi: Moshi) : Interceptor { |
| 20 | + |
| 21 | + private companion object { |
| 22 | + private const val WAIT_THRESHOLD = 2_000 |
| 23 | + |
| 24 | + private val errorMediaType = "application/json".toMediaTypeOrNull() |
| 25 | + private val responseType = Types.newParameterizedType(ProxerResponse::class.java, Unit::class.java) |
| 26 | + |
| 27 | + private val limits = mapOf( |
| 28 | + "anime" to Limit(20, 360_000), |
| 29 | + "apps" to Limit(100, 30_000), // Not enforced by api. |
| 30 | + "chat" to Limit(30, 30_000), |
| 31 | + "comment" to Limit(100, 30_000), // Not enforced by api. |
| 32 | + "files" to Limit(30, 100_000), |
| 33 | + "forum" to Limit(100, 30_000), // Not enforced by api. |
| 34 | + "info" to Limit(60, 360_000), |
| 35 | + "list" to Limit(60, 60_000), |
| 36 | + "manga" to Limit(10, 300_000), |
| 37 | + "media" to Limit(100, 30_000), // Not enforced by api. |
| 38 | + "messenger" to Limit(100, 50_000), |
| 39 | + "misc" to Limit(100, 30_000), // Not enforced by api. |
| 40 | + "notifications" to Limit(100, 30_000), // Not enforced by api. |
| 41 | + "ucp" to Limit(40, 60_000), |
| 42 | + "user" to Limit(40, 360_000), |
| 43 | + "users" to Limit(40, 360_000), |
| 44 | + "wiki" to Limit(100, 30_000) // Not enforced by api. |
| 45 | + ) |
| 46 | + } |
| 47 | + |
| 48 | + private var state = limits.mapValues { State(0, null) } |
| 49 | + |
| 50 | + private val lock = ReentrantReadWriteLock() |
| 51 | + |
| 52 | + @Suppress("ReturnCount") |
| 53 | + override fun intercept(chain: Interceptor.Chain): Response { |
| 54 | + val oldRequest = chain.request() |
| 55 | + |
| 56 | + if (oldRequest.url.host != ProxerUrls.apiBase.host) { |
| 57 | + return chain.proceed(oldRequest) |
| 58 | + } |
| 59 | + |
| 60 | + val apiClass = oldRequest.url.pathSegments.getOrNull(2) |
| 61 | + val limit = limits[apiClass] |
| 62 | + |
| 63 | + if (apiClass == null || limit == null) { |
| 64 | + return chain.proceed(oldRequest) |
| 65 | + } |
| 66 | + |
| 67 | + return intercept(apiClass, limit, chain, oldRequest) |
| 68 | + } |
| 69 | + |
| 70 | + @Suppress("ReturnCount") |
| 71 | + private fun intercept( |
| 72 | + apiClass: String, |
| 73 | + limit: Limit, |
| 74 | + chain: Interceptor.Chain, |
| 75 | + oldRequest: Request |
| 76 | + ): Response { |
| 77 | + val (requests, firstRequest) = lock.read { state.getValue(apiClass) } |
| 78 | + val timeDifferenceMillis = if (firstRequest != null) Date().time - firstRequest.time else null |
| 79 | + |
| 80 | + if (timeDifferenceMillis == null || timeDifferenceMillis > limit.millis) { |
| 81 | + lock.write { state = state.update(apiClass, 1, Date()) } |
| 82 | + |
| 83 | + return chain.proceed(oldRequest) |
| 84 | + } else if (requests + 2 >= limit.maxRequests) { |
| 85 | + return if ((limit.millis - timeDifferenceMillis) < WAIT_THRESHOLD) { |
| 86 | + Thread.sleep(timeDifferenceMillis) |
| 87 | + |
| 88 | + intercept(chain) |
| 89 | + } else { |
| 90 | + val errorBody = moshi |
| 91 | + .adapter<ProxerResponse<Unit?>>(responseType) |
| 92 | + .toJson(ProxerResponse(true, "Rate limit", RATE_LIMIT.code, null)) |
| 93 | + |
| 94 | + Response.Builder() |
| 95 | + .body(errorBody.toResponseBody(errorMediaType)) |
| 96 | + .protocol(Protocol.HTTP_1_1) |
| 97 | + .request(oldRequest) |
| 98 | + .code(200) |
| 99 | + .message("") |
| 100 | + .build() |
| 101 | + } |
| 102 | + } else { |
| 103 | + lock.write { state = state.update(apiClass, requests + 1, firstRequest) } |
| 104 | + |
| 105 | + return chain.proceed(oldRequest) |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + private fun Map<String, State>.update(apiClass: String, requests: Int, firstRequest: Date?): Map<String, State> { |
| 110 | + return toMutableMap().apply { put(apiClass, State(requests, firstRequest)) } |
| 111 | + } |
| 112 | + |
| 113 | + private data class Limit(val maxRequests: Int, val millis: Int) |
| 114 | + private data class State(val requests: Int, val firstRequest: Date?) |
| 115 | +} |
0 commit comments