-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Adding Method to Retryable Exception for evaluation #744
Changes from 1 commit
806149c
5993c5e
e918aca
bf3df0f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,18 +13,22 @@ | |
*/ | ||
package feign; | ||
|
||
import static feign.Util.checkNotNull; | ||
import static feign.Util.valuesOrEmpty; | ||
import java.net.HttpURLConnection; | ||
import java.nio.charset.Charset; | ||
import java.util.Collection; | ||
import java.util.Map; | ||
import static feign.Util.checkNotNull; | ||
import static feign.Util.valuesOrEmpty; | ||
|
||
/** | ||
* An immutable request to an http server. | ||
*/ | ||
public final class Request { | ||
|
||
public enum Methods { | ||
GET, HEAD, POST, PUT, DELETE, CONNECT, OPTIONS, TRACE, PATCH | ||
} | ||
|
||
/** | ||
* No parameters can be null except {@code body} and {@code charset}. All parameters must be | ||
* effectively immutable, via safe copies, not mutating or otherwise. | ||
|
@@ -37,15 +41,15 @@ public static Request create(String method, | |
return new Request(method, url, headers, body, charset); | ||
} | ||
|
||
private final String method; | ||
private final Methods method; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice... maybe just call it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had not considered that, good catch! I'll rename to |
||
private final String url; | ||
private final Map<String, Collection<String>> headers; | ||
private final byte[] body; | ||
private final Charset charset; | ||
|
||
Request(String method, String url, Map<String, Collection<String>> headers, byte[] body, | ||
Charset charset) { | ||
this.method = checkNotNull(method, "method of %s", url); | ||
this.method = Methods.valueOf(checkNotNull(method, "method of %s", url)); | ||
this.url = checkNotNull(url, "url"); | ||
this.headers = checkNotNull(headers, "headers of %s %s", method, url); | ||
this.body = body; // nullable | ||
|
@@ -54,7 +58,7 @@ public static Request create(String method, | |
|
||
/* Method to invoke on the server. */ | ||
public String method() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thin we should create a new method There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, let's do that. I had not added the Enum to |
||
return method; | ||
return method.name(); | ||
} | ||
|
||
/* Fully resolved URL including query. */ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,11 +45,12 @@ public final class Response implements Closeable { | |
|
||
private Response(Builder builder) { | ||
checkState(builder.status >= 200, "Invalid status code: %s", builder.status); | ||
checkState(builder.request != null, "original request is required"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wanted an |
||
this.status = builder.status; | ||
this.request = builder.request; | ||
this.reason = builder.reason; // nullable | ||
this.headers = Collections.unmodifiableMap(caseInsensitiveCopyOf(builder.headers)); | ||
this.body = builder.body; // nullable | ||
this.request = builder.request; // nullable | ||
} | ||
|
||
public Builder toBuilder() { | ||
|
@@ -121,12 +122,13 @@ public Builder body(String text, Charset charset) { | |
|
||
/** | ||
* @see Response#request | ||
* | ||
* NOTE: will add null check in version 10 which may require changes to custom feign.Client | ||
* or loggers | ||
*/ | ||
public Builder request(Request request) { | ||
this.request = request; | ||
checkNotNull(request, "the original request is required on all responses"); | ||
|
||
/* don't keep the body, we don't want to tie up memory on large requests */ | ||
this.request = Request.create( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMO, If someone decides to keep the Response in memory for some reason, I think, they would like to have the whole Request. Lemme know your thoughts There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My concern is with larger requests that deal with multi-part uploads and pure binary requests. Keeping the body in memory after the request feels like it may lead to unexpected memory usages and leaks. I had not considered the GC impact. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It feels like an early optimization for me. What if we include the original There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Understood. I'll change to to reference the original and not create a new request. |
||
request.method(), request.url(), request.headers(), null, request.charset()); | ||
return this; | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,20 +24,23 @@ public class RetryableException extends FeignException { | |
private static final long serialVersionUID = 1L; | ||
|
||
private final Long retryAfter; | ||
private final String method; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about using the enumeration |
||
|
||
/** | ||
* @param retryAfter usually corresponds to the {@link feign.Util#RETRY_AFTER} header. | ||
*/ | ||
public RetryableException(String message, Throwable cause, Date retryAfter) { | ||
public RetryableException(String message, String method, Throwable cause, Date retryAfter) { | ||
super(message, cause); | ||
this.method = method; | ||
this.retryAfter = retryAfter != null ? retryAfter.getTime() : null; | ||
} | ||
|
||
/** | ||
* @param retryAfter usually corresponds to the {@link feign.Util#RETRY_AFTER} header. | ||
*/ | ||
public RetryableException(String message, Date retryAfter) { | ||
public RetryableException(String message, String method, Date retryAfter) { | ||
super(message); | ||
this.method = method; | ||
this.retryAfter = retryAfter != null ? retryAfter.getTime() : null; | ||
} | ||
|
||
|
@@ -48,4 +51,8 @@ public RetryableException(String message, Date retryAfter) { | |
public Date retryAfter() { | ||
return retryAfter != null ? new Date(retryAfter) : null; | ||
} | ||
|
||
public String method() { | ||
return this.method; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By doing this you can eliminate the
.toBuilder().request(request).build()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I must have missed this one during testing. I'll remove it.