You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuweni.apache.org by to...@apache.org on 2020/02/21 00:26:55 UTC

[incubator-tuweni] branch master updated: Support multiple URIs for ethnetstats, so one can try wss, then ws

This is an automated email from the ASF dual-hosted git repository.

toulmean pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-tuweni.git


The following commit(s) were added to refs/heads/master by this push:
     new eb9fe99  Support multiple URIs for ethnetstats, so one can try wss, then ws
eb9fe99 is described below

commit eb9fe9900a9be21e8d19a6b8347a7880bf91f040
Author: Antoine Toulme <an...@lunar-ocean.com>
AuthorDate: Thu Feb 20 16:26:39 2020 -0800

    Support multiple URIs for ethnetstats, so one can try wss, then ws
---
 .../apache/tuweni/ethstats/EthStatsReporter.java   | 39 +++++++++++++---------
 .../tuweni/ethstats/EthStatsReporterTest.java      |  2 +-
 2 files changed, 24 insertions(+), 17 deletions(-)

diff --git a/ethstats/src/main/java/org/apache/tuweni/ethstats/EthStatsReporter.java b/ethstats/src/main/java/org/apache/tuweni/ethstats/EthStatsReporter.java
index e27c82b..b811a26 100644
--- a/ethstats/src/main/java/org/apache/tuweni/ethstats/EthStatsReporter.java
+++ b/ethstats/src/main/java/org/apache/tuweni/ethstats/EthStatsReporter.java
@@ -58,6 +58,7 @@ public final class EthStatsReporter {
   static {
     mapper.registerModule(new EthJsonModule());
   }
+
   private final static long DELAY = 5000;
   private final static long REPORTING_PERIOD = 1000;
   private final static long PING_PERIOD = 15000;
@@ -65,7 +66,7 @@ public final class EthStatsReporter {
 
   private final String id;
   private final Vertx vertx;
-  private final URI ethstatsServerURI;
+  private final List<URI> ethstatsServerURIs;
   private final Logger logger;
   private final AtomicBoolean started = new AtomicBoolean(false);
   private final AtomicBoolean waitingOnPong = new AtomicBoolean(false);
@@ -82,10 +83,11 @@ public final class EthStatsReporter {
 
   /**
    * Default constructor.
-   * 
+   *
    * @param vertx a Vert.x instance, externally managed.
    * @param logger a logger
-   * @param ethstatsServerURI the URI to connect to eth-netstats, such as ws://www.ethnetstats.org:3000/api
+   * @param ethstatsServerURIs the URIs to connect to eth-netstats, such as ws://www.ethnetstats.org:3000/api. URIs are
+   *        tried in sequence, and the first one to work is used.
    * @param secret the secret to use when we connect to eth-netstats
    * @param name the name of the node to be reported in the UI
    * @param node the node name to be reported in the UI
@@ -99,7 +101,7 @@ public final class EthStatsReporter {
   public EthStatsReporter(
       Vertx vertx,
       Logger logger,
-      URI ethstatsServerURI,
+      List<URI> ethstatsServerURIs,
       String secret,
       String name,
       String node,
@@ -112,7 +114,7 @@ public final class EthStatsReporter {
     this.id = UUID.randomUUID().toString();
     this.vertx = vertx;
     this.logger = logger;
-    this.ethstatsServerURI = ethstatsServerURI;
+    this.ethstatsServerURIs = ethstatsServerURIs;
     this.secret = secret;
     this.nodeInfo = new NodeInfo(name, node, port, network, protocol, os, osVer);
     this.historyRequester = historyRequester;
@@ -150,25 +152,30 @@ public final class EthStatsReporter {
   }
 
   private void startInternal() {
-    executor.executeBlocking(this::connect, result -> {
-      if (started.get()) {
-        if ((result.failed() || !result.result())) {
-          logger.debug("Attempting to connect", result.cause());
-          attemptConnect(null);
-        }
+    AtomicBoolean connectedOK = new AtomicBoolean(false);
+    for (URI uri : ethstatsServerURIs) {
+      executor.executeBlocking((Future<Boolean> handler) -> connect(handler, uri), result -> {
+        logger.debug("Attempting to connect", result.cause());
+        connectedOK.set(!result.failed() && result.result());
+      });
+      if (connectedOK.get()) {
+        break;
       }
-    });
+    }
+    if (!connectedOK.get() && started.get()) {
+      attemptConnect(null);
+    }
   }
 
   private void attemptConnect(Void aVoid) {
     vertx.setTimer(DELAY, handler -> this.startInternal());
   }
 
-  private void connect(Future<Boolean> result) {
+  private void connect(Future<Boolean> result, URI uri) {
     client.websocket(
-        ethstatsServerURI.getPort(),
-        ethstatsServerURI.getHost(),
-        ethstatsServerURI.toString(),
+        uri.getPort(),
+        uri.getHost(),
+        uri.toString(),
         MultiMap.caseInsensitiveMultiMap().add("origin", "http://localhost"),
         ws -> {
           ws.closeHandler(this::attemptConnect);
diff --git a/ethstats/src/test/java/org/apache/tuweni/ethstats/EthStatsReporterTest.java b/ethstats/src/test/java/org/apache/tuweni/ethstats/EthStatsReporterTest.java
index 63b8895..936aed9 100644
--- a/ethstats/src/test/java/org/apache/tuweni/ethstats/EthStatsReporterTest.java
+++ b/ethstats/src/test/java/org/apache/tuweni/ethstats/EthStatsReporterTest.java
@@ -41,7 +41,7 @@ public class EthStatsReporterTest {
     EthStatsReporter reporter = new EthStatsReporter(
         vertx,
         logger,
-        URI.create("ws://localhost:3000/api"),
+        Collections.singletonList(URI.create("ws://localhost:3000/api")),
         "wat",
         "name",
         "node",


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@tuweni.apache.org
For additional commands, e-mail: commits-help@tuweni.apache.org