mirror of
https://github.com/mihonapp/mihon.git
synced 2025-11-08 18:18:56 +01:00
Add throttling when restoring E-Hentai/ExHentai galleries.
This commit is contained in:
31
app/src/main/java/exh/eh/EHentaiThrottleManager.kt
Normal file
31
app/src/main/java/exh/eh/EHentaiThrottleManager.kt
Normal file
@@ -0,0 +1,31 @@
|
||||
package exh.eh
|
||||
|
||||
class EHentaiThrottleManager(private val max: Int = THROTTLE_MAX,
|
||||
private val inc: Int = THROTTLE_INC) {
|
||||
private var lastThrottleTime: Long = 0
|
||||
var throttleTime: Long = 0
|
||||
private set
|
||||
|
||||
fun throttle() {
|
||||
//Throttle requests if necessary
|
||||
val now = System.currentTimeMillis()
|
||||
val timeDiff = now - lastThrottleTime
|
||||
if(timeDiff < throttleTime)
|
||||
Thread.sleep(throttleTime - timeDiff)
|
||||
|
||||
if(throttleTime < max)
|
||||
throttleTime += inc
|
||||
|
||||
lastThrottleTime = System.currentTimeMillis()
|
||||
}
|
||||
|
||||
fun resetThrottle() {
|
||||
lastThrottleTime = 0
|
||||
throttleTime = 0
|
||||
}
|
||||
|
||||
companion object {
|
||||
const val THROTTLE_MAX = 5500
|
||||
const val THROTTLE_INC = 20
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user