Skip to content

Commit 4eb9dd0

Browse files
Move Request and Response to loadbalancer package. Fixes gh-772. (#773)
* Move Request and Response to loadbalancer package. Fixes gh-772. * Fix checkstyle. * Inherit and reference repackaged classes. * Revert "Inherit and reference repackaged classes." This reverts commit 02808c5. * Fix Status enums. * Make old classes extend the new ones.
1 parent 5e9f6c2 commit 4eb9dd0

File tree

14 files changed

+374
-83
lines changed

14 files changed

+374
-83
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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 org.springframework.core.style.ToStringCreator;
20+
21+
/**
22+
* @author Spencer Gibb
23+
*/
24+
// TODO: add metrics
25+
public class CompletionContext {
26+
27+
private final Status status;
28+
29+
private final Throwable throwable;
30+
31+
public CompletionContext(Status status) {
32+
this(status, null);
33+
}
34+
35+
public CompletionContext(Status status, Throwable throwable) {
36+
this.status = status;
37+
this.throwable = throwable;
38+
}
39+
40+
public Status status() {
41+
return this.status;
42+
}
43+
44+
public Throwable getThrowable() {
45+
return this.throwable;
46+
}
47+
48+
@Override
49+
public String toString() {
50+
ToStringCreator to = new ToStringCreator(this);
51+
to.append("status", this.status);
52+
to.append("throwable", this.throwable);
53+
return to.toString();
54+
}
55+
56+
/**
57+
* Request status state.
58+
*/
59+
public enum Status {
60+
61+
/** Request was handled successfully. */
62+
SUCCESS,
63+
/** Request reached the server but failed due to timeout or internal error. */
64+
FAILED,
65+
/** Request did not go off box and should not be counted for statistics. */
66+
DISCARD,
67+
68+
}
69+
70+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
/**
20+
* A default implementation of {@link Request}.
21+
*
22+
* @author Spencer Gibb
23+
* @author Olga Maciaszek-Sharma
24+
*/
25+
public class DefaultRequest<T> implements Request<T> {
26+
27+
private T context;
28+
29+
public DefaultRequest() {
30+
new DefaultRequestContext();
31+
}
32+
33+
public DefaultRequest(T context) {
34+
this.context = context;
35+
}
36+
37+
@Override
38+
public T getContext() {
39+
return context;
40+
}
41+
42+
public void setContext(T context) {
43+
this.context = context;
44+
}
45+
46+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
/**
20+
* Contains information relevant to the request.
21+
*
22+
* @author Olga Maciaszek-Sharma
23+
*/
24+
public class DefaultRequestContext {
25+
26+
/**
27+
* A {@link String} value of hint that can be used to choose the correct service
28+
* instance.
29+
*/
30+
private String hint = "default";
31+
32+
public DefaultRequestContext() {
33+
}
34+
35+
public DefaultRequestContext(String hint) {
36+
this.hint = hint;
37+
}
38+
39+
public String getHint() {
40+
return hint;
41+
}
42+
43+
public void setHint(String hint) {
44+
this.hint = hint;
45+
}
46+
47+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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 org.springframework.cloud.client.ServiceInstance;
20+
21+
/**
22+
* @author Spencer Gibb
23+
*/
24+
public class DefaultResponse implements Response<ServiceInstance> {
25+
26+
private final ServiceInstance serviceInstance;
27+
28+
public DefaultResponse(ServiceInstance serviceInstance) {
29+
this.serviceInstance = serviceInstance;
30+
}
31+
32+
@Override
33+
public boolean hasServer() {
34+
return this.serviceInstance != null;
35+
}
36+
37+
@Override
38+
public ServiceInstance getServer() {
39+
return this.serviceInstance;
40+
}
41+
42+
@Override
43+
public void onComplete(CompletionContext completionContext) {
44+
// TODO: implement
45+
}
46+
47+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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 org.springframework.cloud.client.ServiceInstance;
20+
21+
/**
22+
* @author Spencer Gibb
23+
*/
24+
public class EmptyResponse implements Response<ServiceInstance> {
25+
26+
@Override
27+
public boolean hasServer() {
28+
return false;
29+
}
30+
31+
@Override
32+
public ServiceInstance getServer() {
33+
return null;
34+
}
35+
36+
@Override
37+
public void onComplete(CompletionContext completionContext) {
38+
// TODO: implement
39+
}
40+
41+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
/**
20+
* Marker interface for a request.
21+
*
22+
* @author Spencer Gibb
23+
* @author Olga Maciaszek-Sharma
24+
*/
25+
public interface Request<C> {
26+
27+
// Avoid breaking backward compatibility
28+
default C getContext() {
29+
return null;
30+
}
31+
32+
// TODO: define contents
33+
34+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
/**
20+
* Response created for each request.
21+
*
22+
* @param <T> type of the server
23+
* @author Spencer Gibb
24+
*/
25+
public interface Response<T> {
26+
27+
boolean hasServer();
28+
29+
T getServer();
30+
31+
/**
32+
* Notification that the request completed.
33+
* @deprecated <code>onComplete</code> callbacks for load-balanced calls are being
34+
* moved to a separate interface
35+
* @param completionContext - completion context
36+
*/
37+
@Deprecated
38+
void onComplete(CompletionContext completionContext);
39+
40+
}

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

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,46 +16,46 @@
1616

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

19-
import org.springframework.core.style.ToStringCreator;
20-
2119
/**
20+
* @deprecated in favour of
21+
* {@link org.springframework.cloud.client.loadbalancer.CompletionContext}
2222
* @author Spencer Gibb
2323
*/
2424
// TODO: add metrics
25-
public class CompletionContext {
25+
@Deprecated
26+
public class CompletionContext
27+
extends org.springframework.cloud.client.loadbalancer.CompletionContext {
2628

2729
private final Status status;
2830

29-
private final Throwable throwable;
30-
3131
public CompletionContext(Status status) {
3232
this(status, null);
3333
}
3434

3535
public CompletionContext(Status status, Throwable throwable) {
36+
super(resolveStatus(status), throwable);
3637
this.status = status;
37-
this.throwable = throwable;
3838
}
3939

4040
public Status getStatus() {
4141
return this.status;
4242
}
4343

44-
public Throwable getThrowable() {
45-
return this.throwable;
46-
}
47-
48-
@Override
49-
public String toString() {
50-
ToStringCreator to = new ToStringCreator(this);
51-
to.append("status", this.status);
52-
to.append("throwable", this.throwable);
53-
return to.toString();
44+
private static org.springframework.cloud.client.loadbalancer.CompletionContext.Status resolveStatus(
45+
Status status) {
46+
if (Status.SUCCESSS.equals(status)) {
47+
return org.springframework.cloud.client.loadbalancer.CompletionContext.Status.SUCCESS;
48+
}
49+
return org.springframework.cloud.client.loadbalancer.CompletionContext.Status
50+
.valueOf(status.name());
5451
}
5552

5653
/**
5754
* Request status state.
55+
* @deprecated in favour of
56+
* {@link org.springframework.cloud.client.loadbalancer.CompletionContext.Status}
5857
*/
58+
@Deprecated
5959
public enum Status {
6060

6161
/** Request was handled successfully. */

0 commit comments

Comments
 (0)