Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lock initialization of AuroraHostListProvider #347

Merged
merged 5 commits into from
Feb 22, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.Properties;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Logger;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
Expand Down Expand Up @@ -109,6 +110,7 @@ public class AuroraHostListProvider implements DynamicHostListProvider {

private static final String PG_DRIVER_PROTOCOL = "postgresql";
private final String retrieveTopologyQuery;
private final ReentrantLock lock = new ReentrantLock();
protected String clusterId;
protected HostSpec clusterInstanceTemplate;
protected ConnectionUrlParser connectionUrlParser;
Expand Down Expand Up @@ -162,6 +164,12 @@ protected void init() throws SQLException {
return;
}

lock.lock();
if (this.isInitialized) {
lock.unlock();
return;
}

// initial topology is based on connection string
this.initialHostList = this.connectionUrlParser.getHostsFromConnectionUrl(this.originalUrl, false);
if (this.initialHostList == null || this.initialHostList.isEmpty()) {
Expand Down Expand Up @@ -207,6 +215,7 @@ protected void init() throws SQLException {
}

this.isInitialized = true;
lock.unlock();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might make sense to create a utility lock that implements Closable and then wrap this in try block, either way it should be in a try block with the unlock in a finally

}

/**
Expand Down