Skip to content
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

Make XrplClient timeouts configurable #550

Merged
merged 5 commits into from
Sep 17, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.google.common.annotations.Beta;
import feign.Feign;
import feign.Headers;
import feign.Request.Options;
import feign.RequestLine;
import feign.jackson.JacksonDecoder;
import feign.jackson.JacksonEncoder;
Expand Down Expand Up @@ -71,18 +72,34 @@ public interface JsonRpcClient {
/**
* Constructs a new client for the given url.
*
* @param rippledUrl url for the faucet server.
* @param rippledUrl The {@link HttpUrl} of the node to connect to.
*
* @return A {@link JsonRpcClient} that can make request to {@code rippledUrl}
*/
static JsonRpcClient construct(final HttpUrl rippledUrl) {
Objects.requireNonNull(rippledUrl);

return construct(rippledUrl, new Options());
}

/**
* Constructs a new client for the given url with the given client options.
*
* @param rippledUrl The {@link HttpUrl} of the node to connect to.
* @param options An {@link Options}.
*
* @return A {@link JsonRpcClient}.
*/
static JsonRpcClient construct(HttpUrl rippledUrl, Options options) {
Objects.requireNonNull(rippledUrl);
Objects.requireNonNull(options);

return Feign.builder()
.encoder(new JacksonEncoder(objectMapper))
// rate limiting will return a 503 status that can be retried
.errorDecoder(new RetryStatusDecoder(RETRY_INTERVAL, SERVICE_UNAVAILABLE_STATUS))
.decode404()
.options(options)
.decoder(new OptionalDecoder(new JacksonDecoder(objectMapper)))
.target(JsonRpcClient.class, rippledUrl.toString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import com.google.common.collect.Range;
import com.google.common.primitives.UnsignedInteger;
import com.google.common.primitives.UnsignedLong;
import feign.Request;
import feign.Request.Options;
import okhttp3.HttpUrl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -99,8 +101,10 @@
import org.xrpl.xrpl4j.model.transactions.Transaction;
import org.xrpl.xrpl4j.model.transactions.TransactionMetadata;

import java.time.Duration;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.TimeUnit;

/**
* <p>A client which wraps a rippled network client and is responsible for higher order functionality such as signing
Expand All @@ -127,6 +131,31 @@ public XrplClient(final HttpUrl rippledUrl) {
this(JsonRpcClient.construct(rippledUrl));
}

/**
* Public constructor that allows for configuration of connect and read timeouts.
*
* <p>Note that any {@link Duration} passed in that is less than one millisecond will result in the actual timeout
* being zero milliseconds. It is therefore advised to never set {@code connectTimeout} or {@code readTimeout} to a
* {@link Duration} less than one millisecond.
*
* @param rippledUrl The {@link HttpUrl} of the node to connect to.
* @param connectTimeout A {@link Duration} indicating the client's connect timeout.
* @param readTimeout A {@link Duration} indicating the client's read timeout.
*/
public XrplClient(
HttpUrl rippledUrl,
Duration connectTimeout,
Duration readTimeout
) {
this(
JsonRpcClient.construct(
rippledUrl,
new Options(connectTimeout.toMillis(), TimeUnit.MILLISECONDS, readTimeout.toMillis(), TimeUnit.MILLISECONDS,
true)
)
);
}

/**
* Required-args constructor (exists for testing purposes only).
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,11 @@
import org.xrpl.xrpl4j.model.transactions.XrpCurrencyAmount;

import java.math.BigDecimal;
import java.time.Duration;
import java.time.ZonedDateTime;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;

/**
Expand Down Expand Up @@ -599,6 +601,18 @@ public void getXrplClientTest() {
assertThat(new XrplClient(rippledUrl) instanceof JsonRpcClient).isFalse();
}

@Test
void createXrplClientWithDurationTimeouts() {
HttpUrl rippledUrl = HttpUrl.parse("https://s.altnet.rippletest.net:51234");
XrplClient client = new XrplClient(
rippledUrl,
Duration.ofSeconds(1),
Duration.ofMinutes(2)
);

assertThat(client).isInstanceOf(XrplClient.class);
}

@Test
public void submitSingleSignedTransaction() {
BcSignatureService bcSignatureService = new BcSignatureService();
Expand Down
Loading