|
| 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 | +} |
0 commit comments