Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.rollbar.notifier.util.ObjectsUtils;

import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;

/**
Expand Down Expand Up @@ -148,7 +149,10 @@ private NetworkStatus getNetworkStatus(Throwable error) {
return NetworkStatus.OK;
}

if (error instanceof SocketException || error instanceof UnknownHostException) {
// SocketTimeoutException is not a SocketException. It's what a connection that stops
// answering throws once the sender's timeouts expire, so it has to be listed on its own.
if (error instanceof SocketException || error instanceof SocketTimeoutException
|| error instanceof UnknownHostException) {
if (detector.isNetworkAvailable()) {
return NetworkStatus.UNKNOWN_NETWORK_ISSUE;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.rollbar.api.payload.Payload;
import com.rollbar.notifier.sender.SenderFailureStrategy;
import com.rollbar.notifier.sender.exception.SenderException;
import com.rollbar.notifier.sender.result.Response;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -10,6 +11,7 @@
import org.mockito.ArgumentCaptor;

import java.net.ConnectException;
import java.net.SocketTimeoutException;

import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
Expand Down Expand Up @@ -105,6 +107,48 @@ public void ifTheNetworkIsUpSendingShouldBeSuspendedFor1SecAndPayloadRetried() {
}
}

public static class GivenATimeoutErrorTest extends ConnectionAwareSenderFailureStrategyTest {
// Wrapped the same way the sender reports it to the listeners.
private final SenderException exception =
new SenderException(new SocketTimeoutException("connect timed out"));

@Test
public void ifTheNetworkIsDownSendingShouldBeSuspendedFor5MinutesAndPayloadRetried() {
when(connectivityDetector.isNetworkAvailable()).thenReturn(false);

SenderFailureStrategy.PayloadAction action = sut.getAction(payload, exception);

assertThat(action, equalTo(SenderFailureStrategy.PayloadAction.CAN_BE_RETRIED));
assertThat(sut.isSendingSuspended(), is(true));

moveTimeForward(299999);

assertThat(sut.isSendingSuspended(), is(true));

moveTimeForward(1);

assertThat(sut.isSendingSuspended(), is(false));
}

@Test
public void ifTheNetworkIsUpSendingShouldBeSuspendedFor1SecAndPayloadRetried() {
when(connectivityDetector.isNetworkAvailable()).thenReturn(true);

SenderFailureStrategy.PayloadAction action = sut.getAction(payload, exception);

assertThat(action, equalTo(SenderFailureStrategy.PayloadAction.CAN_BE_RETRIED));
assertThat(sut.isSendingSuspended(), is(true));

moveTimeForward(999);

assertThat(sut.isSendingSuspended(), is(true));

moveTimeForward(1);

assertThat(sut.isSendingSuspended(), is(false));
}
}

public static class GivenAnUnrecognizedError extends ConnectionAwareSenderFailureStrategyTest {
private final IllegalStateException exception = new IllegalStateException("Something unexpected");

Expand Down
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 @@ -24,6 +24,26 @@ public interface Config extends CommonConfig {
*/
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;
}

/**
* <p>
* If set to true (the default), a JVM shutdown hook is registered that flushes any payloads
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ public class ConfigBuilder {

protected boolean compressPayload;

protected int connectTimeoutMillis = SyncSender.DEFAULT_CONNECT_TIMEOUT_MILLIS;

protected int readTimeoutMillis = SyncSender.DEFAULT_READ_TIMEOUT_MILLIS;

protected boolean flushOnShutdown;

protected long shutdownTimeoutMillis;
Expand Down Expand Up @@ -166,6 +170,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.flushOnShutdown = config.flushOnShutdown();
this.shutdownTimeoutMillis = config.shutdownTimeoutMillis();
this.maximumTelemetryData = config.maximumTelemetryData();
Expand Down Expand Up @@ -527,6 +533,27 @@ 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 +552 to +553

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>
* If set to true (the default), a JVM shutdown hook is registered that flushes any payloads
Expand All @@ -552,6 +579,34 @@ public ConfigBuilder flushOnShutdown(boolean flushOnShutdown) {
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>
* The maximum time, in milliseconds, that the shutdown hook will spend flushing buffered
Expand Down Expand Up @@ -658,7 +713,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 @@ -735,6 +792,10 @@ private static class ConfigImpl implements Config {

private final boolean compressPayload;

private final int connectTimeoutMillis;

private final int readTimeoutMillis;

private final boolean flushOnShutdown;

private final long shutdownTimeoutMillis;
Expand Down Expand Up @@ -782,6 +843,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.flushOnShutdown = builder.flushOnShutdown;
this.shutdownTimeoutMillis = builder.shutdownTimeoutMillis;
this.maximumTelemetryData = builder.maximumTelemetryData;
Expand Down Expand Up @@ -943,6 +1006,16 @@ public boolean compressPayload() {
return this.compressPayload;
}

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

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

@Override
public boolean flushOnShutdown() {
return this.flushOnShutdown;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@
* </p>
*
* <p>
* The hook is bounded by a timeout. Flushing performs blocking network I/O, and the sender does
* not impose a socket timeout of its own, so an unbounded flush would be able to stall JVM
* shutdown for as long as the remote end kept the connection open. Instead the flush runs on a
* separate daemon thread and this hook waits for it for at most {@code timeoutMillis}; once that
* elapses shutdown continues regardless, and the daemon thread is torn down with the JVM.
* The hook is bounded by a timeout. Flushing performs blocking network I/O, and the sender's
* socket timeouts (30 s by default) are much longer than this hook's budget, so an unbounded
* flush could stall JVM shutdown for that long, or forever if the timeouts are set to zero.
* Instead the flush runs on a separate daemon thread and this hook waits for it for at most
* {@code timeoutMillis}; once that elapses shutdown continues regardless, and the daemon thread
* is torn down with the JVM.
* </p>
*
* <p>
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
Loading
Loading