Skip to content
This repository has been archived by the owner on Aug 1, 2023. It is now read-only.

Commit

Permalink
Support multiple URIs for ethnetstats, so one can try wss, then ws
Browse files Browse the repository at this point in the history
  • Loading branch information
atoulme committed Feb 21, 2020
1 parent 0773ca2 commit eb9fe99
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,15 @@ 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;


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);
Expand All @@ -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
Expand All @@ -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,
Expand All @@ -112,7 +114,7 @@ public 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;
Expand Down Expand Up @@ -150,25 +152,30 @@ public void sendHistoryResponse(List<BlockStats> blocks) {
}

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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void testConnectToLocalEthStats(@VertxInstance Vertx vertx) throws InterruptedEx
EthStatsReporter reporter = new EthStatsReporter(
vertx,
logger,
URI.create("ws://localhost:3000/api"),
Collections.singletonList(URI.create("ws://localhost:3000/api")),
"wat",
"name",
"node",
Expand Down

0 comments on commit eb9fe99

Please sign in to comment.