Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ sendStatusMessage: true
newsGuildId:
newsChannelId:
newsChannelPublishWaitMilliSec: 1000
http.timeoutSec: 30
http:
timeoutSec: 30
thread.pool.size: 20
logging: NONE
shardsTotal:
shardIds:
disableCommandUpdate: false
Expand Down
1 change: 1 addition & 0 deletions discord-connector/jda/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ dependencies {
implementation(libs.commons.text)
implementation(libs.avaje.config)
implementation(libs.avaje.slf4j)
implementation("com.squareup.okhttp3:logging-interceptor:5.1.0")

compileOnly(libs.lombok)
annotationProcessor(libs.lombok)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.google.common.base.Stopwatch;
import com.google.common.base.Strings;
import com.google.common.io.Resources;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import de.janno.discord.connector.api.*;
import io.avaje.config.Config;
import lombok.NonNull;
Expand Down Expand Up @@ -33,7 +34,10 @@
import net.dv8tion.jda.api.sharding.DefaultShardManagerBuilder;
import net.dv8tion.jda.api.sharding.ShardManager;
import net.dv8tion.jda.internal.utils.IOUtil;
import okhttp3.ConnectionPool;
import okhttp3.Dispatcher;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import org.apache.commons.lang3.StringUtils;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Scheduler;
Expand All @@ -45,6 +49,8 @@
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.concurrent.ConcurrentSkipListSet;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;
Expand All @@ -65,15 +71,34 @@ public JdaClient(@NonNull List<SlashCommand> slashCommands,
final Stopwatch startStopwatch = Stopwatch.createStarted();
final Scheduler scheduler = Schedulers.boundedElastic();
final Set<Long> botInGuildIdSet = new ConcurrentSkipListSet<>();
final Duration timeout = Duration.of(Config.getLong("http.timeoutSec"), ChronoUnit.SECONDS);
final int CONCURRENCY_LEVEL = Config.getInt("http.thread.pool.size", 20);
final Duration timeout = Duration.of(Config.getLong("http.timeoutSec", 30), ChronoUnit.SECONDS);
ConnectionPool connectionPool = new ConnectionPool(CONCURRENCY_LEVEL, 5, TimeUnit.MINUTES);
JdaMetrics.registerHttpClientConnectionPool(connectionPool, CONCURRENCY_LEVEL);
Dispatcher dispatcher = new Dispatcher(new ThreadPoolExecutor(8, 8,
1, TimeUnit.MINUTES,
new LinkedBlockingQueue<>(16),
new ThreadFactoryBuilder().setNameFormat("okhttp-dispatcher-%d").setDaemon(true).build(),
new ThreadPoolExecutor.CallerRunsPolicy()));
dispatcher.setMaxRequestsPerHost(CONCURRENCY_LEVEL);
dispatcher.setMaxRequests(CONCURRENCY_LEVEL);
HttpLoggingInterceptor logging = new HttpLoggingInterceptor(log::debug);
logging.setLevel(logLevelFromString(Config.get("http.logging", "NONE")));

Config.onChange("http.logging", s -> logging.setLevel(logLevelFromString(s)));

final OkHttpClient okHttpClient = IOUtil.newHttpClientBuilder()
.eventListener(JdaMetrics.getOkHttpEventListener())
.addInterceptor(logging)
.writeTimeout(timeout)
.readTimeout(timeout)
.connectTimeout(timeout)
.connectionPool(connectionPool)
.dispatcher(dispatcher)
.pingInterval(Duration.ofSeconds(3))
.retryOnConnectionFailure(true)
.build();

JdaMetrics.registerHttpClient(okHttpClient);
final String token = Config.get("token");
if (Strings.isNullOrEmpty(token)) {
throw new IllegalArgumentException("Missing discord token");
Expand Down Expand Up @@ -238,6 +263,12 @@ public void onButtonInteraction(@NonNull ButtonInteractionEvent event) {
.registerSlashCommands(shards.getFirst(), disableCommandUpdate);
}

private static HttpLoggingInterceptor.Level logLevelFromString(String level) {
return HttpLoggingInterceptor.Level.getEntries().stream()
.filter(l -> l.name().equals(level))
.findFirst().orElse(HttpLoggingInterceptor.Level.NONE);
}

@VisibleForTesting
static void onGuildReadyHandler(GuildReadyEvent event,
Set<Long> botInGuildIdSet,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
import io.micrometer.core.instrument.binder.okhttp3.OkHttpMetricsEventListener;
import lombok.extern.slf4j.Slf4j;
import net.dv8tion.jda.api.JDA;
import okhttp3.ConnectionPool;
import okhttp3.EventListener;
import okhttp3.OkHttpClient;

import java.util.List;
import java.util.Locale;
import java.util.Set;

Expand All @@ -24,8 +25,8 @@ public class JdaMetrics {
private static final String USER_LOCALE = "userLocale";
private static final String SHARD_ID = "shardId";

public static void registerHttpClient(OkHttpClient client) {
new OkHttpConnectionPoolMetrics(client.connectionPool()).bindTo(globalRegistry);
public static void registerHttpClientConnectionPool(ConnectionPool connectionPool, int maxIdleConnections) {
new OkHttpConnectionPoolMetrics(connectionPool, "okhttp.pool", List.of(), maxIdleConnections).bindTo(globalRegistry);
}

public static void startGuildCountGauge(Set<Long> botInGuildIdSet) {
Expand Down