/
TwitchClientPoolBuilder.java
502 lines (442 loc) · 15.9 KB
/
TwitchClientPoolBuilder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
package com.github.twitch4j;
import com.github.philippheuer.credentialmanager.CredentialManager;
import com.github.philippheuer.credentialmanager.CredentialManagerBuilder;
import com.github.philippheuer.credentialmanager.domain.OAuth2Credential;
import com.github.philippheuer.events4j.api.service.IEventHandler;
import com.github.philippheuer.events4j.core.EventManager;
import com.github.philippheuer.events4j.simple.SimpleEventHandler;
import com.github.twitch4j.auth.TwitchAuth;
import com.github.twitch4j.chat.ITwitchChat;
import com.github.twitch4j.chat.TwitchChat;
import com.github.twitch4j.chat.TwitchChatBuilder;
import com.github.twitch4j.chat.TwitchChatConnectionPool;
import com.github.twitch4j.chat.util.TwitchChatLimitHelper;
import com.github.twitch4j.common.annotation.Unofficial;
import com.github.twitch4j.common.config.ProxyConfig;
import com.github.twitch4j.common.config.Twitch4JGlobal;
import com.github.twitch4j.common.util.EventManagerUtils;
import com.github.twitch4j.common.util.ThreadUtils;
import com.github.twitch4j.extensions.TwitchExtensions;
import com.github.twitch4j.extensions.TwitchExtensionsBuilder;
import com.github.twitch4j.graphql.TwitchGraphQL;
import com.github.twitch4j.graphql.TwitchGraphQLBuilder;
import com.github.twitch4j.helix.TwitchHelix;
import com.github.twitch4j.helix.TwitchHelixBuilder;
import com.github.twitch4j.kraken.TwitchKraken;
import com.github.twitch4j.kraken.TwitchKrakenBuilder;
import com.github.twitch4j.pubsub.ITwitchPubSub;
import com.github.twitch4j.pubsub.TwitchPubSub;
import com.github.twitch4j.pubsub.TwitchPubSubBuilder;
import com.github.twitch4j.pubsub.TwitchPubSubConnectionPool;
import com.github.twitch4j.tmi.TwitchMessagingInterface;
import com.github.twitch4j.tmi.TwitchMessagingInterfaceBuilder;
import feign.Logger;
import io.github.bucket4j.Bandwidth;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.With;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ScheduledThreadPoolExecutor;
/**
* Builder to get a TwitchClientPool Instance by provided various options, to provide the user with a lot of customizable options.
*/
@Slf4j
@Getter
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class TwitchClientPoolBuilder {
/**
* Client Id
*/
@With
private String clientId = Twitch4JGlobal.clientId;
/**
* Client Secret
*/
@With
private String clientSecret = Twitch4JGlobal.clientSecret;
/**
* User Agent
*/
@With
private String userAgent = Twitch4JGlobal.userAgent;
/**
* HTTP Request Queue Size
*/
@With
private Integer requestQueueSize = -1;
/**
* Redirect Url
*/
@With
private String redirectUrl = "http://localhost";
/**
* Default Timeout
*/
@With
private Integer timeout = 5000;
/**
* Enabled: Extensions
*/
@With
private Boolean enableExtensions = false;
/**
* Enabled: Helix
*/
@With
private Boolean enableHelix = false;
/**
* Enabled: Kraken
*/
@With
private Boolean enableKraken = false;
/**
* Enabled: TMI
*/
@With
private Boolean enableTMI = false;
/**
* Enabled: Chat
*/
@With
private Boolean enableChat = false;
@With
private Boolean enableChatPool = false;
@With
private int maxChannelsPerChatInstance = 100;
/**
* User IDs of Bot Owners for applying {@link com.github.twitch4j.common.enums.CommandPermission#OWNER}
*/
@Setter
@Accessors(chain = true)
protected Collection<String> botOwnerIds = new HashSet<>();
/**
* IRC Command Handlers
*/
protected Set<String> commandPrefixes = new HashSet<>();
/**
* Enabled: PubSub
*/
@With
private Boolean enablePubSub = false;
@With
private Boolean enablePubSubPool = false;
@With
private int maxTopicsPerPubSubInstance = 50;
/**
* Enabled: GraphQL
* <p>
* This is an unofficial API that is not intended for third-party use. Use at your own risk. Methods could change or stop working at any time.
*/
@With
@Unofficial
private Boolean enableGraphQL = false;
/**
* Chat Account
*/
@With
private OAuth2Credential chatAccount;
/**
* EventManager
*/
@With
private EventManager eventManager = null;
/**
* EventManager
*/
@With
private Class<? extends IEventHandler> defaultEventHandler = SimpleEventHandler.class;
/**
* Size of the ChatQueue
*/
@With
protected Integer chatQueueSize = 200;
/**
* Custom RateLimit for ChatMessages
*/
@With
protected Bandwidth chatRateLimit = TwitchChatLimitHelper.USER_MESSAGE_LIMIT;
/**
* Custom RateLimit for Whispers
*/
@With
protected Bandwidth[] chatWhisperLimit = TwitchChatLimitHelper.USER_WHISPER_LIMIT.toArray(new Bandwidth[2]);
/**
* Custom RateLimit for JOIN/PART
*/
@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
*/
@With
private long chatQueueTimeout = 1000L;
/**
* The maximum number of retries to make for joining each channel, with exponential backoff.
* Set to zero or a negative value to disable this feature.
*/
@With
private int chatMaxJoinRetries = 7;
/**
* Sets the default server used for chat
* <p>
* Defaults to TwitchChat.TWITCH_WEB_SOCKET_SERVER, you can use TwitchChat.FDGT_TEST_SOCKET_SERVER for testing
*/
@With
private String chatServer = TwitchChat.TWITCH_WEB_SOCKET_SERVER;
/**
* The base URL to use for Helix API calls.
* <p>
* Can be adjusted to point to the <a href="https://dev.twitch.tv/docs/cli/mock-api-command">Twitch CLI Mock API</a>, for example.
*
* @see TwitchHelixBuilder#OFFICIAL_BASE_URL
* @see TwitchHelixBuilder#MOCK_BASE_URL
*/
@With
private String helixBaseUrl = TwitchHelixBuilder.OFFICIAL_BASE_URL;
/**
* CredentialManager
*/
@With
private CredentialManager credentialManager = CredentialManagerBuilder.builder().build();
/**
* Scheduler Thread Pool Executor
*/
@With
private ScheduledThreadPoolExecutor scheduledThreadPoolExecutor = null;
/**
* Millisecond Delay for Client Helper Thread
*/
@With
private long helperThreadDelay = 10000L;
/**
* Default Auth Token for API Requests
*/
@With
private OAuth2Credential defaultAuthToken = null;
/**
* Default First-Party OAuth Token for GraphQL calls
*/
@With
private OAuth2Credential defaultFirstPartyToken = null;
/**
* Proxy Configuration
*/
@With
private ProxyConfig proxyConfig = null;
/**
* you can overwrite the feign loglevel to print the full requests + responses if needed
*/
@With
private Logger.Level feignLogLevel = Logger.Level.NONE;
/**
* With a Bot Owner's User ID
*
* @param userId the user id
* @return TwitchClientPoolBuilder
*/
public TwitchClientPoolBuilder withBotOwnerId(String userId) {
this.botOwnerIds.add(userId);
return this;
}
/**
* With a CommandTrigger
*
* @param commandTrigger Command Trigger (Prefix)
* @return TwitchClientPoolBuilder
*/
public TwitchClientPoolBuilder withCommandTrigger(String commandTrigger) {
this.commandPrefixes.add(commandTrigger);
return this;
}
/**
* With a base thread delay for API calls by {@link TwitchClientHelper}
* <p>
* Note: the method name has been a misnomer as it has always set the <i>delay</i> rather than a rate.
* One can change the <i>rate</i> at any time via {@link TwitchClientHelper#setThreadRate(long)}.
*
* @param helperThreadDelay TwitchClientHelper Base Thread Delay
* @return TwitchClientPoolBuilder
* @deprecated in favor of withHelperThreadDelay
*/
@Deprecated
public TwitchClientPoolBuilder withHelperThreadRate(long helperThreadDelay) {
return this.withHelperThreadDelay(helperThreadDelay);
}
/**
* Initialize the builder
*
* @return Twitch Client Pool Builder
*/
public static TwitchClientPoolBuilder builder() {
return new TwitchClientPoolBuilder();
}
/**
* Initialize
*
* @return {@link TwitchClientPool} initialized class
*/
public TwitchClientPool build() {
log.debug("TwitchClientPool: Initializing ErrorTracking ...");
// Module: Auth (registers Twitch Identity Providers)
TwitchAuth.registerIdentityProvider(credentialManager, getClientId(), getClientSecret(), redirectUrl);
// Initialize/Check EventManager
eventManager = EventManagerUtils.validateOrInitializeEventManager(eventManager, defaultEventHandler);
// Determinate required threadPool size
int poolSize = TwitchClientHelper.REQUIRED_THREAD_COUNT;
if (enableChat || enableChatPool)
poolSize = poolSize + TwitchChat.REQUIRED_THREAD_COUNT;
if (enablePubSub || enablePubSubPool)
poolSize = poolSize + TwitchPubSub.REQUIRED_THREAD_COUNT;
// check a provided threadPool or initialize a default one
if (scheduledThreadPoolExecutor != null && scheduledThreadPoolExecutor.getCorePoolSize() < poolSize) {
log.warn("Twitch4J requires a scheduledThreadPoolExecutor with at least {} threads to be fully functional! Some features may not work as expected.", poolSize);
}
if (scheduledThreadPoolExecutor == null) {
if (enableChatPool || enablePubSubPool)
poolSize = Math.max(poolSize, Runtime.getRuntime().availableProcessors());
scheduledThreadPoolExecutor = ThreadUtils.getDefaultScheduledThreadPoolExecutor("twitch4j-" + RandomStringUtils.random(4, true, true), poolSize);
}
// Module: Extensions
TwitchExtensions extensions = null;
if (this.enableExtensions) {
extensions = TwitchExtensionsBuilder.builder()
.withClientId(clientId)
.withClientSecret(clientSecret)
.withUserAgent(userAgent)
.withRequestQueueSize(requestQueueSize)
.withTimeout(timeout)
.withProxyConfig(proxyConfig)
.withLogLevel(feignLogLevel)
.build();
}
// Module: Helix
TwitchHelix helix = null;
if (this.enableHelix) {
helix = TwitchHelixBuilder.builder()
.withBaseUrl(helixBaseUrl)
.withClientId(clientId)
.withClientSecret(clientSecret)
.withUserAgent(userAgent)
.withDefaultAuthToken(defaultAuthToken)
.withRequestQueueSize(requestQueueSize)
.withTimeout(timeout)
.withProxyConfig(proxyConfig)
.withLogLevel(feignLogLevel)
.build();
}
// Module: Kraken
TwitchKraken kraken = null;
if (this.enableKraken) {
kraken = TwitchKrakenBuilder.builder()
.withClientId(clientId)
.withClientSecret(clientSecret)
.withUserAgent(userAgent)
.withRequestQueueSize(requestQueueSize)
.withTimeout(timeout)
.withProxyConfig(proxyConfig)
.withLogLevel(feignLogLevel)
.build();
}
// Module: TMI
TwitchMessagingInterface tmi = null;
if (this.enableTMI) {
tmi = TwitchMessagingInterfaceBuilder.builder()
.withClientId(clientId)
.withClientSecret(clientSecret)
.withUserAgent(userAgent)
.withRequestQueueSize(requestQueueSize)
.withTimeout(timeout)
.withProxyConfig(proxyConfig)
.withLogLevel(feignLogLevel)
.build();
}
// Module: Chat
ITwitchChat chat = null;
if (this.enableChatPool) {
chat = TwitchChatConnectionPool.builder()
.eventManager(eventManager)
.chatAccount(() -> chatAccount)
.chatRateLimit(chatRateLimit)
.whisperRateLimit(chatWhisperLimit)
.joinRateLimit(chatJoinLimit)
.authRateLimit(chatAuthLimit)
.executor(() -> scheduledThreadPoolExecutor)
.proxyConfig(() -> proxyConfig)
.maxSubscriptionsPerConnection(maxChannelsPerChatInstance)
.advancedConfiguration(builder ->
builder.withCredentialManager(credentialManager)
.withChatQueueSize(chatQueueSize)
.withBaseUrl(chatServer)
.withChatQueueTimeout(chatQueueTimeout)
.withMaxJoinRetries(chatMaxJoinRetries)
.setCommandPrefixes(commandPrefixes)
.setBotOwnerIds(botOwnerIds)
)
.build();
} else if (this.enableChat) {
chat = TwitchChatBuilder.builder()
.withEventManager(eventManager)
.withCredentialManager(credentialManager)
.withChatAccount(chatAccount)
.withChatQueueSize(chatQueueSize)
.withChatRateLimit(chatRateLimit)
.withWhisperRateLimit(chatWhisperLimit)
.withJoinRateLimit(chatJoinLimit)
.withAuthRateLimit(chatAuthLimit)
.withScheduledThreadPoolExecutor(scheduledThreadPoolExecutor)
.withBaseUrl(chatServer)
.withChatQueueTimeout(chatQueueTimeout)
.withProxyConfig(proxyConfig)
.withMaxJoinRetries(chatMaxJoinRetries)
.setBotOwnerIds(botOwnerIds)
.setCommandPrefixes(commandPrefixes)
.build();
}
// Module: PubSub
ITwitchPubSub pubSub = null;
if (this.enablePubSubPool) {
pubSub = TwitchPubSubConnectionPool.builder()
.eventManager(eventManager)
.executor(() -> scheduledThreadPoolExecutor)
.proxyConfig(() -> proxyConfig)
.advancedConfiguration(builder -> builder.setBotOwnerIds(botOwnerIds))
.build();
} else if (this.enablePubSub) {
pubSub = TwitchPubSubBuilder.builder()
.withEventManager(eventManager)
.withScheduledThreadPoolExecutor(scheduledThreadPoolExecutor)
.withProxyConfig(proxyConfig)
.setBotOwnerIds(botOwnerIds)
.build();
}
// Module: GraphQL
TwitchGraphQL graphql = null;
if (this.enableGraphQL) {
graphql = TwitchGraphQLBuilder.builder()
.withEventManager(eventManager)
.withDefaultFirstPartyToken(defaultFirstPartyToken)
.withProxyConfig(proxyConfig)
.withTimeout(timeout)
.build();
}
// Module: TwitchClientPool & ClientHelper
final TwitchClientPool client = new TwitchClientPool(eventManager, extensions, helix, kraken, tmi, chat, pubSub, graphql, scheduledThreadPoolExecutor);
client.getClientHelper().setThreadDelay(helperThreadDelay);
// Return new Client Instance
return client;
}
}