Skip to content

Commit 0a62067

Browse files
committed
Added ability to set the connection timeout
1 parent cddf029 commit 0a62067

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

src/main/java/com/maxmind/geoip2/webservice/Client.java

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,18 +97,20 @@
9797
* </p>
9898
*/
9999
public class Client {
100+
private final String host;
101+
private final List<String> languages;
102+
private final String licenseKey;
103+
private final int timeout;
100104
private final HttpTransport transport;
101105
private final int userId;
102-
private final String licenseKey;
103-
private final List<String> languages;
104-
private String host = "geoip.maxmind.com";
105106

106107
@SuppressWarnings("synthetic-access")
107108
private Client(Builder builder) {
108109
this.userId = builder.userId;
109110
this.licenseKey = builder.licenseKey;
110111
this.host = builder.host;
111112
this.languages = builder.languages;
113+
this.timeout = builder.timeout;
112114
this.transport = builder.transport;
113115
}
114116

@@ -127,8 +129,10 @@ private Client(Builder builder) {
127129
public static class Builder {
128130
private final int userId;
129131
private final String licenseKey;
130-
private List<String> languages = Arrays.asList("en");
132+
131133
private String host = "geoip.maxmind.com";
134+
private List<String> languages = Arrays.asList("en");
135+
private int timeout = 3000;
132136
private HttpTransport transport = new NetHttpTransport();
133137

134138
/**
@@ -142,6 +146,15 @@ public Builder(int userId, String licenseKey) {
142146
this.licenseKey = licenseKey;
143147
}
144148

149+
/**
150+
* @param val
151+
* The host to use.
152+
*/
153+
public Builder host(String val) {
154+
this.host = val;
155+
return this;
156+
}
157+
145158
/**
146159
* @param val
147160
* List of language codes to use in name property from most
@@ -155,10 +168,11 @@ public Builder languages(List<String> val) {
155168

156169
/**
157170
* @param val
158-
* The host to use.
171+
* Timeout in milliseconds for connection to web service. The
172+
* default is 3000 (3 seconds).
159173
*/
160-
public Builder host(String val) {
161-
this.host = val;
174+
public Builder timeout(int val) {
175+
this.timeout = val;
162176
return this;
163177
}
164178

@@ -298,6 +312,8 @@ private HttpResponse getResponse(GenericUrl uri) throws GeoIP2Exception {
298312
} catch (IOException e) {
299313
throw new GeoIP2Exception("Error building request", e);
300314
}
315+
request.setConnectTimeout(this.timeout);
316+
301317
request.getHeaders().setAccept("application/json");
302318
request.getHeaders().setBasicAuthentication(
303319
String.valueOf(this.userId), this.licenseKey);

0 commit comments

Comments
 (0)