Skip to content

Commit

Permalink
Client respects DNS TTL.
Browse files Browse the repository at this point in the history
  • Loading branch information
chickenchickenlove committed Oct 27, 2024
1 parent c5b3f19 commit 417bf3b
Show file tree
Hide file tree
Showing 5 changed files with 336 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

import java.net.InetSocketAddress;
import java.util.Objects;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.function.BiConsumer;

import com.google.common.base.MoreObjects;

Expand All @@ -29,7 +32,11 @@
*/
public final class ConnectProxyConfig extends ProxyConfig {

private final InetSocketAddress proxyAddress;
private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();

private long lastUpdateTime = System.currentTimeMillis();

private InetSocketAddress proxyAddress;

@Nullable
private final String username;
Expand All @@ -43,11 +50,30 @@ public final class ConnectProxyConfig extends ProxyConfig {

ConnectProxyConfig(InetSocketAddress proxyAddress, @Nullable String username,
@Nullable String password, HttpHeaders headers, boolean useTls) {
this(proxyAddress, username, password, headers, useTls, -1);
}

ConnectProxyConfig(InetSocketAddress proxyAddress, @Nullable String username,
@Nullable String password, HttpHeaders headers, boolean useTls,
long refreshInterval) {
this.proxyAddress = proxyAddress;
this.username = username;
this.password = password;
this.headers = headers;
this.useTls = useTls;

if (refreshInterval > 0) {
final BiConsumer<InetSocketAddress, Long> callback = (newProxyAddress, updateTime) -> {
this.proxyAddress = newProxyAddress;
this.lastUpdateTime = updateTime;
};

ProxyConfig.reserveDNSUpdate(callback,
proxyAddress.getHostName(),
proxyAddress.getPort(),
refreshInterval,
scheduler);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@

import java.net.InetSocketAddress;
import java.util.Objects;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.function.BiConsumer;

import com.google.common.base.MoreObjects;

Expand All @@ -31,21 +34,72 @@
*/
public final class HAProxyConfig extends ProxyConfig {

private final InetSocketAddress proxyAddress;
private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();

private long lastUpdateTime = System.currentTimeMillis();

private InetSocketAddress proxyAddress;

@Nullable
private final InetSocketAddress sourceAddress;
private InetSocketAddress sourceAddress;

HAProxyConfig(InetSocketAddress proxyAddress) {
this(proxyAddress, -1);
}

HAProxyConfig(InetSocketAddress proxyAddress, long refreshInterval) {
this.proxyAddress = proxyAddress;
sourceAddress = null;

if (refreshInterval > 0) {
final BiConsumer<InetSocketAddress, Long> callback = (newProxyAddress, updateTime) -> {
this.proxyAddress = newProxyAddress;
this.lastUpdateTime = updateTime;
};

ProxyConfig.reserveDNSUpdate(callback,
proxyAddress.getHostName(),
proxyAddress.getPort(),
refreshInterval,
scheduler);
}
}

HAProxyConfig(InetSocketAddress proxyAddress, InetSocketAddress sourceAddress) {
this(proxyAddress, sourceAddress, -1);
}

HAProxyConfig(InetSocketAddress proxyAddress, InetSocketAddress sourceAddress, long refreshInterval) {
checkArgument(sourceAddress.getAddress().getClass() == proxyAddress.getAddress().getClass(),
"sourceAddress and proxyAddress should be the same type");
this.proxyAddress = proxyAddress;
this.sourceAddress = sourceAddress;

if (refreshInterval > 0) {
final BiConsumer<InetSocketAddress, Long> callback = (newProxyAddress, updateTime) -> {
this.proxyAddress = newProxyAddress;
this.lastUpdateTime = updateTime;
};

ProxyConfig.reserveDNSUpdate(callback,
proxyAddress.getHostName(),
proxyAddress.getPort(),
refreshInterval,
scheduler);
}

if (refreshInterval > 0) {
final BiConsumer<InetSocketAddress, Long> callback = (newSourceAddress, updateTime) -> {
this.sourceAddress = newSourceAddress;
this.lastUpdateTime = updateTime;
};

ProxyConfig.reserveDNSUpdate(callback,
sourceAddress.getHostName(),
sourceAddress.getPort(),
refreshInterval,
scheduler);
}
}

@Override
Expand Down
Loading

0 comments on commit 417bf3b

Please sign in to comment.