Skip to content

Commit 8fb0553

Browse files
committed
Refactor WebSocketSender API to unify connection methods
Replaced `connectAsync` with an enhanced `connect` to simplify API usage and eliminate redundancy. Updated tests and benchmarks to align with the new unified connection method, ensuring compatibility and maintaining functionality. Adjusted default auto-flush and configuration parameters for better performance.
1 parent c33eea9 commit 8fb0553

3 files changed

Lines changed: 30 additions & 118 deletions

File tree

core/src/main/java/io/questdb/client/Sender.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -885,7 +885,7 @@ public Sender build() {
885885

886886
String wsAuthHeader = buildWebSocketAuthHeader();
887887

888-
return QwpWebSocketSender.connectAsync(
888+
return QwpWebSocketSender.connect(
889889
hosts.getQuick(0),
890890
ports.getQuick(0),
891891
tlsEnabled,

core/src/main/java/io/questdb/client/cutlass/qwp/client/QwpWebSocketSender.java

Lines changed: 23 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@
6666
* <p>
6767
* Configuration options:
6868
* <ul>
69-
* <li>{@code autoFlushRows} - Maximum rows per batch (default: 500)</li>
70-
* <li>{@code autoFlushBytes} - Maximum bytes per batch (default: 1MB)</li>
69+
* <li>{@code autoFlushRows} - Maximum rows per batch (default: 1000)</li>
70+
* <li>{@code autoFlushBytes} - Maximum bytes per batch (default: 128KB)</li>
7171
* <li>{@code autoFlushIntervalNanos} - Maximum age before auto-flush (default: 100ms)</li>
7272
* </ul>
7373
* <p>
@@ -189,7 +189,7 @@ private QwpWebSocketSender(
189189

190190
/**
191191
* Creates a new sender and connects to the specified host and port.
192-
* Uses synchronous mode for backward compatibility.
192+
* Uses default auto-flush settings and in-flight window size.
193193
*
194194
* @param host server host
195195
* @param port server HTTP port (WebSocket upgrade happens on same port)
@@ -200,8 +200,8 @@ public static QwpWebSocketSender connect(String host, int port) {
200200
}
201201

202202
/**
203-
* Creates a new sender with TLS and connects to the specified host and port.
204-
* Uses synchronous mode with default auto-flush settings.
203+
* Creates a new sender and connects to the specified host and port.
204+
* Uses default auto-flush settings and in-flight window size.
205205
*
206206
* @param host server host
207207
* @param port server HTTP port
@@ -210,107 +210,29 @@ public static QwpWebSocketSender connect(String host, int port) {
210210
*/
211211
public static QwpWebSocketSender connect(String host, int port, boolean tlsEnabled) {
212212
return connect(
213-
host, port, tlsEnabled, DEFAULT_AUTO_FLUSH_ROWS, DEFAULT_AUTO_FLUSH_BYTES, DEFAULT_AUTO_FLUSH_INTERVAL_NANOS
213+
host, port, tlsEnabled,
214+
DEFAULT_AUTO_FLUSH_ROWS, DEFAULT_AUTO_FLUSH_BYTES, DEFAULT_AUTO_FLUSH_INTERVAL_NANOS,
215+
DEFAULT_IN_FLIGHT_WINDOW_SIZE, null
214216
);
215217
}
216218

217219
/**
218-
* Creates a new sender with TLS and connects to the specified host and port.
219-
* Uses synchronous mode with custom auto-flush settings.
220+
* Creates a new sender with full configuration and connects.
221+
* <p>
222+
* In-flight window size controls the flow behavior: 1 means synchronous (each batch
223+
* waits for ACK), greater than 1 enables asynchronous pipelining with a background I/O thread.
220224
*
221225
* @param host server host
222226
* @param port server HTTP port
223227
* @param tlsEnabled whether to use TLS
224228
* @param autoFlushRows rows per batch (0 = no limit)
225229
* @param autoFlushBytes bytes per batch (0 = no limit)
226230
* @param autoFlushIntervalNanos age before flush in nanos (0 = no limit)
231+
* @param inFlightWindowSize max batches awaiting server ACK (1 = sync, default: 128)
232+
* @param authorizationHeader HTTP Authorization header value, or null
227233
* @return connected sender
228234
*/
229235
public static QwpWebSocketSender connect(
230-
String host,
231-
int port,
232-
boolean tlsEnabled,
233-
int autoFlushRows,
234-
int autoFlushBytes,
235-
long autoFlushIntervalNanos
236-
) {
237-
return connect(host, port, tlsEnabled, autoFlushRows, autoFlushBytes, autoFlushIntervalNanos, null);
238-
}
239-
240-
public static QwpWebSocketSender connect(
241-
String host,
242-
int port,
243-
boolean tlsEnabled,
244-
int autoFlushRows,
245-
int autoFlushBytes,
246-
long autoFlushIntervalNanos,
247-
String authorizationHeader
248-
) {
249-
QwpWebSocketSender sender = new QwpWebSocketSender(
250-
host, port, tlsEnabled, DEFAULT_BUFFER_SIZE, autoFlushRows, autoFlushBytes, autoFlushIntervalNanos,
251-
1, // window=1 for sync behavior
252-
authorizationHeader
253-
);
254-
try {
255-
sender.ensureConnected();
256-
} catch (Throwable t) {
257-
sender.close();
258-
throw t;
259-
}
260-
return sender;
261-
}
262-
263-
/**
264-
* Creates a new sender with async mode and custom configuration.
265-
*
266-
* @param host server host
267-
* @param port server HTTP port
268-
* @param tlsEnabled whether to use TLS
269-
* @param autoFlushRows rows per batch (0 = no limit)
270-
* @param autoFlushBytes bytes per batch (0 = no limit)
271-
* @param autoFlushIntervalNanos age before flush in nanos (0 = no limit)
272-
* @return connected sender
273-
*/
274-
public static QwpWebSocketSender connectAsync(
275-
String host,
276-
int port,
277-
boolean tlsEnabled,
278-
int autoFlushRows,
279-
int autoFlushBytes,
280-
long autoFlushIntervalNanos
281-
) {
282-
return connectAsync(
283-
host, port, tlsEnabled, autoFlushRows, autoFlushBytes, autoFlushIntervalNanos, DEFAULT_IN_FLIGHT_WINDOW_SIZE
284-
);
285-
}
286-
287-
/**
288-
* Creates a new sender with async mode and full configuration including flow control.
289-
*
290-
* @param host server host
291-
* @param port server HTTP port
292-
* @param tlsEnabled whether to use TLS
293-
* @param autoFlushRows rows per batch (0 = no limit)
294-
* @param autoFlushBytes bytes per batch (0 = no limit)
295-
* @param autoFlushIntervalNanos age before flush in nanos (0 = no limit)
296-
* @param inFlightWindowSize max batches awaiting server ACK (default: 8)
297-
* @return connected sender
298-
*/
299-
public static QwpWebSocketSender connectAsync(
300-
String host,
301-
int port,
302-
boolean tlsEnabled,
303-
int autoFlushRows,
304-
int autoFlushBytes,
305-
long autoFlushIntervalNanos,
306-
int inFlightWindowSize
307-
) {
308-
return connectAsync(
309-
host, port, tlsEnabled, autoFlushRows, autoFlushBytes, autoFlushIntervalNanos, inFlightWindowSize, null
310-
);
311-
}
312-
313-
public static QwpWebSocketSender connectAsync(
314236
String host,
315237
int port,
316238
boolean tlsEnabled,
@@ -321,7 +243,9 @@ public static QwpWebSocketSender connectAsync(
321243
String authorizationHeader
322244
) {
323245
QwpWebSocketSender sender = new QwpWebSocketSender(
324-
host, port, tlsEnabled, DEFAULT_BUFFER_SIZE, autoFlushRows, autoFlushBytes, autoFlushIntervalNanos, inFlightWindowSize, authorizationHeader
246+
host, port, tlsEnabled, DEFAULT_BUFFER_SIZE,
247+
autoFlushRows, autoFlushBytes, autoFlushIntervalNanos,
248+
inFlightWindowSize, authorizationHeader
325249
);
326250
try {
327251
sender.ensureConnected();
@@ -332,20 +256,6 @@ public static QwpWebSocketSender connectAsync(
332256
return sender;
333257
}
334258

335-
/**
336-
* Creates a new sender with async mode and default configuration.
337-
*
338-
* @param host server host
339-
* @param port server HTTP port
340-
* @param tlsEnabled whether to use TLS
341-
* @return connected sender
342-
*/
343-
public static QwpWebSocketSender connectAsync(String host, int port, boolean tlsEnabled) {
344-
return connectAsync(
345-
host, port, tlsEnabled, DEFAULT_AUTO_FLUSH_ROWS, DEFAULT_AUTO_FLUSH_BYTES, DEFAULT_AUTO_FLUSH_INTERVAL_NANOS
346-
);
347-
}
348-
349259
/**
350260
* Creates a sender without connecting. For testing only.
351261
* <p>
@@ -359,9 +269,10 @@ public static QwpWebSocketSender connectAsync(String host, int port, boolean tls
359269
*/
360270
public static QwpWebSocketSender createForTesting(String host, int port, int inFlightWindowSize) {
361271
return new QwpWebSocketSender(
362-
host, port, false, DEFAULT_BUFFER_SIZE, DEFAULT_AUTO_FLUSH_ROWS, DEFAULT_AUTO_FLUSH_BYTES, DEFAULT_AUTO_FLUSH_INTERVAL_NANOS, inFlightWindowSize, null
272+
host, port, false, DEFAULT_BUFFER_SIZE,
273+
DEFAULT_AUTO_FLUSH_ROWS, DEFAULT_AUTO_FLUSH_BYTES, DEFAULT_AUTO_FLUSH_INTERVAL_NANOS,
274+
inFlightWindowSize, null
363275
);
364-
// Note: does NOT call ensureConnected()
365276
}
366277

367278
/**
@@ -384,9 +295,10 @@ public static QwpWebSocketSender createForTesting(
384295
int inFlightWindowSize
385296
) {
386297
return new QwpWebSocketSender(
387-
host, port, false, DEFAULT_BUFFER_SIZE, autoFlushRows, autoFlushBytes, autoFlushIntervalNanos, inFlightWindowSize, null
298+
host, port, false, DEFAULT_BUFFER_SIZE,
299+
autoFlushRows, autoFlushBytes, autoFlushIntervalNanos,
300+
inFlightWindowSize, null
388301
);
389-
// Note: does NOT call ensureConnected()
390302
}
391303

392304
@Override

core/src/test/java/io/questdb/client/test/cutlass/qwp/client/QwpWebSocketAckIntegrationTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ public void testAsyncFlushFailsFastOnInvalidAckPayload() throws Exception {
5555

5656
boolean errorCaught = false;
5757
long start = System.currentTimeMillis();
58-
try (QwpWebSocketSender sender = QwpWebSocketSender.connectAsync(
59-
"localhost", port, false, 0, 0, 0)) {
58+
try (QwpWebSocketSender sender = QwpWebSocketSender.connect(
59+
"localhost", port, false, 0, 0, 0, QwpWebSocketSender.DEFAULT_IN_FLIGHT_WINDOW_SIZE, null)) {
6060
sender.table("test")
6161
.longColumn("value", 1)
6262
.atNow();
@@ -86,8 +86,8 @@ public void testAsyncFlushFailsFastOnServerClose() throws Exception {
8686

8787
boolean errorCaught = false;
8888
long start = System.currentTimeMillis();
89-
try (QwpWebSocketSender sender = QwpWebSocketSender.connectAsync(
90-
"localhost", port, false, 0, 0, 0)) {
89+
try (QwpWebSocketSender sender = QwpWebSocketSender.connect(
90+
"localhost", port, false, 0, 0, 0, QwpWebSocketSender.DEFAULT_IN_FLIGHT_WINDOW_SIZE, null)) {
9191
sender.table("test")
9292
.longColumn("value", 1)
9393
.atNow();
@@ -121,8 +121,8 @@ public void testFlushBlocksUntilAcked() throws Exception {
121121
server.start();
122122
Assert.assertTrue("Server failed to start", server.awaitStart(5, TimeUnit.SECONDS));
123123

124-
try (QwpWebSocketSender sender = QwpWebSocketSender.connectAsync(
125-
"localhost", port, false, 0, 0, 0)) {
124+
try (QwpWebSocketSender sender = QwpWebSocketSender.connect(
125+
"localhost", port, false, 0, 0, 0, QwpWebSocketSender.DEFAULT_IN_FLIGHT_WINDOW_SIZE, null)) {
126126

127127
sender.table("test")
128128
.longColumn("value", 42)

0 commit comments

Comments
 (0)