io.hyperfoil.http.connection.HttpDestinationTableImpl Maven / Gradle / Ivy
package io.hyperfoil.http.connection;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Stream;
import io.hyperfoil.api.session.Session;
import io.hyperfoil.http.api.HttpConnectionPool;
import io.hyperfoil.http.api.HttpDestinationTable;
public class HttpDestinationTableImpl implements HttpDestinationTable {
private final Map pools;
private final String[] authorities;
private final byte[][] authorityBytes;
public HttpDestinationTableImpl(Map pools) {
this.pools = pools;
this.authorities = pools.keySet().stream().filter(Objects::nonNull).toArray(String[]::new);
this.authorityBytes = Stream.of(authorities).map(url -> url.getBytes(StandardCharsets.UTF_8)).toArray(byte[][]::new);
}
public HttpDestinationTableImpl(HttpDestinationTable other, Function replacePool) {
this.authorities = other.authorities();
this.authorityBytes = other.authorityBytes();
this.pools = new HashMap<>();
HttpConnectionPool defaultPool = other.getConnectionPool(null);
for (String authority : authorities) {
HttpConnectionPool pool = other.getConnectionPool(authority);
HttpConnectionPool newPool = replacePool.apply(pool);
pools.put(authority, newPool);
if (pool == defaultPool) {
pools.put(null, newPool);
}
}
}
@Override
public String[] authorities() {
return authorities;
}
@Override
public byte[][] authorityBytes() {
return authorityBytes;
}
@Override
public void onSessionReset(Session session) {
pools.values().forEach(HttpConnectionPool::onSessionReset);
}
@Override
public boolean hasSingleDestination() {
return authorities.length == 1;
}
@Override
public HttpConnectionPool getConnectionPool(String authority) {
return pools.get(authority);
}
public Iterable> iterable() {
return pools.entrySet();
}
}