use of com.google.cloud.spanner.pgadapter.metadata.ConnectionMetadata in project pgadapter by GoogleCloudPlatform.
the class ConnectionHandler method run.
/**
* Simple runner starts a loop which keeps taking inbound messages, processing them, sending them
* to Spanner, getting a result, processing that result, and replying to the client (in that
* order). Also instantiates input and output streams from the client and handles auth.
*/
@Override
public void run() {
logger.log(Level.INFO, () -> String.format("Connection handler with ID %s starting for client %s", getName(), socket.getInetAddress().getHostAddress()));
try (DataInputStream input = new DataInputStream(new BufferedInputStream(this.socket.getInputStream()));
DataOutputStream output = new DataOutputStream(new BufferedOutputStream(this.socket.getOutputStream()))) {
if (!server.getOptions().disableLocalhostCheck() && !this.socket.getInetAddress().isAnyLocalAddress() && !this.socket.getInetAddress().isLoopbackAddress()) {
handleError(output, new IllegalAccessException("This proxy may only be accessed from localhost."));
return;
}
try {
this.connectionMetadata = new ConnectionMetadata(input, output);
this.message = this.server.recordMessage(BootstrapMessage.create(this));
this.message.send();
while (this.status == ConnectionStatus.UNAUTHENTICATED) {
try {
message.nextHandler();
message.send();
} catch (EOFException eofException) {
// This indicates that the frontend terminated the connection before we got
// authenticated. This is in most cases an indication that the frontend killed the
// connection after having requested SSL and gotten an SSL denied message.
this.status = ConnectionStatus.TERMINATED;
break;
}
}
while (this.status != ConnectionStatus.TERMINATED) {
try {
message.nextHandler();
message.send();
} catch (IllegalArgumentException | IllegalStateException | EOFException fatalException) {
this.handleError(output, fatalException);
this.status = ConnectionStatus.TERMINATED;
} catch (Exception e) {
this.handleError(output, e);
}
}
} catch (Exception e) {
this.handleError(output, e);
}
} catch (Exception e) {
logger.log(Level.WARNING, e, () -> String.format("Exception on connection handler with ID %s for client %s: %s", getName(), socket.getInetAddress().getHostAddress(), e));
} finally {
logger.log(Level.INFO, () -> String.format("Closing connection handler with ID %s", getName()));
try {
if (this.spannerConnection != null) {
this.spannerConnection.close();
}
this.socket.close();
} catch (SpannerException | IOException e) {
logger.log(Level.WARNING, e, () -> String.format("Exception while closing connection handler with ID %s", getName()));
}
this.server.deregister(this);
logger.log(Level.INFO, () -> String.format("Connection handler with ID %s closed", getName()));
}
}
Aggregations