Skip to content

Commit ed3a2fb

Browse files
authored
fix ipv6 for grpc (#631)
* fix ipv6 for grpc * fmt
1 parent 318726f commit ed3a2fb

File tree

1 file changed

+40
-33
lines changed

1 file changed

+40
-33
lines changed

client/src/main/java/com/vesoft/nebula/driver/graph/net/GrpcConnection.java

Lines changed: 40 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -52,26 +52,30 @@ public void open(HostAddress address,
5252
this.serverAddr = address;
5353
this.connectTimeout = builder.connectTimeoutMills;
5454
this.requestTimeout = builder.requestTimeoutMills;
55+
String formattedHost = address.getHost();
56+
if (formattedHost.contains(":") && !formattedHost.startsWith("[")) {
57+
formattedHost = "[" + formattedHost + "]";
58+
}
5559
if (builder.enableTls) {
5660
try {
5761
NettyChannelBuilder channelBuilder = NettyChannelBuilder
58-
.forAddress(address.getHost(), address.getPort())
59-
.useTransportSecurity()
60-
.sslContext(TlsUtil.getSslContext(builder.disableVerifyServerCert,
61-
builder.tlsCa,
62-
builder.tlsCert,
63-
builder.tlsKey))
64-
.maxInboundMessageSize(Integer.MAX_VALUE);
62+
.forAddress(formattedHost, address.getPort())
63+
.useTransportSecurity()
64+
.sslContext(TlsUtil.getSslContext(builder.disableVerifyServerCert,
65+
builder.tlsCa,
66+
builder.tlsCert,
67+
builder.tlsKey))
68+
.maxInboundMessageSize(Integer.MAX_VALUE);
6569
channel = channelBuilder.build();
6670
} catch (SSLException e) {
6771
throw new IOErrorException(IOErrorException.E_SSL_ERROR, e.getMessage());
6872
}
6973
} else {
7074
channel = NettyChannelBuilder
71-
.forAddress(address.getHost(), address.getPort())
72-
.usePlaintext()
73-
.maxInboundMessageSize(Integer.MAX_VALUE)
74-
.build();
75+
.forAddress(formattedHost, address.getPort())
76+
.usePlaintext()
77+
.maxInboundMessageSize(Integer.MAX_VALUE)
78+
.build();
7579
}
7680
stub = GraphServiceGrpc.newBlockingStub(channel);
7781
}
@@ -88,32 +92,32 @@ public void close() {
8892
public boolean ping(long sessionID, long timeoutMs) throws IOErrorException {
8993
ExecuteResponse response = execute(sessionID, "RETURN 1", timeoutMs);
9094
return ErrorCode.SUCCESSFUL_COMPLETION.code
91-
.equals(response.getStatus().getCode().toString(charset));
95+
.equals(response.getStatus().getCode().toString(charset));
9296
}
9397

9498
public AuthResult authenticate(String user, Map<String, Object> authOptions)
95-
throws AuthFailedException, IOErrorException {
99+
throws AuthFailedException, IOErrorException {
96100
try {
97101
ClientInfo clientInfo = ClientInfo.newBuilder()
98-
.setLang(ClientInfo.Language.JAVA)
99-
.setProtocolVersion(Common
100-
.getDescriptor()
101-
.getOptions()
102-
.getExtension(Common.protocolVersion))
103-
.setVersion(ByteString.copyFrom(ClientVersion.clientVersion, charset))
104-
.build();
102+
.setLang(ClientInfo.Language.JAVA)
103+
.setProtocolVersion(Common
104+
.getDescriptor()
105+
.getOptions()
106+
.getExtension(Common.protocolVersion))
107+
.setVersion(ByteString.copyFrom(ClientVersion.clientVersion, charset))
108+
.build();
105109
ByteString userString = user == null ? ByteString.copyFrom("", charset)
106-
: ByteString.copyFrom(user, charset);
110+
: ByteString.copyFrom(user, charset);
107111
String authInfoString = JSON.toJSONString(authOptions);
108112
AuthRequest authReq = AuthRequest.newBuilder()
109-
.setUsername(userString)
110-
.setAuthInfo(ByteString.copyFrom(authInfoString, charset))
111-
.setClientInfo(clientInfo)
112-
.build();
113+
.setUsername(userString)
114+
.setAuthInfo(ByteString.copyFrom(authInfoString, charset))
115+
.setClientInfo(clientInfo)
116+
.build();
113117

114118
AuthResponse resp = stub
115-
.withDeadlineAfter(connectTimeout, TimeUnit.MILLISECONDS)
116-
.authenticate(authReq);
119+
.withDeadlineAfter(connectTimeout, TimeUnit.MILLISECONDS)
120+
.authenticate(authReq);
117121
String code = resp.getStatus().getCode().toString(charset);
118122
if (!ErrorCode.SUCCESSFUL_COMPLETION.code.equals(code)) {
119123
close();
@@ -123,7 +127,8 @@ public AuthResult authenticate(String user, Map<String, Object> authOptions)
123127
} catch (Exception e) {
124128
close();
125129
if (e instanceof StatusRuntimeException
126-
&& (((StatusRuntimeException) e).getStatus().getCode() == Code.DEADLINE_EXCEEDED)) {
130+
&& (((StatusRuntimeException) e)
131+
.getStatus().getCode() == Code.DEADLINE_EXCEEDED)) {
127132
throw new AuthFailedException(String.format("authenticate to %s timeout after %dms",
128133
serverAddr.toString(),
129134
connectTimeout));
@@ -136,20 +141,22 @@ public AuthResult authenticate(String user, Map<String, Object> authOptions)
136141
}
137142

138143
public ExecuteResponse execute(long sessionID, String stmt, long timeout)
139-
throws IOErrorException {
144+
throws IOErrorException {
140145
if (stmt == null) {
141146
throw new NullPointerException("statement is null.");
142147
}
143148
try {
144149
ExecuteRequest request = ExecuteRequest.newBuilder()
145-
.setSessionId(sessionID)
146-
.setStmt(ByteString.copyFrom(stmt, charset))
147-
.build();
150+
.setSessionId(sessionID)
151+
.setStmt(ByteString.copyFrom(stmt, charset))
152+
.build();
148153

149154
return stub.withDeadlineAfter(timeout, TimeUnit.MILLISECONDS).execute(request);
150155
} catch (Exception e) {
151156
if (e instanceof StatusRuntimeException
152-
&& (((StatusRuntimeException) e).getStatus().getCode() == Code.DEADLINE_EXCEEDED)) {
157+
&& (((StatusRuntimeException) e)
158+
.getStatus()
159+
.getCode() == Code.DEADLINE_EXCEEDED)) {
153160
throw new IOErrorException(E_TIME_OUT,
154161
String.format("request to %s timeout after %dms",
155162
serverAddr.toString(),

0 commit comments

Comments
 (0)