Skip to content

Commit

Permalink
Refactor: avoid generating useless InfluxDbClient
Browse files Browse the repository at this point in the history
  • Loading branch information
LinShunKang committed Jan 17, 2023
1 parent 7e2cd4c commit acfe3d0
Showing 1 changed file with 29 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,40 +10,39 @@ public final class InfluxDbClientFactory {

private static final InfluxDbConfig CONFIG = ProfilingConfig.influxDBConfig();

private static final InfluxDbV1Client V1_CLIENT = new InfluxDbV1Client.Builder()
.host(CONFIG.host())
.port(CONFIG.port())
.database(CONFIG.database())
.username(CONFIG.username())
.password(CONFIG.password())
.connectTimeout(CONFIG.connectTimeout())
.readTimeout(CONFIG.readTimeout())
.build();

private static final InfluxDbV2Client V2_CLIENT = new InfluxDbV2Client.Builder()
.host(CONFIG.host())
.port(CONFIG.port())
.orgName(CONFIG.orgName())
.database(CONFIG.database())
.username(CONFIG.username())
.password(CONFIG.password())
.connectTimeout(CONFIG.connectTimeout())
.readTimeout(CONFIG.readTimeout())
.build();

private InfluxDbClientFactory() {
//empty
private static final InfluxDbClient CLIENT = generateClient();

private static InfluxDbClient generateClient() {
final String version = CONFIG.version();
if (version.startsWith("2.")) {
return new InfluxDbV2Client.Builder()
.host(CONFIG.host())
.port(CONFIG.port())
.orgName(CONFIG.orgName())
.database(CONFIG.database())
.username(CONFIG.username())
.password(CONFIG.password())
.connectTimeout(CONFIG.connectTimeout())
.readTimeout(CONFIG.readTimeout())
.build();
} else {
return new InfluxDbV1Client.Builder()
.host(CONFIG.host())
.port(CONFIG.port())
.database(CONFIG.database())
.username(CONFIG.username())
.password(CONFIG.password())
.connectTimeout(CONFIG.connectTimeout())
.readTimeout(CONFIG.readTimeout())
.build();
}
}

public static InfluxDbClient getClient() {
return CONFIG.version().startsWith("2.") ? getV2Client() : getV1Client();
return CLIENT;
}

public static InfluxDbV1Client getV1Client() {
return V1_CLIENT;
}

public static InfluxDbV2Client getV2Client() {
return V2_CLIENT;
private InfluxDbClientFactory() {
//empty
}
}

0 comments on commit acfe3d0

Please # to comment.