Skip to content

Commit

Permalink
Updated for ShardManager changes
Browse files Browse the repository at this point in the history
  • Loading branch information
MinnDevelopment committed Dec 10, 2017
1 parent a7094a7 commit c793ada
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import net.dv8tion.jda.core.utils.tuple.Pair;
import okhttp3.OkHttpClient;
import org.slf4j.Logger;
import org.slf4j.MDC;

import javax.security.auth.login.LoginException;
import java.util.*;
Expand Down Expand Up @@ -195,6 +196,11 @@ public class DefaultShardManager implements ShardManager
*/
protected IntFunction<OnlineStatus> statusProvider;

/**
* The MDC context new JDA instances should have on startup.
*/
protected Map<String, String> contextMap;

/**
* Creates a new DefaultShardManager instance.
* @param shardsTotal
Expand Down Expand Up @@ -250,7 +256,8 @@ protected DefaultShardManager(final int shardsTotal, final Collection<Integer> s
final int maxReconnectDelay, final int corePoolSize, final boolean enableVoice,
final boolean enableShutdownHook, final boolean enableBulkDeleteSplitting,
final boolean autoReconnect, final IntFunction<Boolean> idleProvider,
final boolean retryOnTimeout, boolean useShutdownNow)
final boolean retryOnTimeout, final boolean useShutdownNow,
final Map<String, String> contextMap)
{
this.shardsTotal = shardsTotal;
this.listeners = listeners;
Expand All @@ -272,6 +279,7 @@ protected DefaultShardManager(final int shardsTotal, final Collection<Integer> s
this.idleProvider = idleProvider;
this.retryOnTimeout = retryOnTimeout;
this.useShutdownNow = useShutdownNow;
this.contextMap = contextMap;

if (shardsTotal != -1)
{
Expand Down Expand Up @@ -489,7 +497,7 @@ protected JDAImpl buildInstance(final int shardId) throws LoginException, RateLi
{
final JDAImpl jda = new JDAImpl(AccountType.BOT, this.token, this.httpClientBuilder, this.wsFactory, this.shardedRateLimiter,
this.autoReconnect, this.enableVoice, this.enableBulkDeleteSplitting, this.enableBulkDeleteSplitting, retryOnTimeout,
this.corePoolSize, this.maxReconnectDelay);
this.corePoolSize, this.maxReconnectDelay, this.contextMap == null ? null : new ConcurrentHashMap<>(this.contextMap));

jda.asBot().setShardManager(this);

Expand Down Expand Up @@ -597,7 +605,12 @@ protected ScheduledExecutorService createExecutor(ThreadFactory threadFactory)
ThreadFactory factory = threadFactory == null
? r ->
{
final Thread t = new Thread(r, "DefaultShardManager");
final Thread t = new Thread(() ->
{
MDC.setContextMap(contextMap);
r.run();
});
t.setName("DefaultShardManager");
t.setDaemon(true);
t.setPriority(Thread.NORM_PRIORITY + 1);
return t;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@
package net.dv8tion.jda.bot.sharding;

import com.neovisionaries.ws.client.WebSocketFactory;
import java.util.*;
import java.util.concurrent.ThreadFactory;
import java.util.function.IntFunction;
import java.util.stream.Collectors;
import javax.security.auth.login.LoginException;

import net.dv8tion.jda.core.OnlineStatus;
import net.dv8tion.jda.core.ShardedRateLimiter;
import net.dv8tion.jda.core.audio.factory.IAudioSendFactory;
Expand All @@ -30,6 +24,12 @@
import net.dv8tion.jda.core.utils.Checks;
import okhttp3.OkHttpClient;

import javax.security.auth.login.LoginException;
import java.util.*;
import java.util.concurrent.ThreadFactory;
import java.util.function.IntFunction;
import java.util.stream.Collectors;

/**
* Used to create new instances of JDA's default {@link net.dv8tion.jda.bot.sharding.ShardManager ShardManager} implementation.
*
Expand All @@ -41,6 +41,8 @@
*/
public class DefaultShardManagerBuilder
{
protected final List<Object> listeners = new ArrayList<>();
protected Map<String, String> mdcContext = null;
protected boolean enableBulkDeleteSplitting = true;
protected boolean enableShutdownHook = true;
protected boolean enableVoice = true;
Expand All @@ -54,7 +56,6 @@ public class DefaultShardManagerBuilder
protected IntFunction<Boolean> idleProvider = null;
protected IntFunction<Game> gameProvider = null;
protected IntFunction<OnlineStatus> statusProvider = null;
protected final List<Object> listeners = new ArrayList<>();
protected Collection<Integer> shards = null;
protected IEventManager eventManager = null;
protected ShardedRateLimiter shardedRateLimiter = null;
Expand All @@ -71,6 +72,27 @@ public class DefaultShardManagerBuilder
*/
public DefaultShardManagerBuilder() {}

/**
* Sets the {@link org.slf4j.MDC MDC} mappings to use in JDA.
* <br>If sharding is enabled JDA will automatically add a {@code jda.shard} context with the format {@code [SHARD_ID / TOTAL]}
* where {@code SHARD_ID} and {@code TOTAL} are the shard configuration.
* Additionally it will provide context for the id via {@code jda.shard.id} and the total via {@code jda.shard.total}.
*
* <p><b>The manager will create a copy of this map for every JDA instance so it is recommended to use a thread-safe map implementation!</b>
*
* @param map
* The <b>modifiable</b> context map to use in JDA, or {@code null} to reset
*
* @return The {@link net.dv8tion.jda.bot.sharding.DefaultShardManagerBuilder DefaultShardManagerBuilder} instance. Useful for chaining.
*
* @see <a href="https://www.slf4j.org/api/org/slf4j/MDC.html" target="_blank">MDC Javadoc</a>
*/
public DefaultShardManagerBuilder setContextMap(Map<String, String> map)
{
this.mdcContext = map;
return this;
}

/**
* Adds all provided listeners to the list of listeners that will be used to populate the {@link DefaultShardManager DefaultShardManager} object.
* <br>This uses the {@link net.dv8tion.jda.core.hooks.InterfacedEventManager InterfacedEventListener} by default.
Expand Down Expand Up @@ -149,35 +171,6 @@ public DefaultShardManagerBuilder removeEventListeners(final Collection<Object>
return this;
}

/**
* Builds a new {@link net.dv8tion.jda.bot.sharding.ShardManager ShardManager} instance and uses the provided token to start the login process.
* <br>The login process runs in a different thread, so while this will return immediately, {@link net.dv8tion.jda.bot.sharding.ShardManager ShardManager} has not
* finished loading, thus many {@link net.dv8tion.jda.bot.sharding.ShardManager ShardManager} methods have the chance to return incorrect information.
* <br>The main use of this method is to start the JDA connect process and do other things in parallel while startup is
* being performed like database connection or local resource loading.
*
* <p>Note that this method is async and as such will <b>not</b> block until all shards are started.
*
* @throws LoginException
* If the provided token is invalid.
* @throws IllegalArgumentException
* If the provided token is empty or null.
*
* @return A {@link net.dv8tion.jda.bot.sharding.ShardManager ShardManager} instance that has started the login process. It is unknown as
* to whether or not loading has finished when this returns.
*/
public ShardManager build() throws LoginException, IllegalArgumentException
{
final DefaultShardManager manager = new DefaultShardManager(this.shardsTotal, this.shards, this.listeners, this.token, this.eventManager,
this.audioSendFactory, this.gameProvider, this.statusProvider, this.httpClientBuilder, this.wsFactory, this.threadFactory, this.shardedRateLimiter,
this.maxReconnectDelay, this.corePoolSize, this.enableVoice, this.enableShutdownHook, this.enableBulkDeleteSplitting,
this.autoReconnect, this.idleProvider, this.retryOnTimeout, this.useShutdownNow);

manager.login();

return manager;
}

/**
* Enables/Disables Voice functionality.
* <br>This is useful, if your current system doesn't support Voice and you do not need it.
Expand Down Expand Up @@ -453,7 +446,7 @@ public DefaultShardManagerBuilder setThreadFactory(final ThreadFactory threadFac

/**
* Sets the {@link okhttp3.OkHttpClient.Builder Builder} that will be used by JDA's requester.
* This can be used to set things such as connection timeout and proxy.
* This can be used to set things such as connection timeout and proxy.
*
* @param builder
* The new {@link okhttp3.OkHttpClient.Builder OkHttpClient.Builder} to use.
Expand Down Expand Up @@ -695,4 +688,33 @@ public DefaultShardManagerBuilder setWebsocketFactory(WebSocketFactory factory)
this.wsFactory = factory;
return this;
}

/**
* Builds a new {@link net.dv8tion.jda.bot.sharding.ShardManager ShardManager} instance and uses the provided token to start the login process.
* <br>The login process runs in a different thread, so while this will return immediately, {@link net.dv8tion.jda.bot.sharding.ShardManager ShardManager} has not
* finished loading, thus many {@link net.dv8tion.jda.bot.sharding.ShardManager ShardManager} methods have the chance to return incorrect information.
* <br>The main use of this method is to start the JDA connect process and do other things in parallel while startup is
* being performed like database connection or local resource loading.
*
* <p>Note that this method is async and as such will <b>not</b> block until all shards are started.
*
* @throws LoginException
* If the provided token is invalid.
* @throws IllegalArgumentException
* If the provided token is empty or null.
*
* @return A {@link net.dv8tion.jda.bot.sharding.ShardManager ShardManager} instance that has started the login process. It is unknown as
* to whether or not loading has finished when this returns.
*/
public ShardManager build() throws LoginException, IllegalArgumentException
{
final DefaultShardManager manager = new DefaultShardManager(this.shardsTotal, this.shards, this.listeners, this.token, this.eventManager,
this.audioSendFactory, this.gameProvider, this.statusProvider, this.httpClientBuilder, this.wsFactory, this.threadFactory, this.shardedRateLimiter,
this.maxReconnectDelay, this.corePoolSize, this.enableVoice, this.enableShutdownHook, this.enableBulkDeleteSplitting,
this.autoReconnect, this.idleProvider, this.retryOnTimeout, this.useShutdownNow, this.mdcContext);

manager.login();

return manager;
}
}
7 changes: 2 additions & 5 deletions src/main/java/net/dv8tion/jda/core/entities/impl/JDAImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,7 @@

import javax.security.auth.login.LoginException;
import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.*;
import java.util.stream.Collectors;

public class JDAImpl implements JDA
Expand Down Expand Up @@ -121,7 +118,7 @@ public JDAImpl(AccountType accountType, String token, OkHttpClient.Builder httpC
this.bulkDeleteSplittingEnabled = bulkDeleteSplittingEnabled;
this.pool = new ScheduledThreadPoolExecutor(corePoolSize, new JDAThreadFactory());
this.maxReconnectDelay = maxReconnectDelay;
this.contextMap = contextMap == null ? new HashMap<>() : contextMap;
this.contextMap = contextMap == null ? new ConcurrentHashMap<>() : contextMap;

this.presence = new PresenceImpl(this);
this.requester = new Requester(this, rateLimiter);
Expand Down

0 comments on commit c793ada

Please sign in to comment.