Skip to content

Commit 8f5558c

Browse files
WebSocketClient optimization/enhancements (discord-jda#645)
* Optimize gc time * Cleaned up WebSocketClient * Invalidate gateway url when we encounter an exception on connect() * Use AtomicInteger for rate-limit counter * Make everything protected rather than private * Added switch to disable stream compression * Moved some things around for better event handling
1 parent 3685c6f commit 8f5558c

File tree

5 files changed

+205
-128
lines changed

5 files changed

+205
-128
lines changed

src/main/java/net/dv8tion/jda/bot/sharding/DefaultShardManager.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,11 @@ public class DefaultShardManager implements ShardManager
206206
*/
207207
protected boolean enableMDC;
208208

209+
/**
210+
* Whether to enable transport compression
211+
*/
212+
protected boolean enableCompression;
213+
209214
/**
210215
* Creates a new DefaultShardManager instance.
211216
* @param shardsTotal
@@ -259,6 +264,8 @@ public class DefaultShardManager implements ShardManager
259264
* Whether MDC should be enabled
260265
* @param contextProvider
261266
* The MDC context provider new JDA instances should use on startup
267+
* @param enableCompression
268+
* Whether to enable transport compression
262269
*/
263270
protected DefaultShardManager(final int shardsTotal, final Collection<Integer> shardIds,
264271
final SessionController controller, final List<Object> listeners,
@@ -271,7 +278,8 @@ protected DefaultShardManager(final int shardsTotal, final Collection<Integer> s
271278
final boolean enableShutdownHook, final boolean enableBulkDeleteSplitting,
272279
final boolean autoReconnect, final IntFunction<Boolean> idleProvider,
273280
final boolean retryOnTimeout, final boolean useShutdownNow,
274-
final boolean enableMDC, final IntFunction<ConcurrentMap<String, String>> contextProvider)
281+
final boolean enableMDC, final IntFunction<ConcurrentMap<String, String>> contextProvider,
282+
final boolean enableCompression)
275283
{
276284
this.shardsTotal = shardsTotal;
277285
this.listeners = listeners;
@@ -296,6 +304,7 @@ protected DefaultShardManager(final int shardsTotal, final Collection<Integer> s
296304
this.useShutdownNow = useShutdownNow;
297305
this.contextProvider = contextProvider;
298306
this.enableMDC = enableMDC;
307+
this.enableCompression = enableCompression;
299308

300309
synchronized (queue)
301310
{
@@ -626,7 +635,7 @@ protected JDAImpl buildInstance(final int shardId) throws LoginException, Interr
626635

627636
final JDA.ShardInfo shardInfo = new JDA.ShardInfo(shardId, this.shardsTotal);
628637

629-
final int shardTotal = jda.login(this.gatewayURL, shardInfo);
638+
final int shardTotal = jda.login(this.gatewayURL, shardInfo, this.enableCompression);
630639
if (this.shardsTotal == -1)
631640
this.shardsTotal = shardTotal;
632641

src/main/java/net/dv8tion/jda/bot/sharding/DefaultShardManagerBuilder.java

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,7 @@
2525
import okhttp3.OkHttpClient;
2626

2727
import javax.security.auth.login.LoginException;
28-
import java.util.ArrayList;
29-
import java.util.Arrays;
30-
import java.util.Collection;
31-
import java.util.Collections;
32-
import java.util.List;
28+
import java.util.*;
3329
import java.util.concurrent.ConcurrentMap;
3430
import java.util.concurrent.ThreadFactory;
3531
import java.util.function.IntFunction;
@@ -57,6 +53,7 @@ public class DefaultShardManagerBuilder
5753
protected boolean autoReconnect = true;
5854
protected boolean retryOnTimeout = true;
5955
protected boolean useShutdownNow = false;
56+
protected boolean enableCompression = true;
6057
protected int shardsTotal = -1;
6158
protected int maxReconnectDelay = 900;
6259
protected int corePoolSize = 2;
@@ -140,6 +137,28 @@ public DefaultShardManagerBuilder setContextEnabled(boolean enable)
140137
return this;
141138
}
142139

140+
/**
141+
* Enable stream-compression on the gateway connection,
142+
* this will decrease the amount of used bandwidth for the running bot instance
143+
* for the cost of a few extra cycles for decompression.
144+
* <br><b>Default: true</b>
145+
*
146+
* <p><b>We recommend to keep this enabled unless you have issues with the decompression</b>
147+
* <br>This mode might become obligatory in a future version, do not rely on this switch to stay.
148+
*
149+
* @param enable
150+
* True, if the gateway connection should use compression
151+
*
152+
* @return The {@link net.dv8tion.jda.bot.sharding.DefaultShardManagerBuilder DefaultShardManagerBuilder} instance. Useful for chaining.
153+
*
154+
* @see <a href="https://discordapp.com/developers/docs/topics/gateway#transport-compression" target="_blank">Official Discord Documentation - Transport Compression</a>
155+
*/
156+
public DefaultShardManagerBuilder setCompressionEnabled(boolean enable)
157+
{
158+
this.enableCompression = enable;
159+
return this;
160+
}
161+
143162
/**
144163
* Adds all provided listeners to the list of listeners that will be used to populate the {@link DefaultShardManager DefaultShardManager} object.
145164
* <br>This uses the {@link net.dv8tion.jda.core.hooks.InterfacedEventManager InterfacedEventListener} by default.
@@ -813,7 +832,7 @@ public ShardManager build() throws LoginException, IllegalArgumentException
813832
this.audioSendFactory, this.gameProvider, this.statusProvider,
814833
this.httpClientBuilder, this.wsFactory, this.threadFactory,
815834
this.maxReconnectDelay, this.corePoolSize, this.enableVoice, this.enableShutdownHook, this.enableBulkDeleteSplitting,
816-
this.autoReconnect, this.idleProvider, this.retryOnTimeout, this.useShutdownNow, this.enableContext, this.contextProvider);
835+
this.autoReconnect, this.idleProvider, this.retryOnTimeout, this.useShutdownNow, this.enableContext, this.contextProvider, this.enableCompression);
817836

818837
manager.login();
819838

src/main/java/net/dv8tion/jda/core/JDABuilder.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public class JDABuilder
7070
protected boolean autoReconnect = true;
7171
protected boolean idle = false;
7272
protected boolean requestTimeoutRetry = true;
73+
protected boolean enableCompression = true;
7374

7475
/**
7576
* Creates a completely empty JDABuilder.
@@ -134,6 +135,28 @@ public JDABuilder setContextEnabled(boolean enable)
134135
return this;
135136
}
136137

138+
/**
139+
* Enable stream-compression on the gateway connection,
140+
* this will decrease the amount of used bandwidth for the running bot instance
141+
* for the cost of a few extra cycles for decompression.
142+
* <br><b>Default: true</b>
143+
*
144+
* <p><b>We recommend to keep this enabled unless you have issues with the decompression</b>
145+
* <br>This mode might become obligatory in a future version, do not rely on this switch to stay.
146+
*
147+
* @param enable
148+
* True, if the gateway connection should use compression
149+
*
150+
* @return The JDABuilder instance. Useful for chaining
151+
*
152+
* @see <a href="https://discordapp.com/developers/docs/topics/gateway#transport-compression" target="_blank">Official Discord Documentation - Transport Compression</a>
153+
*/
154+
public JDABuilder setCompressionEnabled(boolean enable)
155+
{
156+
this.enableCompression = enable;
157+
return this;
158+
}
159+
137160
/**
138161
* Whether the Requester should retry when
139162
* a {@link java.net.SocketTimeoutException SocketTimeoutException} occurs.
@@ -582,7 +605,7 @@ public JDA buildAsync() throws LoginException
582605
.setCacheGame(game)
583606
.setCacheIdle(idle)
584607
.setCacheStatus(status);
585-
jda.login(gateway, shardInfo);
608+
jda.login(gateway, shardInfo, enableCompression);
586609
return jda;
587610
}
588611

src/main/java/net/dv8tion/jda/core/entities/impl/JDAImpl.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public SessionController getSessionController()
136136
return sessionController;
137137
}
138138

139-
public int login(String gatewayUrl, ShardInfo shardInfo) throws LoginException
139+
public int login(String gatewayUrl, ShardInfo shardInfo, boolean compression) throws LoginException
140140
{
141141
this.gatewayUrl = gatewayUrl;
142142
this.shardInfo = shardInfo;
@@ -161,7 +161,7 @@ public int login(String gatewayUrl, ShardInfo shardInfo) throws LoginException
161161
verifyToken();
162162
LOG.info("Login Successful!");
163163

164-
client = new WebSocketClient(this);
164+
client = new WebSocketClient(this, compression);
165165
// remove our MDC metadata when we exit our code
166166
if (previousContext != null)
167167
previousContext.forEach(MDC::put);
@@ -773,4 +773,9 @@ public String getGatewayUrl()
773773
{
774774
return gatewayUrl;
775775
}
776+
777+
public void resetGatewayUrl()
778+
{
779+
this.gatewayUrl = getGateway();
780+
}
776781
}

0 commit comments

Comments
 (0)