Skip to content

Commit

Permalink
feat: implement the irc auth rate limit (#486)
Browse files Browse the repository at this point in the history
  • Loading branch information
iProdigy committed Dec 19, 2021
1 parent 659e449 commit c32c023
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 8 deletions.
15 changes: 13 additions & 2 deletions chat/src/main/java/com/github/twitch4j/chat/TwitchChat.java
Expand Up @@ -28,7 +28,6 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -135,6 +134,11 @@ public class TwitchChat implements ITwitchChat {
*/
protected final Bucket ircJoinBucket;

/**
* IRC Auth Bucket
*/
protected final Bucket ircAuthBucket;

/**
* IRC Command Queue
*/
Expand Down Expand Up @@ -232,14 +236,15 @@ public class TwitchChat implements ITwitchChat {
* @param ircMessageBucket Bucket for chat
* @param ircWhisperBucket Bucket for whispers
* @param ircJoinBucket Bucket for joins
* @param ircAuthBucket Bucket for auths
* @param taskExecutor ScheduledThreadPoolExecutor
* @param chatQueueTimeout Timeout to wait for events in Chat Queue
* @param proxyConfig Proxy Configuration
* @param autoJoinOwnChannel Whether one's own channel should automatically be joined
* @param enableMembershipEvents Whether JOIN/PART events should be enabled
* @param botOwnerIds Bot Owner IDs
*/
public TwitchChat(EventManager eventManager, CredentialManager credentialManager, OAuth2Credential chatCredential, String baseUrl, boolean sendCredentialToThirdPartyHost, List<String> commandPrefixes, Integer chatQueueSize, Bucket ircMessageBucket, Bucket ircWhisperBucket, Bucket ircJoinBucket, ScheduledThreadPoolExecutor taskExecutor, long chatQueueTimeout, ProxyConfig proxyConfig, boolean autoJoinOwnChannel, boolean enableMembershipEvents, Collection<String> botOwnerIds) {
public TwitchChat(EventManager eventManager, CredentialManager credentialManager, OAuth2Credential chatCredential, String baseUrl, boolean sendCredentialToThirdPartyHost, List<String> commandPrefixes, Integer chatQueueSize, Bucket ircMessageBucket, Bucket ircWhisperBucket, Bucket ircJoinBucket, Bucket ircAuthBucket, ScheduledThreadPoolExecutor taskExecutor, long chatQueueTimeout, ProxyConfig proxyConfig, boolean autoJoinOwnChannel, boolean enableMembershipEvents, Collection<String> botOwnerIds) {
this.eventManager = eventManager;
this.credentialManager = credentialManager;
this.chatCredential = chatCredential;
Expand All @@ -251,6 +256,7 @@ public TwitchChat(EventManager eventManager, CredentialManager credentialManager
this.ircMessageBucket = ircMessageBucket;
this.ircWhisperBucket = ircWhisperBucket;
this.ircJoinBucket = ircJoinBucket;
this.ircAuthBucket = ircAuthBucket;
this.taskExecutor = taskExecutor;
this.chatQueueTimeout = chatQueueTimeout;
this.autoJoinOwnChannel = autoJoinOwnChannel;
Expand Down Expand Up @@ -359,6 +365,11 @@ public TwitchChat(EventManager eventManager, CredentialManager credentialManager
@Synchronized
public void connect() {
if (connectionState.equals(TMIConnectionState.DISCONNECTED) || connectionState.equals(TMIConnectionState.RECONNECTING)) {
if (chatCredential != null) {
// Wait for AUTH limit before opening the connection
ircAuthBucket.asBlocking().consumeUninterruptibly(1L);
}

try {
// Change Connection State
connectionState = TMIConnectionState.CONNECTING;
Expand Down
Expand Up @@ -135,6 +135,12 @@ public class TwitchChatBuilder {
@With
protected Bandwidth joinRateLimit = TwitchChatLimitHelper.USER_JOIN_LIMIT;

/**
* Custom RateLimit for AUTH
*/
@With
protected Bandwidth authRateLimit = TwitchChatLimitHelper.USER_AUTH_LIMIT;

/**
* Shared bucket for messages
*/
Expand All @@ -153,6 +159,12 @@ public class TwitchChatBuilder {
@With
protected Bucket ircJoinBucket = null;

/**
* Shared bucket for auths
*/
@With
protected Bucket ircAuthBucket = null;

/**
* Scheduler Thread Pool Executor
*/
Expand Down Expand Up @@ -228,8 +240,11 @@ public TwitchChat build() {
if (ircJoinBucket == null)
ircJoinBucket = userId == null ? TwitchChatLimitHelper.createBucket(this.joinRateLimit) : TwitchLimitRegistry.getInstance().getOrInitializeBucket(userId, TwitchLimitType.CHAT_JOIN_LIMIT, Collections.singletonList(joinRateLimit));

if (ircAuthBucket == null)
ircAuthBucket = userId == null ? TwitchChatLimitHelper.createBucket(this.authRateLimit) : TwitchLimitRegistry.getInstance().getOrInitializeBucket(userId, TwitchLimitType.CHAT_AUTH_LIMIT, Collections.singletonList(authRateLimit));

log.debug("TwitchChat: Initializing Module ...");
return new TwitchChat(this.eventManager, this.credentialManager, this.chatAccount, this.baseUrl, this.sendCredentialToThirdPartyHost, this.commandPrefixes, this.chatQueueSize, this.ircMessageBucket, this.ircWhisperBucket, this.ircJoinBucket, this.scheduledThreadPoolExecutor, this.chatQueueTimeout, this.proxyConfig, this.autoJoinOwnChannel, this.enableMembershipEvents, this.botOwnerIds);
return new TwitchChat(this.eventManager, this.credentialManager, this.chatAccount, this.baseUrl, this.sendCredentialToThirdPartyHost, this.commandPrefixes, this.chatQueueSize, this.ircMessageBucket, this.ircWhisperBucket, this.ircJoinBucket, this.ircAuthBucket, this.scheduledThreadPoolExecutor, this.chatQueueTimeout, this.proxyConfig, this.autoJoinOwnChannel, this.enableMembershipEvents, this.botOwnerIds);
}

/**
Expand Down
Expand Up @@ -85,6 +85,12 @@ public class TwitchChatConnectionPool extends TwitchModuleConnectionPool<TwitchC
@Builder.Default
protected Bandwidth joinRateLimit = TwitchChatLimitHelper.USER_JOIN_LIMIT;

/**
* Custom RateLimit for AUTH
*/
@Builder.Default
protected Bandwidth authRateLimit = TwitchChatLimitHelper.USER_AUTH_LIMIT;

@Override
public boolean sendMessage(String channel, String message, @Nullable Map<String, Object> tags) {
return this.sendMessage(channel, channel, message, tags);
Expand Down Expand Up @@ -245,6 +251,7 @@ protected TwitchChat createConnection() {
.withChatRateLimit(chatRateLimit)
.withWhisperRateLimit(whisperRateLimit)
.withJoinRateLimit(joinRateLimit)
.withAuthRateLimit(authRateLimit)
.withAutoJoinOwnChannel(false) // user will have to manually send a subscribe call to enable whispers. this avoids duplicating whisper events
).build();

Expand Down
Expand Up @@ -11,8 +11,6 @@ public enum TwitchLimitType {

/**
* How fast authentication attempts can be issued over IRC.
* <p>
* Note: this limit is <i>not</i> currently implemented elsewhere in the library.
*/
CHAT_AUTH_LIMIT("irc-auth-limit"),

Expand Down
Expand Up @@ -36,7 +36,6 @@

import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ScheduledThreadPoolExecutor;

Expand Down Expand Up @@ -184,6 +183,12 @@ public class TwitchClientBuilder {
@With
protected Bandwidth chatJoinLimit = TwitchChatLimitHelper.USER_JOIN_LIMIT;

/**
* Custom RateLimit for AUTH
*/
@With
protected Bandwidth chatAuthLimit = TwitchChatLimitHelper.USER_AUTH_LIMIT;

/**
* Wait time for taking items off chat queue in milliseconds. Default recommended
*/
Expand Down Expand Up @@ -384,6 +389,7 @@ public TwitchClient build() {
.withChatRateLimit(chatRateLimit)
.withWhisperRateLimit(chatWhisperLimit)
.withJoinRateLimit(chatJoinLimit)
.withAuthRateLimit(chatAuthLimit)
.withScheduledThreadPoolExecutor(scheduledThreadPoolExecutor)
.withBaseUrl(chatServer)
.withChatQueueTimeout(chatQueueTimeout)
Expand Down
Expand Up @@ -44,9 +44,7 @@
import org.apache.commons.lang3.RandomStringUtils;

import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ScheduledThreadPoolExecutor;

Expand Down Expand Up @@ -206,6 +204,12 @@ public class TwitchClientPoolBuilder {
@With
protected Bandwidth chatJoinLimit = TwitchChatLimitHelper.USER_JOIN_LIMIT;

/**
* Custom RateLimit for AUTH
*/
@With
protected Bandwidth chatAuthLimit = TwitchChatLimitHelper.USER_AUTH_LIMIT;

/**
* Wait time for taking items off chat queue in milliseconds. Default recommended
*/
Expand Down Expand Up @@ -405,6 +409,7 @@ public TwitchClientPool build() {
.chatRateLimit(chatRateLimit)
.whisperRateLimit(chatWhisperLimit)
.joinRateLimit(chatJoinLimit)
.authRateLimit(chatAuthLimit)
.executor(() -> scheduledThreadPoolExecutor)
.proxyConfig(() -> proxyConfig)
.maxSubscriptionsPerConnection(maxChannelsPerChatInstance)
Expand All @@ -426,6 +431,7 @@ public TwitchClientPool build() {
.withChatRateLimit(chatRateLimit)
.withWhisperRateLimit(chatWhisperLimit)
.withJoinRateLimit(chatJoinLimit)
.withAuthRateLimit(chatAuthLimit)
.withScheduledThreadPoolExecutor(scheduledThreadPoolExecutor)
.withBaseUrl(chatServer)
.withChatQueueTimeout(chatQueueTimeout)
Expand Down

0 comments on commit c32c023

Please sign in to comment.