Interface SessionController
-
- All Known Implementing Classes:
ConcurrentSessionController
,SessionControllerAdapter
public interface SessionController
Controls states and behaviour of one or multipleJDA
instances.
One instance of this should be used when sharding a bot account in order to keep track of session information between shards.The
SessionControllerAdapter
provides a default implementation that can be extended and overridden.States & Behaviour
Identify Ratelimit Handling
This will enable handling of (re-)connecting gateway sessions.Global REST Ratelimit
The global REST ratelimit is not bound to a single session and should be handled on all JDA instances. This controller will receive updates of this ratelimit throughsetGlobalRatelimit(long)
and should report the last ratelimit information it received throughgetGlobalRatelimit()
.Gateway Provider
This provider can be used to change the gateway retrieval (using cache, http, or static) and allows to set a custom gateway endpoint. Use carefully.Examples
UsingJDABuilder
JDABuilder builder = JDABuilder.createDefault(BOT_TOKEN); builder.setSessionController(new SessionControllerAdapter() { @Override public void appendSession(SessionConnectNode node) { System.out.println("[SessionController] Adding SessionConnectNode to Queue!"); super.appendSession(node); } }); builder.addEventListeners(myListener); for (int i = 0; i < 10; i++) { builder.useSharding(i, 10).build(); }
Using
ShardManager
DefaultShardManagerBuilder builder = DefaultShardManagerBuilder.createDefault(BOT_TOKEN); builder.setSessionController(new SessionControllerAdapter() { @Override public Pair<String, Integer> getGatewayBot(JDA api) { return Pair.of(getGateway(), 10); } }); builder.addEventListeners(myListener); builder.build();
-
-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static interface
SessionController.SessionConnectNode
Represents a WebSocketClient request to start a session.static class
SessionController.ShardedGateway
POJO containing the gateway endpoint and recommended shard total for a shard manager.
-
Field Summary
Fields Modifier and Type Field Description static int
IDENTIFY_DELAY
The default delay (in seconds) to wait between runningSessionConnectNodes
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Deprecated Methods Modifier and Type Method Description void
appendSession(SessionController.SessionConnectNode node)
Called by a JDA session when a WebSocket should be started.java.lang.String
getGateway(JDA api)
Called by a JDA session when a new gateway session starts (Connecting, Reconnecting).net.dv8tion.jda.internal.utils.tuple.Pair<java.lang.String,java.lang.Integer>
getGatewayBot(JDA api)
Deprecated.UsegetShardedGateway(JDA)
instead, an implementation for this is ignored ifgetShardedGateway(JDA)
is implemented instead.long
getGlobalRatelimit()
Provides the cross-session global REST ratelimit it received throughsetGlobalRatelimit(long)
.default SessionController.ShardedGateway
getShardedGateway(JDA api)
Called byDefaultShardManager
when a new shards is starting.void
removeSession(SessionController.SessionConnectNode node)
Called by a JDA session when a shutdown has been requested.default void
setConcurrency(int level)
Apply themax_concurrency
for this bot.void
setGlobalRatelimit(long ratelimit)
Called by the RateLimiter if the global rest ratelimit has changed.
-
-
-
Field Detail
-
IDENTIFY_DELAY
static final int IDENTIFY_DELAY
The default delay (in seconds) to wait between runningSessionConnectNodes
- See Also:
- Constant Field Values
-
-
Method Detail
-
setConcurrency
default void setConcurrency(int level)
Apply themax_concurrency
for this bot. This property is only useful for very large bots which get access to higher concurrency when starting their shards.Currently, there are 3 different levels of concurrency 1, 16, and 64. The concurrency means the bot can connect multiple shards at once without hitting the IDENTIFY rate-limit. This works by applying the concurrency level as a modulo operand to the shard id:
shard_id % concurrency
. We use one thread per bucket in this implementation.An implementation of this interface is not required to use this concurrency level.
SessionControllerAdapter
does not support this due to backwards compatibility.- Parameters:
level
- The concurrency level- Throws:
java.lang.AssertionError
- If the provided level is not a valid array length size- Since:
- 4.2.0
-
appendSession
void appendSession(@Nonnull SessionController.SessionConnectNode node)
Called by a JDA session when a WebSocket should be started. (Connecting and Reconnecting)
This should only add the node to a queue and execute the queue with respect to theIDENTIFY_DELAY
.- Parameters:
node
- TheSessionConnectNode
-
removeSession
void removeSession(@Nonnull SessionController.SessionConnectNode node)
Called by a JDA session when a shutdown has been requested.
When this happened theSessionConnectNode.run(boolean)
will be a no-op and does not contribute to theIDENTIFY_DELAY
.- Parameters:
node
- TheSessionConnectNode
to remove from the queue.
-
getGlobalRatelimit
long getGlobalRatelimit()
Provides the cross-session global REST ratelimit it received throughsetGlobalRatelimit(long)
.- Returns:
- The current global REST ratelimit or -1 if unset
-
setGlobalRatelimit
void setGlobalRatelimit(long ratelimit)
Called by the RateLimiter if the global rest ratelimit has changed.- Parameters:
ratelimit
- The new global ratelimit
-
getGateway
@Nonnull java.lang.String getGateway(@Nonnull JDA api)
Called by a JDA session when a new gateway session starts (Connecting, Reconnecting).
Should provide the gateway endpoint (wss) to connect to.- Parameters:
api
- The current JDA instance (used for RestActions and ShardInfo)- Returns:
- The gateway endpoint
-
getGatewayBot
@Nonnull @Deprecated @ForRemoval(deadline="4.4.0") @DeprecatedSince("4.0.0") @ReplaceWith("getShardedGateway(api)") net.dv8tion.jda.internal.utils.tuple.Pair<java.lang.String,java.lang.Integer> getGatewayBot(@Nonnull JDA api)
Deprecated.UsegetShardedGateway(JDA)
instead, an implementation for this is ignored ifgetShardedGateway(JDA)
is implemented instead.Called byDefaultShardManager
when a new shards is starting.
Should provide aPair
with(gateway, shardTotal)
.- Parameters:
api
- The current JDA instance (used for RestActions and ShardInfo)- Returns:
- The Pair consisting of the gateway endpoint to connect to and the shardTotal
- See Also:
getGateway(net.dv8tion.jda.api.JDA)
-
getShardedGateway
@Nonnull default SessionController.ShardedGateway getShardedGateway(@Nonnull JDA api)
Called byDefaultShardManager
when a new shards is starting.
Should provide aSessionController.ShardedGateway
with(gateway, shardTotal)
.- Parameters:
api
- The current JDA instance (used for RestActions and ShardInfo)- Returns:
- The ShardedGateway instance consisting of the gateway endpoint to connect to and the shardTotal
- See Also:
getGateway(net.dv8tion.jda.api.JDA)
-
-