Skip to content

Added some logging and slight refactoring to the request sender #119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand Down Expand Up @@ -130,4 +131,41 @@ protected void setSuccessResult(DiscordResponse result) {
protected void setFailureError(Throwable ex) {
future.setException(ex);
}

@Override
public String toString() {
String headersString =
headers.entrySet().stream()
.map(entry -> entry.getKey() + "=" + entry.getValue())
.collect(Collectors.joining(", "));
Comment on lines +137 to +140
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

y not just use default Map#toString


String queryParamsString =
queryParameters.entrySet().stream()
.map(entry -> entry.getKey() + "=" + entry.getValue())
.collect(Collectors.joining(", "));

String bodyString;
try {
bodyString = OBJECT_MAPPER.writeValueAsString(body);
} catch (Exception e) {
bodyString = "Error converting body to string: " + e.getMessage();
}

return this.getClass().getName()
+ "{"
+ "method="
+ method
+ ", path='"
+ path
+ '\''
+ ", headers={"
+ headersString
+ '}'
+ ", queryParameters={"
+ queryParamsString
+ '}'
+ ", body="
+ bodyString
+ '}';
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.javadiscord.jdi.internal.api;

import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
Expand All @@ -23,7 +24,7 @@ public class DiscordRequestDispatcher implements Runnable {
private final HttpClient httpClient;
private final BlockingQueue<DiscordRequestBuilder> queue;
private final String botToken;
private AtomicBoolean running = new AtomicBoolean(false);
private final AtomicBoolean running = new AtomicBoolean(false);
private int numberOfRequestsSent;
private long timeSinceLastRequest;

Expand All @@ -48,20 +49,14 @@ public void run() {
while (running.get()) {
long currentTime = System.currentTimeMillis();
long elapsed = currentTime - timeSinceLastRequest;

if (elapsed < 1000 && numberOfRequestsSent >= 50) {
try {
Thread.sleep(1000 - elapsed);
} catch (InterruptedException e) {
/* Ignore */
}
numberOfRequestsSent = 0;
}

try {
if (elapsed < 1000 && numberOfRequestsSent >= 50) {
Thread.sleep(1000L - elapsed);
numberOfRequestsSent = 0;
}
sendRequest(queue.take());
} catch (InterruptedException e) {
/* Ignore */
Thread.currentThread().interrupt();
}
}
}
Expand All @@ -71,6 +66,7 @@ public void stop() {
}

private void sendRequest(DiscordRequestBuilder discordRequestBuilder) {
LOGGER.debug("Sending request {}", discordRequestBuilder);
try {
HttpRequest.Builder requestBuilder =
HttpRequest.newBuilder()
Expand Down Expand Up @@ -126,7 +122,11 @@ private void sendRequest(DiscordRequestBuilder discordRequestBuilder) {
)
);

} catch (Exception e) {
} catch (InterruptedException e) {
LOGGER.error("Thread was interrupted", e);
discordRequestBuilder.setFailureError(e);
Thread.currentThread().interrupt();
} catch (IOException e) {
LOGGER.error(
"Failed to send request to {}{}", BASE_URL, discordRequestBuilder.getPath(), e
);
Expand Down
Loading