Skip to content

Commit dce413a

Browse files
Do not pass response body to LB lifecycle beans. (#864)
* Do not pass response body to LB lifecycle beans. * Fix argument name.
1 parent 0f98419 commit dce413a

File tree

13 files changed

+293
-189
lines changed

13 files changed

+293
-189
lines changed

spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/DefaultResponse.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.cloud.client.loadbalancer;
1818

19+
import java.util.Objects;
20+
1921
import org.springframework.cloud.client.ServiceInstance;
2022
import org.springframework.core.style.ToStringCreator;
2123

@@ -53,4 +55,21 @@ public String toString() {
5355
return to.toString();
5456
}
5557

58+
@Override
59+
public boolean equals(Object o) {
60+
if (this == o) {
61+
return true;
62+
}
63+
if (!(o instanceof DefaultResponse)) {
64+
return false;
65+
}
66+
DefaultResponse that = (DefaultResponse) o;
67+
return Objects.equals(serviceInstance, that.serviceInstance);
68+
}
69+
70+
@Override
71+
public int hashCode() {
72+
return Objects.hash(serviceInstance);
73+
}
74+
5675
}

spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/HttpRequestContext.java

Lines changed: 0 additions & 38 deletions
This file was deleted.
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
* Copyright 2012-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.client.loadbalancer;
18+
19+
import java.net.URI;
20+
import java.util.HashMap;
21+
import java.util.Map;
22+
import java.util.Objects;
23+
24+
import org.springframework.core.style.ToStringCreator;
25+
import org.springframework.http.HttpHeaders;
26+
import org.springframework.http.HttpMethod;
27+
import org.springframework.http.HttpRequest;
28+
import org.springframework.util.MultiValueMap;
29+
import org.springframework.web.reactive.function.client.ClientRequest;
30+
31+
/**
32+
* Represents the data of the request that can be safely read (without passing request reactive stream values).
33+
*
34+
* @author Olga Maciaszek-Sharma
35+
* @since 3.0.0
36+
*/
37+
public class RequestData {
38+
39+
private final HttpMethod httpMethod;
40+
41+
private final URI url;
42+
43+
private final HttpHeaders headers;
44+
45+
private final MultiValueMap<String, String> cookies;
46+
47+
private final Map<String, Object> attributes;
48+
49+
public RequestData(HttpMethod httpMethod, URI url, HttpHeaders headers, MultiValueMap<String, String> cookies,
50+
Map<String, Object> attributes) {
51+
this.httpMethod = httpMethod;
52+
this.url = url;
53+
this.headers = headers;
54+
this.cookies = cookies;
55+
this.attributes = attributes;
56+
}
57+
58+
public RequestData(ClientRequest request) {
59+
this(request.method(), request.url(), request.headers(), request.cookies(), request.attributes());
60+
}
61+
62+
public RequestData(HttpRequest request) {
63+
this(request.getMethod(), request.getURI(), request.getHeaders(), null, new HashMap<>());
64+
}
65+
66+
public HttpMethod getHttpMethod() {
67+
return httpMethod;
68+
}
69+
70+
public URI getUrl() {
71+
return url;
72+
}
73+
74+
public HttpHeaders getHeaders() {
75+
return headers;
76+
}
77+
78+
public MultiValueMap<String, String> getCookies() {
79+
return cookies;
80+
}
81+
82+
public Map<String, Object> getAttributes() {
83+
return attributes;
84+
}
85+
86+
@Override
87+
public String toString() {
88+
ToStringCreator to = new ToStringCreator(this);
89+
to.append("httpMethod", httpMethod);
90+
to.append("url", url);
91+
to.append("headers", headers);
92+
to.append("cookies", cookies);
93+
return to.toString();
94+
}
95+
96+
@Override
97+
public boolean equals(Object o) {
98+
if (this == o) {
99+
return true;
100+
}
101+
if (!(o instanceof RequestData)) {
102+
return false;
103+
}
104+
RequestData that = (RequestData) o;
105+
return httpMethod == that.httpMethod && Objects.equals(url, that.url) && Objects.equals(headers, that.headers)
106+
&& Objects.equals(cookies, that.cookies) && Objects.equals(attributes, that.attributes);
107+
}
108+
109+
@Override
110+
public int hashCode() {
111+
return Objects.hash(httpMethod, url, headers, cookies, attributes);
112+
}
113+
114+
}
Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,29 @@
1717
package org.springframework.cloud.client.loadbalancer;
1818

1919
import org.springframework.http.HttpMethod;
20-
import org.springframework.web.reactive.function.client.ClientRequest;
2120

2221
/**
22+
* A {@link RequestData}-based {@link DefaultRequestContext}.
23+
*
2324
* @author Olga Maciaszek-Sharma
2425
* @since 3.0.0
2526
*/
26-
public class ClientRequestContext extends DefaultRequestContext {
27+
public class RequestDataContext extends DefaultRequestContext {
2728

28-
public ClientRequestContext(ClientRequest clientRequest) {
29-
this(clientRequest, "default");
29+
public RequestDataContext(RequestData requestData) {
30+
this(requestData, "default");
3031
}
3132

32-
public ClientRequestContext(ClientRequest clientRequest, String hint) {
33-
super(clientRequest, hint);
33+
public RequestDataContext(RequestData requestData, String hint) {
34+
super(requestData, hint);
3435
}
3536

36-
public ClientRequest getClientRequest() {
37-
return (ClientRequest) super.getClientRequest();
37+
public RequestData getClientRequest() {
38+
return (RequestData) super.getClientRequest();
3839
}
3940

4041
public HttpMethod method() {
41-
return ((ClientRequest) super.getClientRequest()).method();
42+
return ((RequestData) super.getClientRequest()).getHttpMethod();
4243
}
4344

4445
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
* Copyright 2012-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.client.loadbalancer;
18+
19+
import java.util.Objects;
20+
21+
import org.springframework.core.style.ToStringCreator;
22+
import org.springframework.http.HttpHeaders;
23+
import org.springframework.http.HttpStatus;
24+
import org.springframework.http.ResponseCookie;
25+
import org.springframework.util.MultiValueMap;
26+
import org.springframework.web.reactive.function.client.ClientResponse;
27+
28+
/**
29+
* Represents the data of the request that can be safely read (without passing request reactive stream values).
30+
*
31+
* @author Olga Maciaszek-Sharma
32+
* @since 3.0.0
33+
*/
34+
public class ResponseData {
35+
36+
private final HttpStatus httpStatus;
37+
38+
private final HttpHeaders headers;
39+
40+
private final MultiValueMap<String, ResponseCookie> cookies;
41+
42+
private final RequestData requestData;
43+
44+
public ResponseData(HttpStatus httpStatus, HttpHeaders headers, MultiValueMap<String, ResponseCookie> cookies,
45+
RequestData requestData) {
46+
this.httpStatus = httpStatus;
47+
this.headers = headers;
48+
this.cookies = cookies;
49+
this.requestData = requestData;
50+
}
51+
52+
public ResponseData(ClientResponse response, RequestData requestData) {
53+
httpStatus = response.statusCode();
54+
headers = response.headers().asHttpHeaders();
55+
cookies = response.cookies();
56+
this.requestData = requestData;
57+
}
58+
59+
public HttpStatus getHttpStatus() {
60+
return httpStatus;
61+
}
62+
63+
public HttpHeaders getHeaders() {
64+
return headers;
65+
}
66+
67+
public MultiValueMap<String, ResponseCookie> getCookies() {
68+
return cookies;
69+
}
70+
71+
public RequestData getRequestData() {
72+
return requestData;
73+
}
74+
75+
@Override
76+
public String toString() {
77+
ToStringCreator to = new ToStringCreator(this);
78+
to.append("httpStatus", httpStatus);
79+
return to.toString();
80+
}
81+
82+
@Override
83+
public boolean equals(Object o) {
84+
if (this == o) {
85+
return true;
86+
}
87+
if (!(o instanceof ResponseData)) {
88+
return false;
89+
}
90+
ResponseData that = (ResponseData) o;
91+
return httpStatus == that.httpStatus && Objects.equals(headers, that.headers)
92+
&& Objects.equals(cookies, that.cookies) && Objects.equals(requestData, that.requestData);
93+
}
94+
95+
@Override
96+
public int hashCode() {
97+
return Objects.hash(httpStatus, headers, cookies, requestData);
98+
}
99+
100+
}

spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/RetryLoadBalancerInterceptor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public ClientHttpResponse intercept(final HttpRequest request, final byte[] body
9090
Set<LoadBalancerLifecycle> supportedLifecycleProcessors = LoadBalancerLifecycleValidator
9191
.getSupportedLifecycleProcessors(
9292
loadBalancerFactory.getInstances(serviceName, LoadBalancerLifecycle.class),
93-
HttpRequestContext.class, ClientHttpResponse.class, ServiceInstance.class);
93+
RequestDataContext.class, ResponseData.class, ServiceInstance.class);
9494
if (serviceInstance == null) {
9595
if (LOG.isDebugEnabled()) {
9696
LOG.debug("Service instance retrieved from LoadBalancedRetryContext: was null. "
@@ -103,7 +103,7 @@ public ClientHttpResponse intercept(final HttpRequest request, final byte[] body
103103
}
104104
String hint = getHint(serviceName);
105105
DefaultRequest<RetryableRequestContext> lbRequest = new DefaultRequest<>(
106-
new RetryableRequestContext(previousServiceInstance, request, hint));
106+
new RetryableRequestContext(previousServiceInstance, new RequestData(request), hint));
107107
supportedLifecycleProcessors.forEach(lifecycle -> lifecycle.onStart(lbRequest));
108108
serviceInstance = loadBalancer.choose(serviceName, lbRequest);
109109
if (LOG.isDebugEnabled()) {

spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/ServerHttpRequestContext.java

Lines changed: 0 additions & 44 deletions
This file was deleted.

0 commit comments

Comments
 (0)