diff options
author | Harald Musum <musum@verizonmedia.com> | 2021-09-21 23:43:03 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-21 23:43:03 +0200 |
commit | 16c799289d15eb30f55675373b254210396fad17 (patch) | |
tree | e34d5c46983b805142f8a8f6fe066364d95c294b | |
parent | ad456cc31f6f7934fe9e7e2a28ca901b1f1fc401 (diff) | |
parent | 62c76fb625ede6326c3e14c491218a114a551b92 (diff) |
Merge pull request #19229 from vespa-engine/balder/cache-connection-target
Cache the connection target and reuse to avoid ssl renegotiation.
-rw-r--r-- | metrics-proxy/src/main/java/ai/vespa/metricsproxy/service/ConfigSentinelClient.java | 44 |
1 files changed, 25 insertions, 19 deletions
diff --git a/metrics-proxy/src/main/java/ai/vespa/metricsproxy/service/ConfigSentinelClient.java b/metrics-proxy/src/main/java/ai/vespa/metricsproxy/service/ConfigSentinelClient.java index d07a52f42bd..3d834106ebc 100644 --- a/metrics-proxy/src/main/java/ai/vespa/metricsproxy/service/ConfigSentinelClient.java +++ b/metrics-proxy/src/main/java/ai/vespa/metricsproxy/service/ConfigSentinelClient.java @@ -24,7 +24,9 @@ import java.util.logging.Logger; public class ConfigSentinelClient extends AbstractComponent { private final static Logger log = Logger.getLogger(ConfigSentinelClient.class.getName()); + private static final Spec SPEC = new Spec("localhost", 19097); private final Supervisor supervisor; + private Target connection = null; @Inject public ConfigSentinelClient() { @@ -33,6 +35,12 @@ public class ConfigSentinelClient extends AbstractComponent { @Override public void deconstruct() { + synchronized (this) { + if (connection != null) { + connection.close(); + connection = null; + } + } supervisor.transport().shutdown().join(); super.deconstruct(); } @@ -126,7 +134,7 @@ public class ConfigSentinelClient extends AbstractComponent { } for (int i = 1; i < parts.length; i++) { - String keyValue[] = parts[i].split("="); + String [] keyValue = parts[i].split("="); String key = keyValue[0]; String value = keyValue[1]; @@ -155,26 +163,24 @@ public class ConfigSentinelClient extends AbstractComponent { String sentinelLs() { String servicelist = ""; - int rpcPort = 19097; - Spec spec = new Spec("localhost", rpcPort); - Target connection = supervisor.connect(spec); - try { - if (connection.isValid()) { - Request req = new Request("sentinel.ls"); - connection.invokeSync(req, 5.0); - if (req.errorCode() == ErrorCode.NONE && - req.checkReturnTypes("s")) - { - servicelist = req.returnValues().get(0).asString(); - } else { - log.log(Level.WARNING, "Bad answer to RPC request: " + req.errorMessage()); - } + synchronized (this) { + if (connection == null || ! connection.isValid()) { + connection = supervisor.connect(SPEC); + } + } + if (connection.isValid()) { + Request req = new Request("sentinel.ls"); + connection.invokeSync(req, 5.0); + if (req.errorCode() == ErrorCode.NONE && + req.checkReturnTypes("s")) + { + servicelist = req.returnValues().get(0).asString(); } else { - log.log(Level.WARNING, "Could not connect to sentinel at: "+spec); + log.log(Level.WARNING, "Bad answer to RPC request: " + req.errorMessage()); } - return servicelist; - } finally { - connection.close(); + } else { + log.log(Level.WARNING, "Could not connect to sentinel at: " + SPEC); } + return servicelist; } } |