forked from grpc/grpc-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInternalConfigSelector.java
More file actions
146 lines (127 loc) · 4.32 KB
/
InternalConfigSelector.java
File metadata and controls
146 lines (127 loc) · 4.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
* Copyright 2020 The gRPC Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.grpc;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import javax.annotation.Nullable;
// The class can not be located in io.grpc.internal since it is used as a cross-module API.
// Otherwise, shading would break it.
/**
* Per method config selector that the channel or load balancers will use to choose the appropriate
* config or take config related actions for an RPC.
*/
@Internal
public abstract class InternalConfigSelector {
@NameResolver.ResolutionResultAttr
public static final Attributes.Key<io.grpc.InternalConfigSelector> KEY
= Attributes.Key.create("io.grpc.config-selector");
// Use PickSubchannelArgs for SelectConfigArgs for now. May change over time.
/** Selects the config for an PRC. */
public abstract Result selectConfig(LoadBalancer.PickSubchannelArgs args);
public static final class Result {
private final Status status;
private final Object config;
private final CallOptions callOptions;
@Nullable
private final Runnable committedCallback;
private Result(
Status status, Object config, CallOptions callOptions, Runnable committedCallback) {
this.status = checkNotNull(status, "status");
this.config = config;
this.callOptions = callOptions;
this.committedCallback = committedCallback;
}
/**
* Creates a {@code Result} with the given error status.
*/
public static Result forError(Status status) {
checkArgument(!status.isOk(), "status is OK");
return new Result(status, null, null, null);
}
/**
* Returns the status of the config selection operation. If status is not {@link Status#OK},
* this result should not be used.
*/
public Status getStatus() {
return status;
}
/**
* Returns a parsed config. Must have been returned via
* ServiceConfigParser.parseServiceConfig().getConfig()
*/
public Object getConfig() {
return config;
}
/**
* Returns a config-selector-modified CallOptions for the RPC.
*/
public CallOptions getCallOptions() {
return callOptions;
}
/**
* Returns a callback to be invoked when the RPC no longer needs a picker.
*/
@Nullable
public Runnable getCommittedCallback() {
return committedCallback;
}
public static Builder newBuilder() {
return new Builder();
}
public static final class Builder {
private Object config;
private CallOptions callOptions;
private Runnable committedCallback;
private Builder() {}
/**
* Sets the parsed config. This field is required.
*
* @return this
*/
public Builder setConfig(Object config) {
this.config = checkNotNull(config, "config");
return this;
}
/**
* Sets the CallOptions. This field is required.
*
* @return this
*/
public Builder setCallOptions(CallOptions callOptions) {
this.callOptions = checkNotNull(callOptions, "callOptions");
return this;
}
/**
* Sets the interceptor. This field is optional.
*
* @return this
*/
public Builder setCommittedCallback(Runnable committedCallback) {
this.committedCallback = checkNotNull(committedCallback, "committedCallback");
return this;
}
/**
* Build this {@link Result}.
*/
public Result build() {
checkState(config != null, "config is not set");
checkState(callOptions != null, "callOptions is not set");
return new Result(Status.OK, config, callOptions, committedCallback);
}
}
}
}