Skip to content

Commit c0b91f0

Browse files
committed
Polish OpenSergoClientManager and rename OpenSergoClientConfig
Signed-off-by: Eric Zhao <sczyh16@gmail.com>
1 parent 0a161d9 commit c0b91f0

File tree

3 files changed

+50
-38
lines changed

3 files changed

+50
-38
lines changed

src/main/java/io/opensergo/OpenSergoClient.java

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
*/
3939
public class OpenSergoClient implements AutoCloseable {
4040

41+
private final OpenSergoClientConfig clientConfig;
42+
4143
private final ManagedChannel channel;
4244
private final OpenSergoUniversalTransportServiceGrpc.OpenSergoUniversalTransportServiceStub transportGrpcStub;
4345

@@ -52,41 +54,51 @@ public static class Builder {
5254

5355
private String host;
5456
private int port;
55-
private OpenSergoConfig openSergoConfig;
57+
private OpenSergoClientConfig openSergoConfig;
5658

5759
public OpenSergoClient.Builder endpoint(String host, int port) {
5860
this.host = host;
5961
this.port = port;
6062
return this;
6163
}
6264

63-
public OpenSergoClient.Builder openSergoConfig(OpenSergoConfig openSergoConfig) {
65+
public OpenSergoClient.Builder openSergoConfig(OpenSergoClientConfig openSergoConfig) {
6466
this.openSergoConfig = openSergoConfig;
6567
return this;
6668
}
6769

6870
public OpenSergoClient build() {
6971
if (this.openSergoConfig == null) {
70-
this.openSergoConfig = new OpenSergoConfig();
72+
this.openSergoConfig = new OpenSergoClientConfig();
7173
}
7274

7375
return new OpenSergoClient(this.host, this.port, this.openSergoConfig);
7476
}
7577

7678
}
7779

78-
private OpenSergoClient(String host, int port, OpenSergoConfig config) {
79-
// TODO: add customized config business for the OpenSergoClient.
80+
public OpenSergoClient(String host, int port) {
81+
// TODO: improve default config logic here.
82+
this(host, port, new OpenSergoClientConfig());
83+
}
84+
85+
public OpenSergoClient(String host, int port, OpenSergoClientConfig clientConfig) {
86+
checkClientConfig(clientConfig);
8087
// TODO: support TLS
88+
this.clientConfig = clientConfig;
8189
this.channel = ManagedChannelBuilder.forAddress(host, port)
82-
.usePlaintext()
83-
.build();
90+
.usePlaintext()
91+
.build();
8492
this.transportGrpcStub = OpenSergoUniversalTransportServiceGrpc.newStub(channel);
8593
this.configCache = new SubscribedConfigCache();
8694
this.subscribeRegistry = new SubscribeRegistry();
8795
this.reqId = new AtomicInteger(0);
8896
}
8997

98+
private void checkClientConfig(OpenSergoClientConfig clientConfig) {
99+
AssertUtils.assertNotNull(clientConfig, "clientConfig cannot be null");
100+
}
101+
90102
public void start() throws Exception {
91103
this.requestAndResponseWriter = transportGrpcStub
92104
.withWaitForReady()

src/main/java/io/opensergo/OpenSergoConfig.java renamed to src/main/java/io/opensergo/OpenSergoClientConfig.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,9 @@
1616
package io.opensergo;
1717

1818
/**
19-
* .
20-
*
2119
* @author Jiangnan Jia
22-
**/
23-
public class OpenSergoConfig {
24-
20+
* @author Eric Zhao
21+
*/
22+
public class OpenSergoClientConfig {
23+
// TODO: add config attribute
2524
}

src/main/java/io/opensergo/OpenSergoClientManager.java

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,26 @@
1616
package io.opensergo;
1717

1818
import java.util.concurrent.ConcurrentHashMap;
19+
import java.util.concurrent.ConcurrentMap;
1920

2021
/**
21-
* .
22-
*
2322
* @author Jiangnan Jia
24-
**/
23+
*/
2524
public class OpenSergoClientManager {
2625

2726
private static volatile OpenSergoClientManager instance;
2827

2928
/**
3029
* Cached all the shared OpenSergoClients.
3130
*/
32-
private ConcurrentHashMap<String, OpenSergoClient> sharedOpenSergoClientCache = new ConcurrentHashMap<>();
33-
31+
private final ConcurrentMap<String, OpenSergoClient> sharedClientCache = new ConcurrentHashMap<>();
3432

35-
private OpenSergoClientManager() {
36-
37-
}
33+
private OpenSergoClientManager() {}
3834

3935
/**
40-
* get OpenSergoClientManager by DCL (Double Check Lock)
41-
* @return
36+
* Get OpenSergoClientManager by DCL (Double Check Lock).
37+
*
38+
* @return the OpenSergoClientManager singleton
4239
*/
4340
public static OpenSergoClientManager get() {
4441
if (instance == null) {
@@ -56,38 +53,42 @@ private String buildSharedCacheKey(String host, int port) {
5653
}
5754

5855
/**
59-
* get the instance from sharedOpenSergoClientCache
60-
* if there is no onewill create a new instance and return it.
56+
* Get the instance from sharedOpenSergoClientCache.
57+
* If there is no one, the manager will create a new client instance and return it.
6158
*
6259
* @param host endpoint of the OpenSergo Control Plane
6360
* @param port port of the OpenSergo Control Plane
6461
* @return OpenSergoClient
6562
*/
6663
public OpenSergoClient getOrCreateClient(String host, int port) {
67-
return this.getOrCreateClient(host, port, new OpenSergoConfig());
64+
return this.getOrCreateClient(host, port, new OpenSergoClientConfig());
6865
}
6966

7067
/**
71-
* get the instance from sharedOpenSergoClientCache with config.
68+
* Get the instance from sharedOpenSergoClientCache with config.
7269
* If instance can be found by host and port, the one will be returned, whether the config is matched or not.
7370
*
74-
* @param host endpoint of the OpenSergo Control Plane
75-
* @param port port of the OpenSergo Control Plane
76-
* @param config OpenSergoConfig
71+
* @param host endpoint of the OpenSergo Control Plane
72+
* @param port port of the OpenSergo Control Plane
73+
* @param config client config
7774
* @return OpenSergoClient
7875
*/
79-
public OpenSergoClient getOrCreateClient(String host, int port, OpenSergoConfig config) {
76+
public OpenSergoClient getOrCreateClient(String host, int port, OpenSergoClientConfig config) {
8077
String sharedOpenSergoClientKey = buildSharedCacheKey(host, port);
81-
OpenSergoClient openSergoClient = sharedOpenSergoClientCache.get(sharedOpenSergoClientKey);
78+
synchronized (this) {
79+
OpenSergoClient openSergoClient = sharedClientCache.get(sharedOpenSergoClientKey);
80+
if (openSergoClient != null) {
81+
return openSergoClient;
82+
}
8283

83-
if (openSergoClient != null) {
84-
return openSergoClient;
84+
if (config == null) {
85+
config = new OpenSergoClientConfig();
86+
}
87+
openSergoClient = new OpenSergoClient.Builder().endpoint(host, port)
88+
.openSergoConfig(config).build();
89+
sharedClientCache.putIfAbsent(sharedOpenSergoClientKey, openSergoClient);
90+
return sharedClientCache.get(sharedOpenSergoClientKey);
8591
}
86-
87-
config = config == null ? new OpenSergoConfig() : config;
88-
openSergoClient = new OpenSergoClient.Builder().endpoint(host, port).openSergoConfig(config).build();
89-
sharedOpenSergoClientCache.putIfAbsent(sharedOpenSergoClientKey, openSergoClient);
90-
return sharedOpenSergoClientCache.get(sharedOpenSergoClientKey);
9192
}
9293

9394
}

0 commit comments

Comments
 (0)