-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PK: Configurable rate limiting support
- Loading branch information
Showing
4 changed files
with
109 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
...otlin/com/kotlindiscord/kord/extensions/modules/extra/pluralkit/config/PKConfigBuilder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
@file:Suppress("StringLiteralDuplication") | ||
|
||
package com.kotlindiscord.kord.extensions.modules.extra.pluralkit.config | ||
|
||
import dev.kord.common.ratelimit.IntervalRateLimiter | ||
import kotlin.time.Duration | ||
import kotlin.time.Duration.Companion.seconds | ||
|
||
class PKConfigBuilder { | ||
/** | ||
* A mapping of domain names to their corresponding rate limiters. | ||
* | ||
* Provide `null` for a domain to disable rate limiting. | ||
*/ | ||
val domainRateLimiters: MutableMap<String, IntervalRateLimiter?> = mutableMapOf( | ||
"api.pluralkit.me" to IntervalRateLimiter(2, 1.seconds) | ||
) | ||
|
||
/** | ||
* Rate limiter to use by default, when there's no domain-specific rate limiter. | ||
* | ||
* Provide `null` to disable rate limiting. | ||
*/ | ||
var defaultRateLimiter: IntervalRateLimiter? = IntervalRateLimiter(2, 1.seconds) | ||
|
||
/** Replace the default rate limiter, using the specified settings. **/ | ||
fun defaultLimit(limit: Int, interval: Duration) { | ||
defaultRateLimiter = IntervalRateLimiter(limit, interval) | ||
} | ||
|
||
/** Remove the default rate limiter, disabling rate limiting by default. **/ | ||
fun unlimitByDefault() { | ||
defaultRateLimiter = null | ||
} | ||
|
||
/** Set a domain-specific rate limiter, using the specified settings. **/ | ||
fun domainLimit(domain: String, limit: Int, interval: Duration) { | ||
if ("/" in domain) { | ||
error("URL provided as the `domain` parameter - please provide a domain instead.") | ||
} | ||
|
||
domainRateLimiters[domain] = IntervalRateLimiter(limit, interval) | ||
} | ||
|
||
/** Remove a domain-specific rate limiter, making it use the default rate limiter instead. **/ | ||
fun defaultDomainLimit(domain: String) { | ||
if ("/" in domain) { | ||
error("URL provided as the `domain` parameter - please provide a domain instead.") | ||
} | ||
|
||
domainRateLimiters.remove(domain) | ||
} | ||
|
||
/** Disable rate limiting for the given domain. **/ | ||
fun unlimitDomain(domain: String) { | ||
if ("/" in domain) { | ||
error("URL provided as the `domain` parameter - please provide a domain instead.") | ||
} | ||
|
||
domainRateLimiters[domain] = null | ||
} | ||
|
||
internal fun getLimiter(url: String): IntervalRateLimiter? { | ||
var domain = url | ||
|
||
if ("://" in domain) { | ||
domain = domain.split("://", limit = 2).first() | ||
} | ||
|
||
if ("/" in domain) { | ||
domain = domain.split("/", limit = 2).first() | ||
} | ||
|
||
if (domain in domainRateLimiters) { | ||
return domainRateLimiters[domain] | ||
} | ||
|
||
return defaultRateLimiter | ||
} | ||
} |