Ignore:
Timestamp:
2020-03-10T16:43:52+01:00 (5 years ago)
Author:
simon04
Message:

fix #josm18905 - Too many open files when panning

File:
1 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/http2/src/org/openstreetmap/josm/plugins/http2/Http2Client.java

    r35062 r35356  
    2222import java.util.Map;
    2323import java.util.Objects;
     24import java.util.concurrent.ConcurrentHashMap;
    2425
    2526import org.openstreetmap.josm.data.Version;
     
    3435public final class Http2Client extends org.openstreetmap.josm.tools.HttpClient {
    3536
    36     private HttpClient.Builder clientBuilder;
    37     private HttpRequest.Builder requestBuilder;
     37    private static final Map<Duration, HttpClient> clientForConnectTimeout = new ConcurrentHashMap<>();
     38    private HttpRequest request;
    3839    private HttpResponse<InputStream> response;
    3940
     
    4445    @Override
    4546    protected void setupConnection(ProgressMonitor progressMonitor) throws IOException {
    46         clientBuilder = HttpClient.newBuilder().followRedirects(Redirect.NEVER); // we do that ourselves
    47         int timeout = getConnectTimeout();
    48         if (timeout > 0) {
    49             clientBuilder.connectTimeout(Duration.ofMillis(timeout));
    50         }
     47        HttpRequest.Builder requestBuilder;
    5148        try {
    5249            requestBuilder = HttpRequest.newBuilder()
     
    5956            throw new IOException(e);
    6057        }
    61         timeout = getReadTimeout();
     58        int timeout = getReadTimeout();
    6259        if (timeout > 0) {
    6360            requestBuilder.timeout(Duration.ofMillis(timeout));
     
    7976            }
    8077        }
     78        request = requestBuilder.build();
    8179
    8280        notifyConnect(progressMonitor);
     
    8987    @Override
    9088    protected ConnectionResponse performConnection() throws IOException {
     89        // reuse HttpClient
     90        HttpClient client = clientForConnectTimeout.computeIfAbsent(Duration.ofMillis(getConnectTimeout()), timeout ->
     91                HttpClient.newBuilder()
     92                        .followRedirects(Redirect.NEVER) // we do that ourselves
     93                        .connectTimeout(timeout)
     94                        .build());
    9195        try {
    92             response = clientBuilder.build().send(requestBuilder.build(), BodyHandlers.ofInputStream());
     96            response = client.send(request, BodyHandlers.ofInputStream());
    9397            return new ConnectionResponse() {
    9498                @Override
Note: See TracChangeset for help on using the changeset viewer.