|
44 | 44 | import java.net.http.HttpClient; |
45 | 45 | import java.net.http.HttpRequest; |
46 | 46 | import java.net.http.HttpResponse; |
| 47 | +import java.net.http.HttpResponse.BodyHandlers; |
47 | 48 | import java.time.Duration; |
48 | 49 | import java.util.Date; |
49 | 50 | import java.util.function.BiFunction; |
50 | | -import java.util.function.Supplier; |
51 | 51 |
|
52 | 52 | public class AccessTokenProvider { |
53 | 53 |
|
54 | 54 | private static final Logger log = LoggerFactory.getLogger(AccessTokenProvider.class); |
55 | 55 |
|
56 | | - private final Supplier<HttpClient> httpClientSupplier; |
| 56 | + private final HttpClient httpClient; |
57 | 57 | private final Duration httpTimeout; |
58 | 58 | private final ObjectMapper objectMapper; |
59 | 59 |
|
60 | 60 | public AccessTokenProvider(GitHubAppInstallationConfig cfg, ObjectMapper objectMapper) { |
61 | 61 | this.httpTimeout = cfg.getHttpClientTimeout(); |
62 | 62 | this.objectMapper = objectMapper; |
63 | | - // TODO: use thread pool for http client. JDK17 HttpClient creates a new thread per client even for synchronous execution |
64 | | - this.httpClientSupplier = () -> HttpClient.newBuilder() // would be nice to re-use client thread-safely |
| 63 | + this.httpClient = HttpClient.newBuilder() |
65 | 64 | .connectTimeout(httpTimeout) |
66 | 65 | .build(); |
67 | 66 | } |
68 | 67 |
|
69 | 68 | public AccessTokenProvider(GitHubAppInstallationConfig cfg, |
70 | 69 | ObjectMapper objectMapper, |
71 | | - Supplier<HttpClient> httpClientSupplier) { |
| 70 | + HttpClient httpClient) { |
72 | 71 | this.httpTimeout = cfg.getHttpClientTimeout(); |
73 | 72 | this.objectMapper = objectMapper; |
74 | | - this.httpClientSupplier = httpClientSupplier; |
| 73 | + this.httpClient = httpClient; |
75 | 74 | } |
76 | 75 |
|
77 | 76 | ExternalAuthToken getRepoInstallationToken(GitHubAppAuthConfig app, String orgRepo) throws GitHubAppException { |
@@ -162,8 +161,7 @@ private static String generateJWT(GitHubAppAuthConfig auth) throws JOSEException |
162 | 161 |
|
163 | 162 | private <T> T sendRequest(HttpRequest httpRequest, int expectedCode, Class<T> clazz, BiFunction<Integer, String, GitHubAppException> exFun) throws GitHubAppException { |
164 | 163 | try { |
165 | | - var resp = httpClientSupplier.get() |
166 | | - .send(httpRequest, HttpResponse.BodyHandlers.ofInputStream()); |
| 164 | + var resp = httpClient.send(httpRequest, BodyHandlers.ofInputStream()); |
167 | 165 | if (resp.statusCode() != expectedCode) { |
168 | 166 | throw exFun.apply(resp.statusCode(), readBody(resp)); |
169 | 167 | } |
|
0 commit comments