Skip to content
This repository was archived by the owner on May 30, 2024. It is now read-only.

Commit 314aee7

Browse files
committed
Add javadoc, and make the flush interval configurable
1 parent f3a2c42 commit 314aee7

File tree

2 files changed

+99
-41
lines changed

2 files changed

+99
-41
lines changed

src/main/java/com/launchdarkly/client/EventProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class EventProcessor implements Closeable {
2525
EventProcessor(String apiKey, LDConfig config) {
2626
this.apiKey = apiKey;
2727
this.queue = new ArrayBlockingQueue<Event>(config.capacity);
28-
this.scheduler.scheduleAtFixedRate(new Consumer(config), 0, 10, TimeUnit.SECONDS);
28+
this.scheduler.scheduleAtFixedRate(new Consumer(config), 0, config.flushInterval, TimeUnit.SECONDS);
2929
}
3030

3131
boolean sendEvent(Event e) {

src/main/java/com/launchdarkly/client/LDConfig.java

Lines changed: 98 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@
99
import java.net.URI;
1010

1111
/**
12-
* This class exposes advanced configuration options for the {@link LDClient}.
12+
* This class exposes advanced configuration options for the {@link LDClient}. Instances of this class must be constructed with a {@link com.launchdarkly.client.LDConfig.Builder}.
1313
*
1414
*/
1515
public final class LDConfig {
1616
private static final URI DEFAULT_BASE_URI = URI.create("https://app.launchdarkly.com");
1717
private static final int DEFAULT_CAPACITY = 10000;
18-
private static final int DEFAULT_CONNECT_TIMEOUT = 3;
19-
private static final int DEFAULT_SOCKET_TIMEOUT = 3;
18+
private static final int DEFAULT_CONNECT_TIMEOUT = 2;
19+
private static final int DEFAULT_SOCKET_TIMEOUT = 10;
20+
private static final int DEFAULT_FLUSH_INTERVAL = 5;
2021
private static final Logger logger = LoggerFactory.getLogger(LDConfig.class);
2122

2223
protected static final LDConfig DEFAULT = new Builder().build();
@@ -25,12 +26,106 @@ public final class LDConfig {
2526
final int capacity;
2627
final int connectTimeout;
2728
final int socketTimeout;
29+
final int flushInterval;
2830

2931
protected LDConfig(Builder builder) {
3032
this.baseURI = builder.baseURI;
3133
this.capacity = builder.capacity;
3234
this.connectTimeout = builder.connectTimeout;
3335
this.socketTimeout = builder.socketTimeout;
36+
this.flushInterval = builder.flushInterval;
37+
}
38+
39+
/**
40+
* A <a href="http://en.wikipedia.org/wiki/Builder_pattern">builder</a> that helps construct {@link com.launchdarkly.client.LDConfig} objects. Builder
41+
* calls can be chained, enabling the following pattern:
42+
* <p>
43+
* <pre>
44+
* LDConfig config = new LDConfig.Builder()
45+
* .connectTimeout(3)
46+
* .socketTimeout(3)
47+
* .build()
48+
* </pre>
49+
* </p>
50+
*
51+
*/
52+
public static class Builder{
53+
private URI baseURI = DEFAULT_BASE_URI;
54+
private int connectTimeout = DEFAULT_CONNECT_TIMEOUT;
55+
private int socketTimeout = DEFAULT_SOCKET_TIMEOUT;
56+
private int capacity = DEFAULT_CAPACITY;
57+
private int flushInterval = DEFAULT_FLUSH_INTERVAL;
58+
59+
/**
60+
* Creates a builder with all configuration parameters set to the default
61+
*/
62+
public Builder() {
63+
}
64+
65+
/**
66+
* Set the base URL of the LaunchDarkly server for this configuration
67+
* @param baseURI the base URL of the LaunchDarkly server for this configuration
68+
* @return the builder
69+
*/
70+
public Builder baseURI(URI baseURI) {
71+
this.baseURI = baseURI;
72+
return this;
73+
}
74+
75+
/**
76+
* Set the connection timeout in seconds for the configuration. This is the time allowed for the underlying HTTP client to connect
77+
* to the LaunchDarkly server. The default is 2 seconds.
78+
* @param connectTimeout the connection timeout in seconds
79+
* @return the builder
80+
*/
81+
public Builder connectTimeout(int connectTimeout) {
82+
this.connectTimeout = connectTimeout;
83+
return this;
84+
}
85+
86+
/**
87+
* Set the socket timeout in seconds for the configuration. This is the time allowed for the server to return a response after
88+
* the connection is established. The default is 10 seconds.
89+
* @param socketTimeout the socket timeout in seconds
90+
* @return the builder
91+
*/
92+
public Builder socketTimeout(int socketTimeout) {
93+
this.socketTimeout = socketTimeout;
94+
return this;
95+
}
96+
97+
/**
98+
* Set the number of seconds between flushes of the event buffer. Decreasing the flush interval means
99+
* that the event buffer is less likely to reach capacity.
100+
*
101+
* @param flushInterval the flush interval in seconds
102+
* @return the builder
103+
*/
104+
public Builder flushInterval(int flushInterval) {
105+
this.flushInterval = flushInterval;
106+
return this;
107+
}
108+
109+
/**
110+
* Set the capacity of the events buffer. The client buffers up to this many events in memory before flushing. If the capacity is exceeded before the buffer is flushed, events will be discarded.
111+
* Increasing the capacity means that events are less likely to be discarded, at the cost of consuming more memory.
112+
*
113+
* @param capacity the capacity of the event buffer
114+
* @return the builder
115+
*/
116+
public Builder capacity(int capacity) {
117+
this.capacity = capacity;
118+
return this;
119+
}
120+
121+
/**
122+
* Build the configured {@link com.launchdarkly.client.LDConfig} object
123+
* @return the {@link com.launchdarkly.client.LDConfig} configured by this builder
124+
*/
125+
public LDConfig build() {
126+
return new LDConfig(this);
127+
}
128+
34129
}
35130

36131

@@ -72,41 +167,4 @@ HttpPost postRequest(String apiKey, String path) {
72167
return null;
73168
}
74169
}
75-
76-
77-
public static class Builder{
78-
private URI baseURI = DEFAULT_BASE_URI;
79-
private int connectTimeout = DEFAULT_CONNECT_TIMEOUT;
80-
private int socketTimeout = DEFAULT_SOCKET_TIMEOUT;
81-
private int capacity = DEFAULT_CAPACITY;
82-
83-
public Builder() {
84-
}
85-
86-
public Builder baseURI(URI baseURI) {
87-
this.baseURI = baseURI;
88-
return this;
89-
}
90-
91-
public Builder connectTimeout(int connectTimeout) {
92-
this.connectTimeout = connectTimeout;
93-
return this;
94-
}
95-
96-
public Builder socketTimeout(int socketTimeout) {
97-
this.socketTimeout = socketTimeout;
98-
return this;
99-
}
100-
101-
public Builder capacity(int capacity) {
102-
this.capacity = capacity;
103-
return this;
104-
}
105-
106-
public LDConfig build() {
107-
return new LDConfig(this);
108-
}
109-
110-
}
111-
112170
}

0 commit comments

Comments
 (0)