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
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.rollbar.notifier.Rollbar;
import com.rollbar.notifier.sender.Sender;
import com.rollbar.notifier.sender.json.JsonSerializer;
import com.rollbar.notifier.sender.SyncSender;

import java.net.Proxy;

Expand All @@ -23,4 +23,24 @@ public interface Config extends CommonConfig {
* @return the proxy.
*/
Proxy proxy();

/**
* Time in milliseconds the default sender waits while connecting to the Rollbar API.
* Ignored when a custom {@link #sender() sender} is configured.
*
* @return the connect timeout in milliseconds.
*/
default int connectTimeoutMillis() {
return SyncSender.DEFAULT_CONNECT_TIMEOUT_MILLIS;
}

/**
* Time in milliseconds the default sender waits for data from the Rollbar API once connected.
* Ignored when a custom {@link #sender() sender} is configured.
*
* @return the read timeout in milliseconds.
*/
default int readTimeoutMillis() {
return SyncSender.DEFAULT_READ_TIMEOUT_MILLIS;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ public class ConfigBuilder {

protected boolean compressPayload;

protected int connectTimeoutMillis = SyncSender.DEFAULT_CONNECT_TIMEOUT_MILLIS;

protected int readTimeoutMillis = SyncSender.DEFAULT_READ_TIMEOUT_MILLIS;

protected List<String> redactedKeys;

protected boolean useDefaultRedactedKeys = true;
Expand Down Expand Up @@ -149,6 +153,8 @@ private ConfigBuilder(Config config) {
this.defaultLevels = new DefaultLevels(config);
this.truncateLargePayloads = config.truncateLargePayloads();
this.compressPayload = config.compressPayload();
this.connectTimeoutMillis = config.connectTimeoutMillis();
this.readTimeoutMillis = config.readTimeoutMillis();
this.maximumTelemetryData = config.maximumTelemetryData();
this.telemetryEventTracker = config.telemetryEventTracker();
this.redactedKeys = config.redactedKeys();
Expand Down Expand Up @@ -508,6 +514,55 @@ public ConfigBuilder compressPayload(boolean compress) {
return this;
}

/**
* <p>
* Time in milliseconds the default sender waits while connecting to the Rollbar API.
* Default: {@value SyncSender#DEFAULT_CONNECT_TIMEOUT_MILLIS}. Zero means wait forever, which
* is not recommended.
* </p>
* <p>
* Only applies when this builder creates the default sender. It has no effect when a custom
* {@link #sender(Sender) sender} is set, or when the builder starts from an existing
* configuration, for example through {@code Rollbar.configure}, because that configuration's
* sender is reused.
* </p>
* @param connectTimeoutMillis the connect timeout in milliseconds.
* @return the builder instance.
* @throws IllegalArgumentException if the timeout is negative.
*/
public ConfigBuilder connectTimeoutMillis(int connectTimeoutMillis) {
this.connectTimeoutMillis = requireNonNegative(connectTimeoutMillis, "connectTimeoutMillis");
Comment on lines +533 to +534

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Rebuild inherited senders when changing timeouts

When either timeout setter is used through Rollbar.configure, which starts with ConfigBuilder.withConfig, the constructor has already copied the current sender into this.sender. Consequently, build() skips its sender == null branch, so the returned Config reports the new timeout while the reused SyncSender continues using the old value. This makes runtime timeout reconfiguration ineffective; the builder needs to rebuild or otherwise update an inherited default sender when these values change.

Useful? React with 👍 / 👎.

return this;
}

/**
* <p>
* Time in milliseconds the default sender waits for data from the Rollbar API once connected.
* Default: {@value SyncSender#DEFAULT_READ_TIMEOUT_MILLIS}. Zero means wait forever, which is
* not recommended.
* </p>
* <p>
* Only applies when this builder creates the default sender. It has no effect when a custom
* {@link #sender(Sender) sender} is set, or when the builder starts from an existing
* configuration, for example through {@code Rollbar.configure}, because that configuration's
* sender is reused.
* </p>
* @param readTimeoutMillis the read timeout in milliseconds.
* @return the builder instance.
* @throws IllegalArgumentException if the timeout is negative.
*/
public ConfigBuilder readTimeoutMillis(int readTimeoutMillis) {
this.readTimeoutMillis = requireNonNegative(readTimeoutMillis, "readTimeoutMillis");
return this;
}

private static int requireNonNegative(int value, String name) {
if (value < 0) {
throw new IllegalArgumentException(name + " must not be negative: " + value);
}
return value;
}

/**
* <p>
* Maximum Telemetry events sent in a payload, only for the default TelemetryEventTracker, if
Expand Down Expand Up @@ -595,7 +650,9 @@ public Config build() {
new SyncSender.Builder(this.endpoint)
.accessToken(accessToken)
.proxy(proxy)
.compressPayload(this.compressPayload);
.compressPayload(this.compressPayload)
.connectTimeoutMillis(this.connectTimeoutMillis)
.readTimeoutMillis(this.readTimeoutMillis);
if (this.jsonSerializer != null) {
innerSender.jsonSerializer(this.jsonSerializer);
}
Expand Down Expand Up @@ -672,6 +729,10 @@ private static class ConfigImpl implements Config {

private final boolean compressPayload;

private final int connectTimeoutMillis;

private final int readTimeoutMillis;

private final int maximumTelemetryData;

private final TelemetryEventTracker telemetryEventTracker;
Expand Down Expand Up @@ -715,6 +776,8 @@ private static class ConfigImpl implements Config {
this.defaultLevels = builder.defaultLevels;
this.truncateLargePayloads = builder.truncateLargePayloads;
this.compressPayload = builder.compressPayload;
this.connectTimeoutMillis = builder.connectTimeoutMillis;
this.readTimeoutMillis = builder.readTimeoutMillis;
this.maximumTelemetryData = builder.maximumTelemetryData;
this.telemetryEventTracker = builder.telemetryEventTracker;
this.redactedKeys = builder.redactedKeys != null
Expand Down Expand Up @@ -874,6 +937,16 @@ public boolean compressPayload() {
return this.compressPayload;
}

@Override
public int connectTimeoutMillis() {
return this.connectTimeoutMillis;
}

@Override
public int readTimeoutMillis() {
return this.readTimeoutMillis;
}

@Override
public int maximumTelemetryData() {
return this.maximumTelemetryData;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ public class SyncSender extends AbstractSender {

public static final String UTF_8 = "UTF-8";

/**
* Default time in milliseconds to wait while establishing a connection to the Rollbar API.
*/
public static final int DEFAULT_CONNECT_TIMEOUT_MILLIS = 30_000;

/**
* Default time in milliseconds to wait for data from the Rollbar API once connected.
*/
public static final int DEFAULT_READ_TIMEOUT_MILLIS = 30_000;

private final URL url;

private final JsonSerializer jsonSerializer;
Expand All @@ -36,12 +46,18 @@ public class SyncSender extends AbstractSender {

private final boolean compressPayload;

private final int connectTimeoutMillis;

private final int readTimeoutMillis;

SyncSender(Builder builder) {
this.url = builder.url;
this.jsonSerializer = builder.jsonSerializer;
this.accessToken = builder.accessToken;
this.proxy = builder.proxy != null ? builder.proxy : Proxy.NO_PROXY;
this.compressPayload = builder.compressPayload;
this.connectTimeoutMillis = builder.connectTimeoutMillis;
this.readTimeoutMillis = builder.readTimeoutMillis;
}

@Override
Expand All @@ -66,6 +82,11 @@ private HttpURLConnection getConnection() throws IOException {
// By default, this will use the HTTPS Rollbar API URL defined in DEFAULT_API_ENDPOINT.
HttpURLConnection connection = (HttpURLConnection) url.openConnection(this.proxy);

// Without these calls HttpURLConnection never times out, which could block the sender
// thread forever.
connection.setConnectTimeout(connectTimeoutMillis);
connection.setReadTimeout(readTimeoutMillis);

if (accessToken != null && !"".equals(accessToken)) {
connection.setRequestProperty("x-rollbar-access-token", accessToken);
}
Expand Down Expand Up @@ -144,6 +165,10 @@ public static final class Builder {

private boolean compressPayload = true;

private int connectTimeoutMillis = DEFAULT_CONNECT_TIMEOUT_MILLIS;

private int readTimeoutMillis = DEFAULT_READ_TIMEOUT_MILLIS;

public Builder() {
this(DEFAULT_API_ENDPOINT);
}
Expand Down Expand Up @@ -218,6 +243,32 @@ public Builder compressPayload(boolean compress) {
return this;
}

/**
* Time in milliseconds to wait while establishing a connection to the Rollbar API.
* Default: {@value SyncSender#DEFAULT_CONNECT_TIMEOUT_MILLIS}. Zero means wait forever, which
* is not recommended: a single unresponsive connection would stop all reporting.
* @param connectTimeoutMillis the connect timeout in milliseconds.
* @return the builder instance.
* @throws IllegalArgumentException if the timeout is negative.
*/
public Builder connectTimeoutMillis(int connectTimeoutMillis) {
this.connectTimeoutMillis = requireNonNegative(connectTimeoutMillis, "connectTimeoutMillis");
return this;
}

/**
* Time in milliseconds to wait for data from the Rollbar API once connected.
* Default: {@value SyncSender#DEFAULT_READ_TIMEOUT_MILLIS}. Zero means wait forever, which
* is not recommended: a single unresponsive connection would stop all reporting.
* @param readTimeoutMillis the read timeout in milliseconds.
* @return the builder instance.
* @throws IllegalArgumentException if the timeout is negative.
*/
public Builder readTimeoutMillis(int readTimeoutMillis) {
this.readTimeoutMillis = requireNonNegative(readTimeoutMillis, "readTimeoutMillis");
return this;
}

/**
* Builds the {@link SyncSender sync sender}.
*
Expand All @@ -227,6 +278,13 @@ public SyncSender build() {
return new SyncSender(this);
}

private static int requireNonNegative(int value, String name) {
if (value < 0) {
throw new IllegalArgumentException(name + " must not be negative: " + value);
}
return value;
}

private static URL parseUrl(String url) {
try {
URL result = new URL(url);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsInstanceOf.instanceOf;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

import com.rollbar.api.payload.Payload;
import com.rollbar.api.payload.data.Client;
import com.rollbar.api.payload.data.Level;
import com.rollbar.api.payload.data.Notifier;
Expand All @@ -19,12 +23,19 @@
import com.rollbar.notifier.provider.notifier.NotifierProvider;
import com.rollbar.notifier.sender.BufferedSender;
import com.rollbar.notifier.sender.Sender;
import com.rollbar.notifier.sender.SyncSender;
import com.rollbar.notifier.sender.exception.SenderException;
import com.rollbar.notifier.sender.listener.SenderListener;
import com.rollbar.notifier.transformer.Transformer;
import com.rollbar.notifier.uuid.UuidGenerator;
import java.net.InetAddress;
import java.net.Proxy;
import java.net.ServerSocket;
import java.net.SocketTimeoutException;
import java.util.Map;
import org.junit.Rule;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
Expand Down Expand Up @@ -97,6 +108,8 @@ public void shouldBuildConfigurationWithDefaults() {
assertThat(config.handleUncaughtErrors(), is(true));
assertThat(config.isEnabled(), is(true));
assertThat(config.proxy(), is(nullValue()));
assertThat(config.connectTimeoutMillis(), is(SyncSender.DEFAULT_CONNECT_TIMEOUT_MILLIS));
assertThat(config.readTimeoutMillis(), is(SyncSender.DEFAULT_READ_TIMEOUT_MILLIS));

assertThat(config.defaultMessageLevel(), equalTo(Level.WARNING));
assertThat(config.defaultThrowableLevel(), equalTo(Level.ERROR));
Expand Down Expand Up @@ -204,4 +217,57 @@ public void shouldBuildWithConfig() {
assertThat(config.isEnabled(), is(copy.isEnabled()));
assertThat(config.maximumTelemetryData(), is(maximumTelemetryData));
}

@Test
public void shouldBuildWithTimeoutsAndCopyThem() {
Config config = ConfigBuilder.withAccessToken(ACCESS_TOKEN)
.connectTimeoutMillis(1234)
.readTimeoutMillis(5678)
.build();

Config copy = withConfig(config).build();

assertThat(config.connectTimeoutMillis(), is(1234));
assertThat(config.readTimeoutMillis(), is(5678));
assertThat(copy.connectTimeoutMillis(), is(1234));
assertThat(copy.readTimeoutMillis(), is(5678));
}
Comment on lines +221 to +234

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Low: Most users will set the new timeouts on the configuration builder, but the new test only checks that the values are stored on the configuration and survive a copy. Nothing checks that the sender the builder creates actually uses them. If the two lines that pass the values to the sender (ConfigBuilder.java:654-655) were dropped or broken, every test would still pass. A user who sets, for example, a 5-second read timeout would silently get 30 seconds, while the configuration still reports 5 seconds. The values can't be read back from the sender directly because they are private. However, the default sender wraps the real one and exposes it through BufferedSender.sender(), so the existing silent-server test can be reused through the builder.


@Test(timeout = 10_000)
public void shouldApplyReadTimeoutToTheDefaultSender() throws Exception {
// Accepts the connection (via the backlog) but never responds. If the configured timeout did
// not reach the sender, it would wait for the 30 s default and this test would time out.
String host = "127.0.0.1";
try (ServerSocket silentServer = new ServerSocket(0, 1, InetAddress.getByName(host))) {
Config config = ConfigBuilder.withAccessToken(ACCESS_TOKEN)
.endpoint("http://" + host + ":" + silentServer.getLocalPort() + "/api/1/item/")
.readTimeoutMillis(200)
.build();
try {
Sender defaultSender = ((BufferedSender) config.sender()).sender();
assertThat(defaultSender, is(instanceOf(SyncSender.class)));
SenderListener listener = mock(SenderListener.class);
defaultSender.addListener(listener);
Payload payload = new Payload("{}");

defaultSender.send(payload);

ArgumentCaptor<SenderException> error = ArgumentCaptor.forClass(SenderException.class);
verify(listener).onError(eq(payload), error.capture());
assertThat(error.getValue().getCause(), is(instanceOf(SocketTimeoutException.class)));
} finally {
config.sender().close(false);
}
}
}

@Test(expected = IllegalArgumentException.class)
public void shouldRejectNegativeConnectTimeout() {
ConfigBuilder.withAccessToken(ACCESS_TOKEN).connectTimeoutMillis(-1);
}

@Test(expected = IllegalArgumentException.class)
public void shouldRejectNegativeReadTimeout() {
ConfigBuilder.withAccessToken(ACCESS_TOKEN).readTimeoutMillis(-1);
}
}
Loading
Loading