Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop disabling the DNS cache #880

Merged
merged 1 commit into from
Nov 1, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 23 additions & 52 deletions src/main/java/com/stripe/net/HttpURLConnectionClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
import lombok.Cleanup;

public class HttpURLConnectionClient extends HttpClient {
private static final String DNS_CACHE_TTL_PROPERTY_NAME = "networkaddress.cache.ttl";

/*
* Set this property to override your environment's default
* URLStreamHandler; Settings the property should not be needed in most
Expand All @@ -46,26 +44,6 @@ public class HttpURLConnectionClient extends HttpClient {
@Override
public StripeResponse request(StripeRequest request)
throws AuthenticationException, InvalidRequestException, ApiConnectionException {
String originalDnsCacheTtl = null;
Boolean allowedToSetTtl = true;

try {
originalDnsCacheTtl = java.security.Security.getProperty(DNS_CACHE_TTL_PROPERTY_NAME);
// Disable the DNS cache.
//
// Unfortunately the original author of this change didn't leave a
// comment explaining why it was required, but presumably the worry
// was that cache times were being expanded to a point that was
// problematic for proper resolution. Various JVM's have pretty
// good defaults though, so if the user hasn't touched this value,
// don't touch it either.
if (originalDnsCacheTtl != null) {
java.security.Security.setProperty(DNS_CACHE_TTL_PROPERTY_NAME, "0");
}
} catch (SecurityException se) {
allowedToSetTtl = false;
}

String apiKey = request.options().getApiKey();
if (apiKey == null || apiKey.trim().isEmpty()) {
throw new AuthenticationException(
Expand All @@ -78,39 +56,32 @@ public StripeResponse request(StripeRequest request)
0);
}

try {
StripeResponse response;

long requestStartNanos = System.nanoTime();

switch (request.type()) {
case NORMAL:
response =
getStripeResponse(
request.method(), request.url(), request.params(), request.options());
break;
case MULTIPART:
response =
getMultipartStripeResponse(
request.method(), request.url(), request.params(), request.options());
break;
default:
throw new RuntimeException(
"Invalid APIResource request type. "
+ "This indicates a bug in the Stripe bindings. Please contact "
+ "support@stripe.com for assistance.");
}
StripeResponse response;

long requestStartNanos = System.nanoTime();

switch (request.type()) {
case NORMAL:
response =
getStripeResponse(request.method(), request.url(), request.params(), request.options());
break;
case MULTIPART:
response =
getMultipartStripeResponse(
request.method(), request.url(), request.params(), request.options());
break;
default:
throw new RuntimeException(
"Invalid APIResource request type. "
+ "This indicates a bug in the Stripe bindings. Please contact "
+ "support@stripe.com for assistance.");
}

Duration requestDuration = Duration.ofNanos(System.nanoTime() - requestStartNanos);
Duration requestDuration = Duration.ofNanos(System.nanoTime() - requestStartNanos);

requestTelemetry.MaybeEnqueueMetrics(response, requestDuration);
requestTelemetry.MaybeEnqueueMetrics(response, requestDuration);

return response;
} finally {
if (allowedToSetTtl && originalDnsCacheTtl != null) {
java.security.Security.setProperty(DNS_CACHE_TTL_PROPERTY_NAME, originalDnsCacheTtl);
}
}
return response;
}

private static StripeResponse getStripeResponse(
Expand Down