Skip to content

Commit 78ee8f8

Browse files
Introducing SessionController System (discord-jda#562)
This should make sharding a lot easier
1 parent 9149723 commit 78ee8f8

16 files changed

+833
-232
lines changed

README.md

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -103,42 +103,40 @@ To use sharding in JDA you will need to use `JDABuilder.useSharding(int shardId,
103103
has the ID 0. The **shardTotal** is the total amount of shards (not 0-based) which can be seen similar to the length of an array, the last shard has the ID of
104104
`shardTotal - 1`.
105105

106-
When using sharding it is also recommended to use a `SessionReconnectQueue` instance for all shards. This allows JDA to properly
107-
handle reconnecting multiple shards without violating Discord limitations (not using this might cause an IP ban on bad days).
106+
The [`SessionController`](https://home.dv8tion.net:8080/job/JDA/javadoc/net/dv8tion/jda/core/utils/SessionController.html) is a tool of the JDABuilder
107+
that allows to control state and behaviour between shards (sessions). When using multiple builders to build shards you have to create one instance
108+
of this controller and add the same instance to each builder: `builder.setSessionController(controller)`
108109

109-
Additionally to keep track of the global REST rate-limit JDA has a `ShardedRateLimiter` which is set by default when using the same JDABuilder
110-
for all shards. If you want to use multiple builders to build your shards you should use the same ShardedRateLimiter instance!
110+
Since version **3.4.0** JDA provides a `ShardManager` which automates this building process.
111111

112-
**Logins between shards _must_ happen with a minimum of _5 SECONDS_ of backoff time. JDA will inform you if the backoff is too short
113-
with a log message:**
114-
115-
```
116-
Encountered IDENTIFY (OP 2) Rate Limit! Waiting 5 seconds before trying again!
117-
```
118-
> Note: Failing to backoff properly will cause JDA to wait 5 seconds after failing to connect. Respect the 5 second login rate limit!
119-
120-
#### Example Sharding
112+
#### Example Sharding - Using JDABuilder
121113

122114
```java
123115
public static void main(String[] args) throws Exception
124116
{
125-
JDABuilder shardBuilder = new JDABuilder(AccountType.BOT).setToken(args[0]).setReconnectQueue(new SessionReconnectQueue());
117+
JDABuilder shardBuilder = new JDABuilder(AccountType.BOT).setToken(args[0]);
126118
//register your listeners here using shardBuilder.addEventListener(...)
127119
shardBuilder.addEventListener(new MessageListener());
128120
for (int i = 0; i < 10; i++)
129121
{
130-
//using buildBlocking(JDA.Status.AWAITING_LOGIN_CONFIRMATION)
131-
// makes sure we start to delay the next shard once the current one actually
132-
// sent the login information, otherwise we might hit nasty race conditions
133122
shardBuilder.useSharding(i, 10)
134-
.buildBlocking(JDA.Status.AWAITING_LOGIN_CONFIRMATION);
135-
Thread.sleep(5000); //sleep 5 seconds between each login
123+
.buildAsync();
136124
}
137125
}
138126
```
139127

140-
> Note: We are not setting a `ShardedRateLimiter` here as it isn't necessary when we use the same builder!<br>
141-
> When you use multiple builders you should use JDABuilder.setShardedRateLimiter(ShardedRateLimiter) with a shared instance of the same ShardedRateLimiter!
128+
> When the `useSharding` method is invoked for the first time, the builder automatically sets a SessionController internally (if none is present)
129+
130+
#### Example Sharding - Using DefaultShardManager
131+
```java
132+
public static void main(String[] args) throws Exception
133+
{
134+
DefaultShardManagerBuilder builder = new DefaultShardManagerBuilder();
135+
builder.setToken(args[0]);
136+
builder.addEventListener(new MessageListener());
137+
builder.build();
138+
}
139+
```
142140

143141
## More Examples
144142
We provide a small set of Examples in the [Example Directory](https://github.com/DV8FromTheWorld/JDA/tree/master/src/examples/java).

build.gradle

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,9 @@ bintrayUpload {
226226
class Version {
227227
String major, minor, revision
228228

229-
String getBuild() {
230-
System.getenv("BUILD_NUMBER") ?: System.getProperty("BUILD_NUMBER") ?: "DEV"
229+
static String getBuild() {
230+
System.getenv("BUILD_NUMBER") ?: System.getProperty("BUILD_NUMBER") ?:
231+
System.getenv("GIT_COMMIT")?.substring(0, 7) ?: System.getProperty("GIT_COMMIT")?.substring(0, 7) ?:"DEV"
231232
}
232233

233234
String toString() {

0 commit comments

Comments
 (0)