Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: comply with undocumented helix rate limits (#561)
* feat: allow broad helix bandwidth customization * feat: stricter limit for helix ban and unban user * refactor: simplify request channel id extraction * feat: deplete ban bucket on 429 * feat: implement createClip rate limit * feat: implement blocked terms rate limit * refactor: move util methods to common module * refactor: create TwitchHelixRateLimitTracker * refactor: create TwitchHelixTokenManager * fix: ensure error response body stream is closed * fix: comply with slower-than-documented helix refill rate
- Loading branch information
Showing
17 changed files
with
612 additions
and
226 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
91 changes: 91 additions & 0 deletions
91
common/src/main/java/com/github/twitch4j/common/util/BucketUtils.java
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,91 @@ | ||
package com.github.twitch4j.common.util; | ||
|
||
import io.github.bucket4j.Bandwidth; | ||
import io.github.bucket4j.Bucket; | ||
import io.github.bucket4j.local.LocalBucketBuilder; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.concurrent.Callable; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.ScheduledExecutorService; | ||
|
||
public class BucketUtils { | ||
|
||
/** | ||
* Creates a bucket with the specified bandwidth. | ||
* | ||
* @param limit the bandwidth | ||
* @return the bucket | ||
*/ | ||
@NotNull | ||
public static Bucket createBucket(@NotNull Bandwidth limit) { | ||
return Bucket.builder().addLimit(limit).build(); | ||
} | ||
|
||
/** | ||
* Creates a bucket with the specified bandwidths. | ||
* | ||
* @param limits the bandwidths | ||
* @return the bucket | ||
*/ | ||
@NotNull | ||
public static Bucket createBucket(@NotNull Bandwidth... limits) { | ||
LocalBucketBuilder builder = Bucket.builder(); | ||
for (Bandwidth limit : limits) { | ||
builder.addLimit(limit); | ||
} | ||
return builder.build(); | ||
} | ||
|
||
/** | ||
* Creates a bucket with the specified bandwidths. | ||
* | ||
* @param limits the bandwidths | ||
* @return the bucket | ||
*/ | ||
@NotNull | ||
public static Bucket createBucket(@NotNull Iterable<Bandwidth> limits) { | ||
LocalBucketBuilder builder = Bucket.builder(); | ||
for (Bandwidth limit : limits) { | ||
builder.addLimit(limit); | ||
} | ||
return builder.build(); | ||
} | ||
|
||
/** | ||
* Performs the callable after a token has been consumed from the bucket using the executor. | ||
* <p> | ||
* Note: ExecutionException should be inspected if the passed action can throw an exception. | ||
* | ||
* @param bucket rate limit bucket | ||
* @param executor scheduled executor service for async calls | ||
* @param call task that requires a bucket point | ||
* @return the future result of the call | ||
*/ | ||
@NotNull | ||
public static <T> CompletableFuture<T> scheduleAgainstBucket(@NotNull Bucket bucket, @NotNull ScheduledExecutorService executor, @NotNull Callable<T> call) { | ||
if (bucket.tryConsume(1L)) | ||
return CompletableFuture.supplyAsync(new SneakySupplier<>(call)); | ||
|
||
return bucket.asScheduler().consume(1L, executor).thenApplyAsync(v -> new SneakySupplier<>(call).get()); | ||
} | ||
|
||
/** | ||
* Runs the action after a token has been consumed from the bucket using the executor. | ||
* <p> | ||
* Note: while the executor is used to consume the bucket token, the action is performed on the fork-join common pool, by default. | ||
* | ||
* @param bucket rate limit bucket | ||
* @param executor scheduled executor service for async calls | ||
* @param action runnable that requires a bucket point | ||
* @return a future to track completion progress | ||
*/ | ||
@NotNull | ||
public static CompletableFuture<Void> scheduleAgainstBucket(@NotNull Bucket bucket, @NotNull ScheduledExecutorService executor, @NotNull Runnable action) { | ||
if (bucket.tryConsume(1L)) | ||
return CompletableFuture.runAsync(action); | ||
|
||
return bucket.asScheduler().consume(1L, executor).thenRunAsync(action); | ||
} | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
common/src/main/java/com/github/twitch4j/common/util/SneakySupplier.java
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,32 @@ | ||
package com.github.twitch4j.common.util; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.SneakyThrows; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.concurrent.Callable; | ||
import java.util.function.Supplier; | ||
|
||
/** | ||
* A supplier that can sneakily throw exceptions. | ||
* <p> | ||
* This class should be used sparingly (to avoid hackiness) and carefully (to ensure bubbled exceptions are properly handled). | ||
* | ||
* @param <T> the return type of values provided by the supplier | ||
*/ | ||
@RequiredArgsConstructor | ||
public final class SneakySupplier<T> implements Supplier<T> { | ||
|
||
/** | ||
* The action to compute the supplied value, possibly throwing an exception. | ||
*/ | ||
@NotNull | ||
private final Callable<T> callable; | ||
|
||
@Override | ||
@SneakyThrows | ||
public T get() { | ||
return callable.call(); | ||
} | ||
|
||
} |
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
Oops, something went wrong.