|
@@ -20,17 +20,26 @@
|
|
|
package io.milvus.client;
|
|
|
|
|
|
import javax.annotation.Nonnull;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
/** Contains parameters for connecting to Milvus server */
|
|
|
public class ConnectParam {
|
|
|
private final String host;
|
|
|
private final String port;
|
|
|
- private final long timeout;
|
|
|
+ private final long connectTimeoutNanos;
|
|
|
+ private final long keepAliveTimeNanos;
|
|
|
+ private final long keepAliveTimeoutNanos;
|
|
|
+ private final boolean keepAliveWithoutCalls;
|
|
|
+ private final long idleTimeoutNanos;
|
|
|
|
|
|
private ConnectParam(@Nonnull Builder builder) {
|
|
|
this.host = builder.host;
|
|
|
this.port = builder.port;
|
|
|
- this.timeout = builder.timeout;
|
|
|
+ this.connectTimeoutNanos = builder.connectTimeoutNanos;
|
|
|
+ this.keepAliveTimeNanos = builder.keepAliveTimeNanos;
|
|
|
+ this.keepAliveTimeoutNanos = builder.keepAliveTimeoutNanos;
|
|
|
+ this.keepAliveWithoutCalls = builder.keepAliveWithoutCalls;
|
|
|
+ this.idleTimeoutNanos = builder.idleTimeoutNanos;
|
|
|
}
|
|
|
|
|
|
public String getHost() {
|
|
@@ -41,24 +50,61 @@ public class ConnectParam {
|
|
|
return port;
|
|
|
}
|
|
|
|
|
|
- public long getTimeout() {
|
|
|
- return timeout;
|
|
|
+ public long getConnectTimeout(@Nonnull TimeUnit timeUnit) {
|
|
|
+ return timeUnit.convert(connectTimeoutNanos, TimeUnit.NANOSECONDS);
|
|
|
+ }
|
|
|
+
|
|
|
+ public long getKeepAliveTime(@Nonnull TimeUnit timeUnit) {
|
|
|
+ return timeUnit.convert(keepAliveTimeNanos, TimeUnit.NANOSECONDS);
|
|
|
+ }
|
|
|
+
|
|
|
+ public long getKeepAliveTimeout(@Nonnull TimeUnit timeUnit) {
|
|
|
+ return timeUnit.convert(keepAliveTimeoutNanos, TimeUnit.NANOSECONDS);
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean isKeepAliveWithoutCalls() {
|
|
|
+ return keepAliveWithoutCalls;
|
|
|
+ }
|
|
|
+
|
|
|
+ public long getIdleTimeout(@Nonnull TimeUnit timeUnit) {
|
|
|
+ return timeUnit.convert(idleTimeoutNanos, TimeUnit.NANOSECONDS);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public String toString() {
|
|
|
- return "ConnectParam{" + "host='" + host + '\'' + ", port='" + port + '\'' + '}';
|
|
|
+ return "ConnectParam {"
|
|
|
+ + "host='"
|
|
|
+ + host
|
|
|
+ + '\''
|
|
|
+ + ", port='"
|
|
|
+ + port
|
|
|
+ + '\''
|
|
|
+ + ", connectTimeoutNanos="
|
|
|
+ + connectTimeoutNanos
|
|
|
+ + ", keepAliveTimeNanos="
|
|
|
+ + keepAliveTimeNanos
|
|
|
+ + ", keepAliveTimeoutNanos="
|
|
|
+ + keepAliveTimeoutNanos
|
|
|
+ + ", keepAliveWithoutCalls="
|
|
|
+ + keepAliveWithoutCalls
|
|
|
+ + ", idleTimeoutNanos="
|
|
|
+ + idleTimeoutNanos
|
|
|
+ + '}';
|
|
|
}
|
|
|
|
|
|
/** Builder for <code>ConnectParam</code> */
|
|
|
public static class Builder {
|
|
|
// Optional parameters - initialized to default values
|
|
|
- private String host = "127.0.0.1";
|
|
|
+ private String host = "localhost";
|
|
|
private String port = "19530";
|
|
|
- private long timeout = 10000; // ms
|
|
|
+ private long connectTimeoutNanos = TimeUnit.NANOSECONDS.convert(10, TimeUnit.SECONDS);
|
|
|
+ private long keepAliveTimeNanos = Long.MAX_VALUE; // Disabling keepalive
|
|
|
+ private long keepAliveTimeoutNanos = TimeUnit.NANOSECONDS.convert(20, TimeUnit.SECONDS);
|
|
|
+ private boolean keepAliveWithoutCalls = false;
|
|
|
+ private long idleTimeoutNanos = TimeUnit.NANOSECONDS.convert(24, TimeUnit.HOURS);
|
|
|
|
|
|
/**
|
|
|
- * Optional. Default to "127.0.0.1".
|
|
|
+ * Optional. Defaults to "localhost".
|
|
|
*
|
|
|
* @param host server host
|
|
|
* @return <code>Builder</code>
|
|
@@ -69,24 +115,108 @@ public class ConnectParam {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Optional. Default to "19530".
|
|
|
+ * Optional. Defaults to "19530".
|
|
|
*
|
|
|
* @param port server port
|
|
|
* @return <code>Builder</code>
|
|
|
*/
|
|
|
- public Builder withPort(@Nonnull String port) {
|
|
|
+ public Builder withPort(@Nonnull String port) throws IllegalArgumentException {
|
|
|
+ int portInt = Integer.parseInt(port);
|
|
|
+ if (portInt < 0 || portInt > 0xFFFF) {
|
|
|
+ throw new IllegalArgumentException("Port is out of range!");
|
|
|
+ }
|
|
|
this.port = port;
|
|
|
return this;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Optional. Default to 10000 ms
|
|
|
+ * Optional. Defaults to 10 seconds.
|
|
|
+ *
|
|
|
+ * @param connectTimeout Timeout for client to establish a connection to server
|
|
|
+ * @return <code>Builder</code>
|
|
|
+ * @throws IllegalArgumentException
|
|
|
+ */
|
|
|
+ public Builder withConnectTimeout(long connectTimeout, @Nonnull TimeUnit timeUnit)
|
|
|
+ throws IllegalArgumentException {
|
|
|
+ if (connectTimeout <= 0L) {
|
|
|
+ throw new IllegalArgumentException("Connect timeout must be positive!");
|
|
|
+ }
|
|
|
+ connectTimeoutNanos = timeUnit.toNanos(connectTimeout);
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Optional. Sets the time without read activity before sending a keepalive ping. An
|
|
|
+ * unreasonably small value might be increased, and Long.MAX_VALUE nano seconds or an
|
|
|
+ * unreasonably large value will disable keepalive. Defaults to infinite.
|
|
|
+ *
|
|
|
+ * @see <a
|
|
|
+ * href="https://grpc.github.io/grpc-java/javadoc/io/grpc/ManagedChannelBuilder.html#keepAliveTime-long-java.util.concurrent.TimeUnit-">
|
|
|
+ * GRPC keepAliveTime Javadoc</a>
|
|
|
+ * @return <code>Builder</code>
|
|
|
+ * @throws IllegalArgumentException
|
|
|
+ */
|
|
|
+ public Builder withKeepAliveTime(long keepAliveTime, @Nonnull TimeUnit timeUnit)
|
|
|
+ throws IllegalArgumentException {
|
|
|
+ if (keepAliveTime <= 0L) {
|
|
|
+ throw new IllegalArgumentException("Keepalive time must be positive!");
|
|
|
+ }
|
|
|
+ keepAliveTimeNanos = timeUnit.toNanos(keepAliveTime);
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Optional. Sets the time waiting for read activity after sending a keepalive ping. If the time
|
|
|
+ * expires without any read activity on the connection, the connection is considered dead. An
|
|
|
+ * unreasonably small value might be increased. Defaults to 20 seconds.
|
|
|
+ *
|
|
|
+ * <p>This value should be at least multiple times the RTT to allow for lost packets.
|
|
|
+ *
|
|
|
+ * @see <a
|
|
|
+ * href="https://grpc.github.io/grpc-java/javadoc/io/grpc/ManagedChannelBuilder.html#keepAliveTimeout-long-java.util.concurrent.TimeUnit-">
|
|
|
+ * GRPC keepAliveTimeout Javadoc</a>
|
|
|
+ * @return <code>Builder</code>
|
|
|
+ * @throws IllegalArgumentException
|
|
|
+ */
|
|
|
+ public Builder withKeepAliveTimeout(long keepAliveTimeout, @Nonnull TimeUnit timeUnit)
|
|
|
+ throws IllegalArgumentException {
|
|
|
+ if (keepAliveTimeout <= 0L) {
|
|
|
+ throw new IllegalArgumentException("Keepalive timeout must be positive!");
|
|
|
+ }
|
|
|
+ keepAliveTimeoutNanos = timeUnit.toNanos(keepAliveTimeout);
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Optional. Sets whether keepalive will be performed when there are no outstanding RPC on a
|
|
|
+ * connection. Defaults to false.
|
|
|
+ *
|
|
|
+ * @see <a
|
|
|
+ * href="https://grpc.github.io/grpc-java/javadoc/io/grpc/ManagedChannelBuilder.html#keepAliveWithoutCalls-boolean-">
|
|
|
+ * GRPC keepAliveWithoutCalls Javadoc</a>
|
|
|
+ * @return <code>Builder</code>
|
|
|
+ */
|
|
|
+ public Builder keepAliveWithoutCalls(boolean enable) {
|
|
|
+ keepAliveWithoutCalls = enable;
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Optional. Set the duration without ongoing RPCs before going to idle mode. Defaults to 24
|
|
|
+ * hour.
|
|
|
*
|
|
|
- * @param timeout Timeout in ms for client to establish a connection to server
|
|
|
+ * @see <a
|
|
|
+ * href="https://grpc.github.io/grpc-java/javadoc/io/grpc/ManagedChannelBuilder.html#idleTimeout-long-java.util.concurrent.TimeUnit-">
|
|
|
+ * GRPC idleTimeout Javadoc</a>
|
|
|
* @return <code>Builder</code>
|
|
|
+ * @throws IllegalArgumentException
|
|
|
*/
|
|
|
- public Builder withTimeout(long timeout) {
|
|
|
- this.timeout = timeout;
|
|
|
+ public Builder withIdleTimeout(long idleTimeout, TimeUnit timeUnit)
|
|
|
+ throws IllegalArgumentException {
|
|
|
+ if (idleTimeout <= 0L) {
|
|
|
+ throw new IllegalArgumentException("Idle timeout must be positive!");
|
|
|
+ }
|
|
|
+ idleTimeoutNanos = timeUnit.toNanos(idleTimeout);
|
|
|
return this;
|
|
|
}
|
|
|
|