2019-04-20 04:59:24 +02:00
|
|
|
package exh.eh
|
|
|
|
|
|
2021-05-21 23:06:40 +02:00
|
|
|
import kotlinx.coroutines.delay
|
2021-12-25 18:07:09 +01:00
|
|
|
import kotlin.time.Duration
|
|
|
|
|
import kotlin.time.Duration.Companion.milliseconds
|
|
|
|
|
import kotlin.time.Duration.Companion.seconds
|
2021-05-21 23:06:40 +02:00
|
|
|
|
2020-04-04 22:30:05 +02:00
|
|
|
class EHentaiThrottleManager(
|
2021-12-25 18:07:09 +01:00
|
|
|
private val max: Duration = THROTTLE_MAX,
|
2022-04-08 21:30:30 +02:00
|
|
|
private val inc: Duration = THROTTLE_INC,
|
2020-04-04 22:30:05 +02:00
|
|
|
) {
|
2021-12-25 18:07:09 +01:00
|
|
|
private var lastThrottleTime = Duration.ZERO
|
|
|
|
|
var throttleTime = Duration.ZERO
|
2019-04-20 04:59:24 +02:00
|
|
|
private set
|
|
|
|
|
|
2021-05-21 23:06:40 +02:00
|
|
|
suspend fun throttle() {
|
2020-04-04 22:30:05 +02:00
|
|
|
// Throttle requests if necessary
|
2021-12-25 18:07:09 +01:00
|
|
|
val now = System.currentTimeMillis().milliseconds
|
2019-04-20 04:59:24 +02:00
|
|
|
val timeDiff = now - lastThrottleTime
|
2020-05-02 06:46:24 +02:00
|
|
|
if (timeDiff < throttleTime) {
|
2021-05-21 23:06:40 +02:00
|
|
|
delay(throttleTime - timeDiff)
|
2020-05-02 06:46:24 +02:00
|
|
|
}
|
2019-04-20 04:59:24 +02:00
|
|
|
|
2020-05-02 06:46:24 +02:00
|
|
|
if (throttleTime < max) {
|
2019-04-20 04:59:24 +02:00
|
|
|
throttleTime += inc
|
2020-05-02 06:46:24 +02:00
|
|
|
}
|
2019-04-20 04:59:24 +02:00
|
|
|
|
2021-12-25 18:07:09 +01:00
|
|
|
lastThrottleTime = System.currentTimeMillis().milliseconds
|
2019-04-20 04:59:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun resetThrottle() {
|
2021-12-25 18:07:09 +01:00
|
|
|
lastThrottleTime = Duration.ZERO
|
|
|
|
|
throttleTime = Duration.ZERO
|
2019-04-20 04:59:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
companion object {
|
2021-12-25 18:07:09 +01:00
|
|
|
val THROTTLE_MAX = 5.5.seconds
|
|
|
|
|
val THROTTLE_INC = 20.milliseconds
|
2019-04-20 04:59:24 +02:00
|
|
|
}
|
2020-04-04 22:30:05 +02:00
|
|
|
}
|