Skip to content

Commit acfe3d0

Browse files
committed
Refactor: avoid generating useless InfluxDbClient
1 parent 7e2cd4c commit acfe3d0

File tree

1 file changed

+29
-30
lines changed

1 file changed

+29
-30
lines changed

MyPerf4J-Base/src/main/java/cn/myperf4j/base/influxdb/InfluxDbClientFactory.java

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,40 +10,39 @@ public final class InfluxDbClientFactory {
1010

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

13-
private static final InfluxDbV1Client V1_CLIENT = new InfluxDbV1Client.Builder()
14-
.host(CONFIG.host())
15-
.port(CONFIG.port())
16-
.database(CONFIG.database())
17-
.username(CONFIG.username())
18-
.password(CONFIG.password())
19-
.connectTimeout(CONFIG.connectTimeout())
20-
.readTimeout(CONFIG.readTimeout())
21-
.build();
22-
23-
private static final InfluxDbV2Client V2_CLIENT = new InfluxDbV2Client.Builder()
24-
.host(CONFIG.host())
25-
.port(CONFIG.port())
26-
.orgName(CONFIG.orgName())
27-
.database(CONFIG.database())
28-
.username(CONFIG.username())
29-
.password(CONFIG.password())
30-
.connectTimeout(CONFIG.connectTimeout())
31-
.readTimeout(CONFIG.readTimeout())
32-
.build();
33-
34-
private InfluxDbClientFactory() {
35-
//empty
13+
private static final InfluxDbClient CLIENT = generateClient();
14+
15+
private static InfluxDbClient generateClient() {
16+
final String version = CONFIG.version();
17+
if (version.startsWith("2.")) {
18+
return new InfluxDbV2Client.Builder()
19+
.host(CONFIG.host())
20+
.port(CONFIG.port())
21+
.orgName(CONFIG.orgName())
22+
.database(CONFIG.database())
23+
.username(CONFIG.username())
24+
.password(CONFIG.password())
25+
.connectTimeout(CONFIG.connectTimeout())
26+
.readTimeout(CONFIG.readTimeout())
27+
.build();
28+
} else {
29+
return new InfluxDbV1Client.Builder()
30+
.host(CONFIG.host())
31+
.port(CONFIG.port())
32+
.database(CONFIG.database())
33+
.username(CONFIG.username())
34+
.password(CONFIG.password())
35+
.connectTimeout(CONFIG.connectTimeout())
36+
.readTimeout(CONFIG.readTimeout())
37+
.build();
38+
}
3639
}
3740

3841
public static InfluxDbClient getClient() {
39-
return CONFIG.version().startsWith("2.") ? getV2Client() : getV1Client();
42+
return CLIENT;
4043
}
4144

42-
public static InfluxDbV1Client getV1Client() {
43-
return V1_CLIENT;
44-
}
45-
46-
public static InfluxDbV2Client getV2Client() {
47-
return V2_CLIENT;
45+
private InfluxDbClientFactory() {
46+
//empty
4847
}
4948
}

0 commit comments

Comments
 (0)