From ae1118a06f792b141acd98e9a59df19d557502e3 Mon Sep 17 00:00:00 2001 From: Russ Allbery Date: Mon, 28 Jun 2021 16:18:35 -0700 Subject: [PATCH] Add retry loop for Gafaelfawr request We're seeing GOAWAY IOException errors from the HTTP request to Gafaelfawr that correspond to a 499 error on the NGINX side. See if a simple retry loop around the request will fix this problem. Try five times before throwing the exception. --- .../opencadc/tap/impl/AuthenticatorImpl.java | 42 +++++++++++++------ 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/src/main/java/org/opencadc/tap/impl/AuthenticatorImpl.java b/src/main/java/org/opencadc/tap/impl/AuthenticatorImpl.java index bda72b8..e04b3d4 100644 --- a/src/main/java/org/opencadc/tap/impl/AuthenticatorImpl.java +++ b/src/main/java/org/opencadc/tap/impl/AuthenticatorImpl.java @@ -58,18 +58,23 @@ public Subject augment(Subject subject) if (principal instanceof BearerTokenPrincipal) { BearerTokenPrincipal tp = (BearerTokenPrincipal) principal; - HttpRequest request = HttpRequest.newBuilder( - URI.create(gafaelfawr_url)) - .header("Accept", "application/json") - .header("Authorization", "bearer " + tp.getName()) - .build(); - try { - HttpResponse response = client.send(request, BodyHandlers.ofString()); - String body = response.body(); - - Gson gson = new Gson(); - JsonObject authData = gson.fromJson(body, JsonObject.class); + JsonObject authData = null; + boolean success = false; + for (int i = 1; i < 5; i++) { + try { + authData = getTokenInfo(tp.getName()); + success = true; + } catch (IOException e) { + log.warn(e); + log.warn("IOException while getting info Gafaelfawr, retrying"); + } + } + + if (!success) { + // Try one more time to throw an accurate exception. + authData = getTokenInfo(tp.getName()); + } String username = authData.getAsJsonPrimitive("username").getAsString(); int uid = authData.getAsJsonPrimitive("uid").getAsInt(); @@ -87,7 +92,7 @@ public Subject augment(Subject subject) log.warn("InterruptedException thrown while getting info from Gafaelfawr"); log.warn(e); } catch (IOException e) { - log.warn("IOException while getting info from Gafaelfawr"); + log.warn("IOException while getting info from Gafaelfawr, failing"); log.warn(e); } } @@ -106,4 +111,17 @@ public Subject augment(Subject subject) public Subject validate(Subject subject) throws AccessControlException { return subject; } + + private JsonObject getTokenInfo(String token) throws IOException, InterruptedException { + HttpRequest request = HttpRequest.newBuilder(URI.create(gafaelfawr_url)) + .header("Accept", "application/json") + .header("Authorization", "bearer " + token) + .build(); + + HttpResponse response = client.send(request, BodyHandlers.ofString()); + String body = response.body(); + + Gson gson = new Gson(); + return gson.fromJson(body, JsonObject.class); + } }