Skip to content

Commit 0a161d9

Browse files
authored
Add OpenSergoClientManager to support client reuse (#24)
Signed-off-by: Jiangnan Jia <jnan0806@gmail.com>
1 parent ba29db1 commit 0a161d9

File tree

3 files changed

+149
-4
lines changed

3 files changed

+149
-4
lines changed

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

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,39 @@ public class OpenSergoClient implements AutoCloseable {
4848

4949
private AtomicInteger reqId;
5050

51-
public OpenSergoClient(String host, int port) {
52-
// TODO: support customized config for the OpenSergoClient.
51+
public static class Builder {
52+
53+
private String host;
54+
private int port;
55+
private OpenSergoConfig openSergoConfig;
56+
57+
public OpenSergoClient.Builder endpoint(String host, int port) {
58+
this.host = host;
59+
this.port = port;
60+
return this;
61+
}
62+
63+
public OpenSergoClient.Builder openSergoConfig(OpenSergoConfig openSergoConfig) {
64+
this.openSergoConfig = openSergoConfig;
65+
return this;
66+
}
67+
68+
public OpenSergoClient build() {
69+
if (this.openSergoConfig == null) {
70+
this.openSergoConfig = new OpenSergoConfig();
71+
}
72+
73+
return new OpenSergoClient(this.host, this.port, this.openSergoConfig);
74+
}
75+
76+
}
77+
78+
private OpenSergoClient(String host, int port, OpenSergoConfig config) {
79+
// TODO: add customized config business for the OpenSergoClient.
5380
// TODO: support TLS
5481
this.channel = ManagedChannelBuilder.forAddress(host, port)
55-
.usePlaintext()
56-
.build();
82+
.usePlaintext()
83+
.build();
5784
this.transportGrpcStub = OpenSergoUniversalTransportServiceGrpc.newStub(channel);
5885
this.configCache = new SubscribedConfigCache();
5986
this.subscribeRegistry = new SubscribeRegistry();
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Copyright 2022, OpenSergo Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.opensergo;
17+
18+
import java.util.concurrent.ConcurrentHashMap;
19+
20+
/**
21+
* .
22+
*
23+
* @author Jiangnan Jia
24+
**/
25+
public class OpenSergoClientManager {
26+
27+
private static volatile OpenSergoClientManager instance;
28+
29+
/**
30+
* Cached all the shared OpenSergoClients.
31+
*/
32+
private ConcurrentHashMap<String, OpenSergoClient> sharedOpenSergoClientCache = new ConcurrentHashMap<>();
33+
34+
35+
private OpenSergoClientManager() {
36+
37+
}
38+
39+
/**
40+
* get OpenSergoClientManager by DCL (Double Check Lock)
41+
* @return
42+
*/
43+
public static OpenSergoClientManager get() {
44+
if (instance == null) {
45+
synchronized (OpenSergoClientManager.class) {
46+
if (instance == null) {
47+
return new OpenSergoClientManager();
48+
}
49+
}
50+
}
51+
return instance;
52+
}
53+
54+
private String buildSharedCacheKey(String host, int port) {
55+
return host + ":" + port;
56+
}
57+
58+
/**
59+
* get the instance from sharedOpenSergoClientCache,
60+
* if there is no one, will create a new instance and return it.
61+
*
62+
* @param host endpoint of the OpenSergo Control Plane
63+
* @param port port of the OpenSergo Control Plane
64+
* @return OpenSergoClient
65+
*/
66+
public OpenSergoClient getOrCreateClient(String host, int port) {
67+
return this.getOrCreateClient(host, port, new OpenSergoConfig());
68+
}
69+
70+
/**
71+
* get the instance from sharedOpenSergoClientCache with config.
72+
* If instance can be found by host and port, the one will be returned, whether the config is matched or not.
73+
*
74+
* @param host endpoint of the OpenSergo Control Plane
75+
* @param port port of the OpenSergo Control Plane
76+
* @param config OpenSergoConfig
77+
* @return OpenSergoClient
78+
*/
79+
public OpenSergoClient getOrCreateClient(String host, int port, OpenSergoConfig config) {
80+
String sharedOpenSergoClientKey = buildSharedCacheKey(host, port);
81+
OpenSergoClient openSergoClient = sharedOpenSergoClientCache.get(sharedOpenSergoClientKey);
82+
83+
if (openSergoClient != null) {
84+
return openSergoClient;
85+
}
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);
91+
}
92+
93+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright 2022, OpenSergo Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.opensergo;
17+
18+
/**
19+
* .
20+
*
21+
* @author Jiangnan Jia
22+
**/
23+
public class OpenSergoConfig {
24+
25+
}

0 commit comments

Comments
 (0)