HttpSession: Use Provider<PeerInfo> instead of just PeerInfo

When retrying an HTTP request, the same instance of the HttpSession will
be (re)used. By using Provider.get() we make sure to use the most recent
PeerInfo for the next retry. This adds support for a scenario where the
other peer node starts on a different host after being restarted.

Change-Id: Ib4e72612e5b1ff3a991c74d136f15dfcc64496a6
diff --git a/src/main/java/com/ericsson/gerrit/plugins/highavailability/forwarder/rest/HttpSession.java b/src/main/java/com/ericsson/gerrit/plugins/highavailability/forwarder/rest/HttpSession.java
index 38d751b..7a95f44 100644
--- a/src/main/java/com/ericsson/gerrit/plugins/highavailability/forwarder/rest/HttpSession.java
+++ b/src/main/java/com/ericsson/gerrit/plugins/highavailability/forwarder/rest/HttpSession.java
@@ -17,6 +17,7 @@
 import com.google.common.base.Strings;
 import com.google.common.net.MediaType;
 import com.google.inject.Inject;
+import com.google.inject.Provider;
 
 import com.ericsson.gerrit.plugins.highavailability.forwarder.rest.HttpResponseHandler.HttpResult;
 import com.ericsson.gerrit.plugins.highavailability.peers.PeerInfo;
@@ -31,11 +32,11 @@
 
 class HttpSession {
   private final CloseableHttpClient httpClient;
-  private PeerInfo peerInfo;
+  private final Provider<PeerInfo> peerInfo;
 
   @Inject
   HttpSession(CloseableHttpClient httpClient,
-      PeerInfo peerInfo) {
+      Provider<PeerInfo> peerInfo) {
     this.httpClient = httpClient;
     this.peerInfo = peerInfo;
   }
@@ -45,7 +46,7 @@
   }
 
   HttpResult post(String endpoint, String content) throws IOException {
-    HttpPost post = new HttpPost(peerInfo.getDirectUrl() + endpoint);
+    HttpPost post = new HttpPost(peerInfo.get().getDirectUrl() + endpoint);
     if (!Strings.isNullOrEmpty(content)) {
       post.addHeader("Content-Type", MediaType.JSON_UTF_8.toString());
       post.setEntity(new StringEntity(content, StandardCharsets.UTF_8));
@@ -54,7 +55,8 @@
   }
 
   HttpResult delete(String endpoint) throws IOException {
-    return httpClient.execute(new HttpDelete(peerInfo.getDirectUrl() + endpoint),
+    return httpClient.execute(
+        new HttpDelete(peerInfo.get().getDirectUrl() + endpoint),
         new HttpResponseHandler());
   }
 }
diff --git a/src/test/java/com/ericsson/gerrit/plugins/highavailability/forwarder/rest/HttpSessionTest.java b/src/test/java/com/ericsson/gerrit/plugins/highavailability/forwarder/rest/HttpSessionTest.java
index 4280ac5..ca59f8d 100644
--- a/src/test/java/com/ericsson/gerrit/plugins/highavailability/forwarder/rest/HttpSessionTest.java
+++ b/src/test/java/com/ericsson/gerrit/plugins/highavailability/forwarder/rest/HttpSessionTest.java
@@ -29,6 +29,7 @@
 import com.github.tomakehurst.wiremock.http.Fault;
 import com.github.tomakehurst.wiremock.junit.WireMockRule;
 import com.github.tomakehurst.wiremock.stubbing.Scenario;
+import com.google.inject.util.Providers;
 
 import org.junit.Before;
 import org.junit.ClassRule;
@@ -71,8 +72,9 @@
 
     PeerInfo peerInfo = mock(PeerInfo.class);
     when(peerInfo.getDirectUrl()).thenReturn(url);
-    httpSession =
-        new HttpSession(new HttpClientProvider(cfg).get(), peerInfo);
+    httpSession = new HttpSession(
+        new HttpClientProvider(cfg).get(),
+        Providers.of(peerInfo));
     wireMockRule.resetRequests();
   }