Skip to content

Commit ba46488

Browse files
committed
The connection startup message should only be sent optionally, when cancelling a request it should not be sent.
1 parent ce6baa2 commit ba46488

File tree

1 file changed

+25
-21
lines changed

1 file changed

+25
-21
lines changed

vertx-pg-client/src/main/java/io/vertx/pgclient/impl/PgConnectionFactory.java

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,11 @@ protected Future<Connection> doConnectInternal(SqlConnectOptions options, Contex
7676
return context.failedFuture(e);
7777
}
7878
SocketAddress server = options.getSocketAddress();
79-
return doConnect(server, context, options);
79+
return connect(server, context, true, pgOptions);
8080
}
8181

8282
public void cancelRequest(PgConnectOptions options, int processId, int secretKey, Handler<AsyncResult<Void>> handler) {
83-
doConnect(options.getSocketAddress(), vertx.createEventLoopContext(), options).onComplete(ar -> {
83+
connect(options.getSocketAddress(), vertx.createEventLoopContext(), false, options).onComplete(ar -> {
8484
if (ar.succeeded()) {
8585
PgSocketConnection conn = (PgSocketConnection) ar.result();
8686
conn.sendCancelRequestMessage(processId, secretKey, handler);
@@ -90,52 +90,56 @@ public void cancelRequest(PgConnectOptions options, int processId, int secretKey
9090
});
9191
}
9292

93-
private Future<Connection> doConnect(SocketAddress server, ContextInternal context, PgConnectOptions options) {
93+
private Future<Connection> connect(SocketAddress server, ContextInternal context, boolean sendStartupMessage, PgConnectOptions options) {
9494
SslMode sslMode = options.isUsingDomainSocket() ? SslMode.DISABLE : options.getSslMode();
9595
Future<Connection> connFuture;
9696
switch (sslMode) {
9797
case DISABLE:
98-
connFuture = doConnect(server, options, context, false, options);
98+
connFuture = connect(server, options, context, false, sendStartupMessage);
9999
break;
100100
case ALLOW:
101-
connFuture = doConnect(server, options, context,false, options).recover(err -> doConnect(server, options, context,true, options));
101+
connFuture = connect(server, options, context,false, sendStartupMessage).recover(err -> connect(server, options, context, true, sendStartupMessage));
102102
break;
103103
case PREFER:
104-
connFuture = doConnect(server, options, context,true, options).recover(err -> doConnect(server, options, context,false, options));
104+
connFuture = connect(server, options, context,true, sendStartupMessage).recover(err -> connect(server, options, context, false, sendStartupMessage));
105105
break;
106106
case REQUIRE:
107107
case VERIFY_CA:
108108
case VERIFY_FULL:
109-
connFuture = doConnect(server, options, context, true, options);
109+
connFuture = connect(server, options, context, true, sendStartupMessage);
110110
break;
111111
default:
112112
return context.failedFuture(new IllegalArgumentException("Unsupported SSL mode"));
113113
}
114114
return connFuture;
115115
}
116116

117-
private Future<Connection> doConnect(SocketAddress server, PgConnectOptions connectOptions, ContextInternal context, boolean ssl, PgConnectOptions options) {
118-
return doConnect_(server, connectOptions, context, ssl, options).flatMap(conn -> {
119-
String username = options.getUser();
120-
String password = options.getPassword();
121-
String database = options.getDatabase();
122-
Map<String, String> properties = options.getProperties() != null ? Collections.unmodifiableMap(options.getProperties()) : null;
123-
PgSocketConnection socket = (PgSocketConnection) conn;
124-
socket.init();
125-
return Future.<Connection>future(p -> socket.sendStartupMessage(username, password, database, properties, p))
126-
.map(conn);
127-
});
117+
private Future<Connection> connect(SocketAddress server, PgConnectOptions connectOptions, ContextInternal context, boolean ssl, boolean sendStartupMessage) {
118+
Future<Connection> res = doConnect(server, connectOptions, context, ssl);
119+
if (sendStartupMessage) {
120+
return res.flatMap(conn -> {
121+
PgSocketConnection socket = (PgSocketConnection) conn;
122+
socket.init();
123+
String username = connectOptions.getUser();
124+
String password = connectOptions.getPassword();
125+
String database = connectOptions.getDatabase();
126+
Map<String, String> properties = connectOptions.getProperties() != null ? Collections.unmodifiableMap(connectOptions.getProperties()) : null;
127+
return Future.future(p -> socket.sendStartupMessage(username, password, database, properties, p));
128+
});
129+
} else {
130+
return res;
131+
}
128132
}
129133

130-
private Future<Connection> doConnect_(SocketAddress server, PgConnectOptions connectOptions, ContextInternal context, boolean ssl, PgConnectOptions options) {
134+
private Future<Connection> doConnect(SocketAddress server, PgConnectOptions connectOptions, ContextInternal context, boolean ssl) {
131135
Future<NetSocket> soFut;
132136
try {
133-
soFut = netClient(options).connect(server, (String) null);
137+
soFut = netClient(connectOptions).connect(server, (String) null);
134138
} catch (Exception e) {
135139
// Client is closed
136140
return context.failedFuture(e);
137141
}
138-
Future<Connection> connFut = soFut.map(so -> newSocketConnection(context, (NetSocketInternal) so, options));
142+
Future<Connection> connFut = soFut.map(so -> newSocketConnection(context, (NetSocketInternal) so, connectOptions));
139143
if (ssl && !server.isDomainSocket()) {
140144
// upgrade connection to SSL if needed
141145
connFut = connFut.flatMap(conn -> Future.future(p -> {

0 commit comments

Comments
 (0)