Skip to content

Commit

Permalink
feat(gateway): improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
seailz committed Jul 24, 2023
1 parent da31efd commit 293d454
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 20 deletions.
18 changes: 10 additions & 8 deletions src/main/java/com/seailz/discordjar/DiscordJar.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,45 +140,46 @@ public class DiscordJar {

public int gatewayConnections = 0;
public List<GatewayFactory> gatewayFactories = new ArrayList<>();
private boolean newSystemForGatewayMemoryManagement = false;

/**
* @deprecated Use {@link DiscordJarBuilder} instead.
*/
@Deprecated(forRemoval = true)
public DiscordJar(String token, EnumSet<Intent> intents, APIVersion version) throws ExecutionException, InterruptedException {
this(token, intents, version, false, null, false, -1, -1, APIRelease.STABLE, EnumSet.of(CacheType.ALL));
this(token, intents, version, false, null, false, -1, -1, APIRelease.STABLE, EnumSet.of(CacheType.ALL), false);
}

/**
* @deprecated Use {@link DiscordJarBuilder} instead.
*/
@Deprecated(forRemoval = true)
public DiscordJar(String token, EnumSet<Intent> intents, APIVersion version, boolean debug) throws ExecutionException, InterruptedException {
this(token, intents, version, false, null, debug, -1, -1, APIRelease.STABLE, EnumSet.of(CacheType.ALL));
this(token, intents, version, false, null, debug, -1, -1, APIRelease.STABLE, EnumSet.of(CacheType.ALL), false);
}

/**
* @deprecated Use {@link DiscordJarBuilder} instead.
*/
@Deprecated(forRemoval = true)
public DiscordJar(String token, APIVersion version) throws ExecutionException, InterruptedException {
this(token, EnumSet.of(Intent.ALL), version, false, null, false, -1, -1, APIRelease.STABLE, EnumSet.of(CacheType.ALL));
this(token, EnumSet.of(Intent.ALL), version, false, null, false, -1, -1, APIRelease.STABLE, EnumSet.of(CacheType.ALL), false);
}

/**
* @deprecated Use {@link DiscordJarBuilder} instead.
*/
@Deprecated(forRemoval = true)
public DiscordJar(String token, APIVersion version, boolean httpOnly, HTTPOnlyInfo httpOnlyInfo) throws ExecutionException, InterruptedException {
this(token, EnumSet.noneOf(Intent.class), version, httpOnly, httpOnlyInfo, false, -1, -1, APIRelease.STABLE, EnumSet.of(CacheType.ALL));
this(token, EnumSet.noneOf(Intent.class), version, httpOnly, httpOnlyInfo, false, -1, -1, APIRelease.STABLE, EnumSet.of(CacheType.ALL), false);
}

/**
* @deprecated Use {@link DiscordJarBuilder} instead.
*/
@Deprecated(forRemoval = true)
public DiscordJar(String token, boolean httpOnly, HTTPOnlyInfo httpOnlyInfo) throws ExecutionException, InterruptedException {
this(token, EnumSet.noneOf(Intent.class), APIVersion.getLatest(), httpOnly, httpOnlyInfo, false, -1, -1, APIRelease.STABLE, EnumSet.of(CacheType.ALL));
this(token, EnumSet.noneOf(Intent.class), APIVersion.getLatest(), httpOnly, httpOnlyInfo, false, -1, -1, APIRelease.STABLE, EnumSet.of(CacheType.ALL), false);
}

/**
Expand All @@ -204,7 +205,7 @@ public DiscordJar(String token, boolean httpOnly, HTTPOnlyInfo httpOnlyInfo) thr
* @deprecated Use {@link DiscordJarBuilder} instead. This constructor will be set to protected in the future.
*/
@Deprecated
public DiscordJar(String token, EnumSet<Intent> intents, APIVersion version, boolean httpOnly, HTTPOnlyInfo httpOnlyInfo, boolean debug, int shardId, int numShards, APIRelease release, EnumSet<CacheType> cacheTypes) throws ExecutionException, InterruptedException {
public DiscordJar(String token, EnumSet<Intent> intents, APIVersion version, boolean httpOnly, HTTPOnlyInfo httpOnlyInfo, boolean debug, int shardId, int numShards, APIRelease release, EnumSet<CacheType> cacheTypes, boolean newSystemForGatewayMemoryManagement) throws ExecutionException, InterruptedException {
System.setProperty("sun.net.http.allowRestrictedHeaders", "true");
new RequestQueueHandler(this);
this.token = token;
Expand All @@ -216,8 +217,9 @@ public DiscordJar(String token, EnumSet<Intent> intents, APIVersion version, boo
this.queuedRequests = new ArrayList<>();
this.buckets = new ArrayList<>();
if (!httpOnly) {
this.gatewayFactory = new GatewayFactory(this, debug, shardId, numShards);
this.gatewayFactory = new GatewayFactory(this, debug, shardId, numShards, newSystemForGatewayMemoryManagement);
}
this.newSystemForGatewayMemoryManagement = newSystemForGatewayMemoryManagement;
this.debug = debug;
this.guildCache = new Cache<>(this, Guild.class,
new DiscordRequest(
Expand Down Expand Up @@ -350,7 +352,7 @@ public void restartGateway() {
} catch (InterruptedException ignored) {}
killGateway();
try {
gatewayFactory = new GatewayFactory(this, debug, shardId, numShards);
gatewayFactory = new GatewayFactory(this, debug, shardId, numShards, newSystemForGatewayMemoryManagement);
gatewayFactories.add(gatewayFactory);
} catch (ExecutionException | InterruptedException e) {
throw new RuntimeException(e);
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/com/seailz/discordjar/DiscordJarBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class DiscordJarBuilder {
private boolean debug;
private int shardId = -1;
private int numShards = 1;
private boolean newSystemForGatewayMemoryManagement = false;

public DiscordJarBuilder(String token) {
this.token = token;
Expand All @@ -48,6 +49,10 @@ public DiscordJarBuilder setCacheTypes(EnumSet<CacheType> cacheTypes) {
return this;
}

public void setIsNewSystemForGatewayMemoryManagement(boolean newSystemForGatewayMemoryManagement) {
this.newSystemForGatewayMemoryManagement = newSystemForGatewayMemoryManagement;
}

/**
* Resets back to default intents.
*/
Expand Down Expand Up @@ -172,7 +177,7 @@ public DiscordJar build() {
if (cacheTypes == null) defaultCacheTypes();
if (httpOnly && httpOnlyInfo == null) throw new IllegalStateException("HTTPOnly is enabled but no HTTPOnlyInfo was provided.");
try {
return new DiscordJar(token, intents, apiVersion, httpOnly, httpOnlyInfo, debug, shardId, numShards, apiRelease, cacheTypes);
return new DiscordJar(token, intents, apiVersion, httpOnly, httpOnlyInfo, debug, shardId, numShards, apiRelease, cacheTypes, newSystemForGatewayMemoryManagement);
} catch (ExecutionException | InterruptedException e) {
throw new RuntimeException(e);
}
Expand Down
31 changes: 20 additions & 11 deletions src/main/java/com/seailz/discordjar/gateway/GatewayFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,19 @@ public class GatewayFactory extends TextWebSocketHandler {
private boolean readyForMessages = false;
public HashMap<String, GatewayFactory.MemberChunkStorageWrapper> memberRequestChunks = new HashMap<>();
private final boolean debug;
private final boolean newSystemForMemoryManagement;
public UUID uuid = UUID.randomUUID();
private int shardId;
private int numShards;
public static List<Long> pingHistoryMs = new ArrayList<>();
public static Date lastHeartbeatSent = new Date();

public GatewayFactory(DiscordJar discordJar, boolean debug, int shardId, int numShards) throws ExecutionException, InterruptedException {
public GatewayFactory(DiscordJar discordJar, boolean debug, int shardId, int numShards, boolean newSystemForMemoryManagement) throws ExecutionException, InterruptedException {
this.discordJar = discordJar;
this.debug = debug;
this.shardId = shardId;
this.numShards = numShards;
this.newSystemForMemoryManagement = newSystemForMemoryManagement;

discordJar.setGatewayFactory(this);

Expand All @@ -95,17 +97,24 @@ public void connect(String customUrl) throws ExecutionException, InterruptedExce
WebSocketClient client = new StandardWebSocketClient();
this.client = client;
this.session = client.execute(this, new WebSocketHttpHeaders(), URI.create(customUrl + "?v=" + URLS.version.getCode())).get();
// Allocate 50% of available memory to the JVM
long maxMemory = Runtime.getRuntime().maxMemory();
long allocatedMemory = Runtime.getRuntime().totalMemory();
long freeMemory = Runtime.getRuntime().freeMemory();
long usedMemory = allocatedMemory - freeMemory;
int totalFreeMemory = (int) ((freeMemory + (maxMemory - usedMemory)) / 2);
if (debug) {
logger.info("[DISCORD.JAR - DEBUG] Allocated " + totalFreeMemory + " bytes of memory to the Gateway.");
// Allocate 25% of the bot's total **AVAILABLE** memory to the gateway.
if (newSystemForMemoryManagement) {
Runtime runtime = Runtime.getRuntime();
long allocatedMemory = runtime.totalMemory() - runtime.freeMemory();
long presumableFreeMemory = runtime.maxMemory() - allocatedMemory;
int totalFreeMemory = (int) (presumableFreeMemory * 0.25);

if (debug) {
logger.info("[DISCORD.JAR - DEBUG] Allocated " + totalFreeMemory + " bytes of memory to the Gateway.");
}

session.setTextMessageSizeLimit(totalFreeMemory);
session.setBinaryMessageSizeLimit(totalFreeMemory);
} else {
session.setTextMessageSizeLimit(100000000);
session.setBinaryMessageSizeLimit(100000000);
}
session.setTextMessageSizeLimit(totalFreeMemory);
session.setBinaryMessageSizeLimit(totalFreeMemory);

if (debug) {
logger.info("[DISCORD.JAR - DEBUG] Gateway connection established.");
}
Expand Down

0 comments on commit 293d454

Please sign in to comment.