Kun Zhang | d3c5b00 | 2015-06-25 20:24:01 -0700 | [diff] [blame] | 1 | /* |
Carl Mastrangelo | 60a0b0c | 2018-05-03 14:55:21 -0700 | [diff] [blame] | 2 | * Copyright 2015 The gRPC Authors |
Kun Zhang | d3c5b00 | 2015-06-25 20:24:01 -0700 | [diff] [blame] | 3 | * |
Carl Mastrangelo | 3bfd630 | 2017-05-31 13:29:01 -0700 | [diff] [blame] | 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 |
Carl Mastrangelo | 166108a | 2017-06-01 14:28:37 -0700 | [diff] [blame] | 7 | * |
Carl Mastrangelo | 3bfd630 | 2017-05-31 13:29:01 -0700 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Carl Mastrangelo | 166108a | 2017-06-01 14:28:37 -0700 | [diff] [blame] | 9 | * |
Carl Mastrangelo | 3bfd630 | 2017-05-31 13:29:01 -0700 | [diff] [blame] | 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. |
Kun Zhang | d3c5b00 | 2015-06-25 20:24:01 -0700 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | package io.grpc; |
| 18 | |
Carl Mastrangelo | 8d49df2 | 2017-01-05 17:23:34 -0800 | [diff] [blame] | 19 | import static com.google.common.base.Preconditions.checkArgument; |
| 20 | |
Kun Zhang | 38ba586 | 2015-11-03 17:07:09 -0800 | [diff] [blame] | 21 | import com.google.common.base.MoreObjects; |
elandau | 053a18e | 2016-03-02 23:40:52 -0800 | [diff] [blame] | 22 | import com.google.common.base.Preconditions; |
Kun Zhang | 903197b | 2017-04-07 11:03:24 -0700 | [diff] [blame] | 23 | import java.util.ArrayList; |
elandau | 90323ad | 2016-06-02 09:14:48 -0700 | [diff] [blame] | 24 | import java.util.Arrays; |
Kun Zhang | 903197b | 2017-04-07 11:03:24 -0700 | [diff] [blame] | 25 | import java.util.Collections; |
| 26 | import java.util.List; |
Eric Anderson | f59e04f | 2015-08-04 12:27:36 -0700 | [diff] [blame] | 27 | import java.util.concurrent.Executor; |
Kun Zhang | d3c5b00 | 2015-06-25 20:24:01 -0700 | [diff] [blame] | 28 | import java.util.concurrent.TimeUnit; |
Grant Oakley | 6bf8e0c | 2018-05-02 15:56:52 -0700 | [diff] [blame] | 29 | import javax.annotation.CheckReturnValue; |
Kun Zhang | d3c5b00 | 2015-06-25 20:24:01 -0700 | [diff] [blame] | 30 | import javax.annotation.Nullable; |
| 31 | import javax.annotation.concurrent.Immutable; |
| 32 | |
| 33 | /** |
| 34 | * The collection of runtime options for a new RPC call. |
| 35 | * |
| 36 | * <p>A field that is not set is {@code null}. |
| 37 | */ |
| 38 | @Immutable |
Grant Oakley | 6bf8e0c | 2018-05-02 15:56:52 -0700 | [diff] [blame] | 39 | @CheckReturnValue |
Kun Zhang | d3c5b00 | 2015-06-25 20:24:01 -0700 | [diff] [blame] | 40 | public final class CallOptions { |
| 41 | /** |
| 42 | * A blank {@code CallOptions} that all fields are not set. |
| 43 | */ |
Eric Anderson | dceb764 | 2022-11-17 18:09:43 -0800 | [diff] [blame] | 44 | public static final CallOptions DEFAULT; |
| 45 | |
| 46 | static { |
| 47 | Builder b = new Builder(); |
| 48 | b.customOptions = new Object[0][2]; |
| 49 | b.streamTracerFactories = Collections.emptyList(); |
| 50 | DEFAULT = b.build(); |
| 51 | } |
Kun Zhang | d3c5b00 | 2015-06-25 20:24:01 -0700 | [diff] [blame] | 52 | |
Carl Mastrangelo | a508c1d | 2015-08-17 11:06:57 -0700 | [diff] [blame] | 53 | @Nullable |
pandaapo | 1b94f48 | 2022-11-18 05:11:27 +0800 | [diff] [blame] | 54 | private final Deadline deadline; |
Carl Mastrangelo | a508c1d | 2015-08-17 11:06:57 -0700 | [diff] [blame] | 55 | |
Kun Zhang | 432cec7 | 2016-05-29 14:43:10 -0700 | [diff] [blame] | 56 | @Nullable |
pandaapo | 1b94f48 | 2022-11-18 05:11:27 +0800 | [diff] [blame] | 57 | private final Executor executor; |
Kun Zhang | 432cec7 | 2016-05-29 14:43:10 -0700 | [diff] [blame] | 58 | |
Carl Mastrangelo | a3c79e8 | 2016-01-27 16:49:41 -0800 | [diff] [blame] | 59 | @Nullable |
pandaapo | 1b94f48 | 2022-11-18 05:11:27 +0800 | [diff] [blame] | 60 | private final String authority; |
Jakob Buchgraber | 33cd6be | 2016-06-15 10:29:05 +0200 | [diff] [blame] | 61 | |
pandaapo | 1b94f48 | 2022-11-18 05:11:27 +0800 | [diff] [blame] | 62 | @Nullable |
| 63 | private final CallCredentials credentials; |
Carl Mastrangelo | a3c79e8 | 2016-01-27 16:49:41 -0800 | [diff] [blame] | 64 | |
pandaapo | 1b94f48 | 2022-11-18 05:11:27 +0800 | [diff] [blame] | 65 | @Nullable |
| 66 | private final String compressorName; |
| 67 | |
| 68 | private final Object[][] customOptions; |
| 69 | |
| 70 | private final List<ClientStreamTracer.Factory> streamTracerFactories; |
Kun Zhang | 903197b | 2017-04-07 11:03:24 -0700 | [diff] [blame] | 71 | |
Carl Mastrangelo | a508c1d | 2015-08-17 11:06:57 -0700 | [diff] [blame] | 72 | /** |
ZHANG Dapeng | 9d4a43f | 2016-06-11 09:30:16 -0700 | [diff] [blame] | 73 | * Opposite to fail fast. |
| 74 | */ |
Carl Mastrangelo | 53a3f12 | 2019-04-25 10:56:40 -0700 | [diff] [blame] | 75 | @Nullable |
pandaapo | 1b94f48 | 2022-11-18 05:11:27 +0800 | [diff] [blame] | 76 | private final Boolean waitForReady; |
ZHANG Dapeng | 9d4a43f | 2016-06-11 09:30:16 -0700 | [diff] [blame] | 77 | |
Carl Mastrangelo | 8d49df2 | 2017-01-05 17:23:34 -0800 | [diff] [blame] | 78 | @Nullable |
pandaapo | 1b94f48 | 2022-11-18 05:11:27 +0800 | [diff] [blame] | 79 | private final Integer maxInboundMessageSize; |
Carl Mastrangelo | 8d49df2 | 2017-01-05 17:23:34 -0800 | [diff] [blame] | 80 | @Nullable |
pandaapo | 1b94f48 | 2022-11-18 05:11:27 +0800 | [diff] [blame] | 81 | private final Integer maxOutboundMessageSize; |
Carl Mastrangelo | 8d49df2 | 2017-01-05 17:23:34 -0800 | [diff] [blame] | 82 | |
pandaapo | 1b94f48 | 2022-11-18 05:11:27 +0800 | [diff] [blame] | 83 | private CallOptions(Builder builder) { |
| 84 | this.deadline = builder.deadline; |
| 85 | this.executor = builder.executor; |
| 86 | this.authority = builder.authority; |
| 87 | this.credentials = builder.credentials; |
| 88 | this.compressorName = builder.compressorName; |
| 89 | this.customOptions = builder.customOptions; |
| 90 | this.streamTracerFactories = builder.streamTracerFactories; |
| 91 | this.waitForReady = builder.waitForReady; |
| 92 | this.maxInboundMessageSize = builder.maxInboundMessageSize; |
| 93 | this.maxOutboundMessageSize = builder.maxOutboundMessageSize; |
| 94 | } |
| 95 | |
| 96 | static class Builder { |
| 97 | Deadline deadline; |
| 98 | Executor executor; |
| 99 | String authority; |
| 100 | CallCredentials credentials; |
| 101 | String compressorName; |
Eric Anderson | dceb764 | 2022-11-17 18:09:43 -0800 | [diff] [blame] | 102 | Object[][] customOptions; |
pandaapo | 1b94f48 | 2022-11-18 05:11:27 +0800 | [diff] [blame] | 103 | // Unmodifiable list |
Eric Anderson | dceb764 | 2022-11-17 18:09:43 -0800 | [diff] [blame] | 104 | List<ClientStreamTracer.Factory> streamTracerFactories; |
pandaapo | 1b94f48 | 2022-11-18 05:11:27 +0800 | [diff] [blame] | 105 | Boolean waitForReady; |
| 106 | Integer maxInboundMessageSize; |
| 107 | Integer maxOutboundMessageSize; |
| 108 | |
| 109 | private CallOptions build() { |
| 110 | return new CallOptions(this); |
| 111 | } |
| 112 | } |
Carl Mastrangelo | 8d49df2 | 2017-01-05 17:23:34 -0800 | [diff] [blame] | 113 | |
ZHANG Dapeng | 9d4a43f | 2016-06-11 09:30:16 -0700 | [diff] [blame] | 114 | /** |
Carl Mastrangelo | a508c1d | 2015-08-17 11:06:57 -0700 | [diff] [blame] | 115 | * Override the HTTP/2 authority the channel claims to be connecting to. <em>This is not |
| 116 | * generally safe.</em> Overriding allows advanced users to re-use a single Channel for multiple |
| 117 | * services, even if those services are hosted on different domain names. That assumes the |
| 118 | * server is virtually hosting multiple domains and is guaranteed to continue doing so. It is |
| 119 | * rare for a service provider to make such a guarantee. <em>At this time, there is no security |
| 120 | * verification of the overridden value, such as making sure the authority matches the server's |
| 121 | * TLS certificate.</em> |
| 122 | */ |
Carl Mastrangelo | 0f9e3fa | 2016-05-03 13:03:05 -0700 | [diff] [blame] | 123 | @ExperimentalApi("https://github.com/grpc/grpc-java/issues/1767") |
Carl Mastrangelo | a508c1d | 2015-08-17 11:06:57 -0700 | [diff] [blame] | 124 | public CallOptions withAuthority(@Nullable String authority) { |
pandaapo | 1b94f48 | 2022-11-18 05:11:27 +0800 | [diff] [blame] | 125 | Builder builder = toBuilder(this); |
| 126 | builder.authority = authority; |
| 127 | return builder.build(); |
Carl Mastrangelo | a508c1d | 2015-08-17 11:06:57 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Kun Zhang | d3c5b00 | 2015-06-25 20:24:01 -0700 | [diff] [blame] | 130 | /** |
Kun Zhang | 432cec7 | 2016-05-29 14:43:10 -0700 | [diff] [blame] | 131 | * Returns a new {@code CallOptions} with the given call credentials. |
| 132 | */ |
Eric Anderson | 32fd329 | 2016-06-30 16:58:34 -0700 | [diff] [blame] | 133 | public CallOptions withCallCredentials(@Nullable CallCredentials credentials) { |
pandaapo | 1b94f48 | 2022-11-18 05:11:27 +0800 | [diff] [blame] | 134 | Builder builder = toBuilder(this); |
| 135 | builder.credentials = credentials; |
| 136 | return builder.build(); |
Kun Zhang | 432cec7 | 2016-05-29 14:43:10 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | /** |
Carl Mastrangelo | a3c79e8 | 2016-01-27 16:49:41 -0800 | [diff] [blame] | 140 | * Sets the compression to use for the call. The compressor must be a valid name known in the |
Larry Safran | 136665f | 2023-03-09 20:25:43 +0000 | [diff] [blame] | 141 | * {@link CompressorRegistry}. By default, the "gzip" compressor will be available. |
Eric Anderson | 57e94d1 | 2019-03-11 11:32:34 -0700 | [diff] [blame] | 142 | * |
| 143 | * <p>It is only safe to call this if the server supports the compression format chosen. There is |
| 144 | * no negotiation performed; if the server does not support the compression chosen, the call will |
| 145 | * fail. |
Carl Mastrangelo | a3c79e8 | 2016-01-27 16:49:41 -0800 | [diff] [blame] | 146 | */ |
Carl Mastrangelo | a3c79e8 | 2016-01-27 16:49:41 -0800 | [diff] [blame] | 147 | public CallOptions withCompression(@Nullable String compressorName) { |
pandaapo | 1b94f48 | 2022-11-18 05:11:27 +0800 | [diff] [blame] | 148 | Builder builder = toBuilder(this); |
| 149 | builder.compressorName = compressorName; |
| 150 | return builder.build(); |
Carl Mastrangelo | a3c79e8 | 2016-01-27 16:49:41 -0800 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | /** |
buchgr | 32ddf9f | 2016-03-24 13:23:50 +0100 | [diff] [blame] | 154 | * Returns a new {@code CallOptions} with the given absolute deadline. |
| 155 | * |
| 156 | * <p>This is mostly used for propagating an existing deadline. {@link #withDeadlineAfter} is the |
| 157 | * recommended way of setting a new deadline, |
| 158 | * |
| 159 | * @param deadline the deadline or {@code null} for unsetting the deadline. |
| 160 | */ |
buchgr | 32ddf9f | 2016-03-24 13:23:50 +0100 | [diff] [blame] | 161 | public CallOptions withDeadline(@Nullable Deadline deadline) { |
pandaapo | 1b94f48 | 2022-11-18 05:11:27 +0800 | [diff] [blame] | 162 | Builder builder = toBuilder(this); |
| 163 | builder.deadline = deadline; |
| 164 | return builder.build(); |
buchgr | 32ddf9f | 2016-03-24 13:23:50 +0100 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | /** |
Kun Zhang | d3c5b00 | 2015-06-25 20:24:01 -0700 | [diff] [blame] | 168 | * Returns a new {@code CallOptions} with a deadline that is after the given {@code duration} from |
| 169 | * now. |
| 170 | */ |
| 171 | public CallOptions withDeadlineAfter(long duration, TimeUnit unit) { |
buchgr | 32ddf9f | 2016-03-24 13:23:50 +0100 | [diff] [blame] | 172 | return withDeadline(Deadline.after(duration, unit)); |
Kun Zhang | d3c5b00 | 2015-06-25 20:24:01 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | /** |
buchgr | 32ddf9f | 2016-03-24 13:23:50 +0100 | [diff] [blame] | 176 | * Returns the deadline or {@code null} if the deadline is not set. |
| 177 | */ |
buchgr | 32ddf9f | 2016-03-24 13:23:50 +0100 | [diff] [blame] | 178 | @Nullable |
| 179 | public Deadline getDeadline() { |
| 180 | return deadline; |
Kun Zhang | d3c5b00 | 2015-06-25 20:24:01 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Carl Mastrangelo | 77b00e0 | 2015-08-11 11:28:55 -0700 | [diff] [blame] | 183 | /** |
zpencer | 347eb09 | 2017-05-17 17:12:45 -0700 | [diff] [blame] | 184 | * Enables <a href="https://github.com/grpc/grpc/blob/master/doc/wait-for-ready.md"> |
Eric Anderson | 4674b27 | 2020-04-30 15:30:24 -0700 | [diff] [blame] | 185 | * 'wait for ready'</a> for the call. Wait-for-ready queues the RPC until a connection is |
| 186 | * available. This may dramatically increase the latency of the RPC, but avoids failing |
| 187 | * "unnecessarily." The default queues the RPC until an attempt to connect has completed, but |
| 188 | * fails RPCs without sending them if unable to connect. |
ZHANG Dapeng | 9d4a43f | 2016-06-11 09:30:16 -0700 | [diff] [blame] | 189 | */ |
ZHANG Dapeng | 9d4a43f | 2016-06-11 09:30:16 -0700 | [diff] [blame] | 190 | public CallOptions withWaitForReady() { |
pandaapo | 1b94f48 | 2022-11-18 05:11:27 +0800 | [diff] [blame] | 191 | Builder builder = toBuilder(this); |
| 192 | builder.waitForReady = Boolean.TRUE; |
| 193 | return builder.build(); |
ZHANG Dapeng | 9d4a43f | 2016-06-11 09:30:16 -0700 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Disables 'wait for ready' feature for the call. |
| 198 | * This method should be rarely used because the default is without 'wait for ready'. |
| 199 | */ |
ZHANG Dapeng | 9d4a43f | 2016-06-11 09:30:16 -0700 | [diff] [blame] | 200 | public CallOptions withoutWaitForReady() { |
pandaapo | 1b94f48 | 2022-11-18 05:11:27 +0800 | [diff] [blame] | 201 | Builder builder = toBuilder(this); |
| 202 | builder.waitForReady = Boolean.FALSE; |
| 203 | return builder.build(); |
ZHANG Dapeng | 9d4a43f | 2016-06-11 09:30:16 -0700 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | /** |
Carl Mastrangelo | a3c79e8 | 2016-01-27 16:49:41 -0800 | [diff] [blame] | 207 | * Returns the compressor's name. |
| 208 | */ |
Carl Mastrangelo | a3c79e8 | 2016-01-27 16:49:41 -0800 | [diff] [blame] | 209 | @Nullable |
| 210 | public String getCompressor() { |
| 211 | return compressorName; |
| 212 | } |
| 213 | |
Kun Zhang | 942f4c9 | 2015-09-04 17:21:44 -0700 | [diff] [blame] | 214 | /** |
Carl Mastrangelo | a508c1d | 2015-08-17 11:06:57 -0700 | [diff] [blame] | 215 | * Override the HTTP/2 authority the channel claims to be connecting to. <em>This is not |
| 216 | * generally safe.</em> Overriding allows advanced users to re-use a single Channel for multiple |
| 217 | * services, even if those services are hosted on different domain names. That assumes the |
| 218 | * server is virtually hosting multiple domains and is guaranteed to continue doing so. It is |
| 219 | * rare for a service provider to make such a guarantee. <em>At this time, there is no security |
| 220 | * verification of the overridden value, such as making sure the authority matches the server's |
| 221 | * TLS certificate.</em> |
| 222 | */ |
| 223 | @Nullable |
Carl Mastrangelo | 0f9e3fa | 2016-05-03 13:03:05 -0700 | [diff] [blame] | 224 | @ExperimentalApi("https://github.com/grpc/grpc-java/issues/1767") |
Carl Mastrangelo | a508c1d | 2015-08-17 11:06:57 -0700 | [diff] [blame] | 225 | public String getAuthority() { |
| 226 | return authority; |
| 227 | } |
| 228 | |
Eric Anderson | f59e04f | 2015-08-04 12:27:36 -0700 | [diff] [blame] | 229 | /** |
Kun Zhang | 432cec7 | 2016-05-29 14:43:10 -0700 | [diff] [blame] | 230 | * Returns the call credentials. |
| 231 | */ |
Kun Zhang | 432cec7 | 2016-05-29 14:43:10 -0700 | [diff] [blame] | 232 | @Nullable |
| 233 | public CallCredentials getCredentials() { |
| 234 | return credentials; |
| 235 | } |
| 236 | |
| 237 | /** |
Eric Anderson | f59e04f | 2015-08-04 12:27:36 -0700 | [diff] [blame] | 238 | * Returns a new {@code CallOptions} with {@code executor} to be used instead of the default |
| 239 | * executor specified with {@link ManagedChannelBuilder#executor}. |
| 240 | */ |
David Hoover | 87cf404 | 2018-12-19 15:47:54 -0800 | [diff] [blame] | 241 | public CallOptions withExecutor(@Nullable Executor executor) { |
pandaapo | 1b94f48 | 2022-11-18 05:11:27 +0800 | [diff] [blame] | 242 | Builder builder = toBuilder(this); |
| 243 | builder.executor = executor; |
| 244 | return builder.build(); |
Eric Anderson | f59e04f | 2015-08-04 12:27:36 -0700 | [diff] [blame] | 245 | } |
Jakob Buchgraber | 33cd6be | 2016-06-15 10:29:05 +0200 | [diff] [blame] | 246 | |
Kun Zhang | 903197b | 2017-04-07 11:03:24 -0700 | [diff] [blame] | 247 | /** |
| 248 | * Returns a new {@code CallOptions} with a {@code ClientStreamTracerFactory} in addition to |
| 249 | * the existing factories. |
| 250 | * |
| 251 | * <p>This method doesn't replace existing factories, or try to de-duplicate factories. |
| 252 | */ |
| 253 | @ExperimentalApi("https://github.com/grpc/grpc-java/issues/2861") |
| 254 | public CallOptions withStreamTracerFactory(ClientStreamTracer.Factory factory) { |
Kun Zhang | 903197b | 2017-04-07 11:03:24 -0700 | [diff] [blame] | 255 | ArrayList<ClientStreamTracer.Factory> newList = |
Carl Mastrangelo | 3a39b81 | 2019-02-04 10:03:50 -0800 | [diff] [blame] | 256 | new ArrayList<>(streamTracerFactories.size() + 1); |
Kun Zhang | 903197b | 2017-04-07 11:03:24 -0700 | [diff] [blame] | 257 | newList.addAll(streamTracerFactories); |
| 258 | newList.add(factory); |
pandaapo | 1b94f48 | 2022-11-18 05:11:27 +0800 | [diff] [blame] | 259 | Builder builder = toBuilder(this); |
| 260 | builder.streamTracerFactories = Collections.unmodifiableList(newList); |
| 261 | return builder.build(); |
Kun Zhang | 903197b | 2017-04-07 11:03:24 -0700 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | /** |
| 265 | * Returns an immutable list of {@code ClientStreamTracerFactory}s. |
| 266 | */ |
| 267 | @ExperimentalApi("https://github.com/grpc/grpc-java/issues/2861") |
| 268 | public List<ClientStreamTracer.Factory> getStreamTracerFactories() { |
| 269 | return streamTracerFactories; |
| 270 | } |
| 271 | |
Muxi Yan | f674776 | 2017-05-04 10:57:49 -0700 | [diff] [blame] | 272 | /** |
| 273 | * Key for a key-value pair. Uses reference equality. |
| 274 | */ |
elandau | 90323ad | 2016-06-02 09:14:48 -0700 | [diff] [blame] | 275 | public static final class Key<T> { |
zpencer | 04a90bc | 2018-05-15 10:03:46 -0700 | [diff] [blame] | 276 | private final String debugString; |
elandau | 90323ad | 2016-06-02 09:14:48 -0700 | [diff] [blame] | 277 | private final T defaultValue; |
| 278 | |
zpencer | 04a90bc | 2018-05-15 10:03:46 -0700 | [diff] [blame] | 279 | private Key(String debugString, T defaultValue) { |
| 280 | this.debugString = debugString; |
elandau | 90323ad | 2016-06-02 09:14:48 -0700 | [diff] [blame] | 281 | this.defaultValue = defaultValue; |
| 282 | } |
| 283 | |
zpencer | 347eb09 | 2017-05-17 17:12:45 -0700 | [diff] [blame] | 284 | /** |
| 285 | * Returns the user supplied default value for this key. |
| 286 | */ |
elandau | 90323ad | 2016-06-02 09:14:48 -0700 | [diff] [blame] | 287 | public T getDefault() { |
| 288 | return defaultValue; |
| 289 | } |
| 290 | |
| 291 | @Override |
| 292 | public String toString() { |
zpencer | 04a90bc | 2018-05-15 10:03:46 -0700 | [diff] [blame] | 293 | return debugString; |
elandau | 90323ad | 2016-06-02 09:14:48 -0700 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | /** |
| 297 | * Factory method for creating instances of {@link Key}. |
| 298 | * |
zpencer | 04a90bc | 2018-05-15 10:03:46 -0700 | [diff] [blame] | 299 | * @param debugString a string used to describe this key, used for debugging. |
elandau | 90323ad | 2016-06-02 09:14:48 -0700 | [diff] [blame] | 300 | * @param defaultValue default value to return when value for key not set |
| 301 | * @param <T> Key type |
| 302 | * @return Key object |
zpencer | e806e38 | 2018-05-15 15:56:42 -0700 | [diff] [blame] | 303 | * @deprecated Use {@link #create} or {@link #createWithDefault} instead. This method will |
| 304 | * be removed. |
elandau | 90323ad | 2016-06-02 09:14:48 -0700 | [diff] [blame] | 305 | */ |
zpencer | e806e38 | 2018-05-15 15:56:42 -0700 | [diff] [blame] | 306 | @ExperimentalApi("https://github.com/grpc/grpc-java/issues/1869") |
zpencer | 04a90bc | 2018-05-15 10:03:46 -0700 | [diff] [blame] | 307 | @Deprecated |
| 308 | public static <T> Key<T> of(String debugString, T defaultValue) { |
| 309 | Preconditions.checkNotNull(debugString, "debugString"); |
Carl Mastrangelo | 3a39b81 | 2019-02-04 10:03:50 -0800 | [diff] [blame] | 310 | return new Key<>(debugString, defaultValue); |
zpencer | 04a90bc | 2018-05-15 10:03:46 -0700 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | /** |
| 314 | * Factory method for creating instances of {@link Key}. The default value of the |
| 315 | * key is {@code null}. |
| 316 | * |
| 317 | * @param debugString a debug string that describes this key. |
| 318 | * @param <T> Key type |
| 319 | * @return Key object |
| 320 | * @since 1.13.0 |
| 321 | */ |
| 322 | public static <T> Key<T> create(String debugString) { |
| 323 | Preconditions.checkNotNull(debugString, "debugString"); |
Carl Mastrangelo | 3a39b81 | 2019-02-04 10:03:50 -0800 | [diff] [blame] | 324 | return new Key<>(debugString, /*defaultValue=*/ null); |
zpencer | 04a90bc | 2018-05-15 10:03:46 -0700 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | /** |
| 328 | * Factory method for creating instances of {@link Key}. |
| 329 | * |
| 330 | * @param debugString a debug string that describes this key. |
| 331 | * @param defaultValue default value to return when value for key not set |
| 332 | * @param <T> Key type |
| 333 | * @return Key object |
| 334 | * @since 1.13.0 |
| 335 | */ |
| 336 | public static <T> Key<T> createWithDefault(String debugString, T defaultValue) { |
| 337 | Preconditions.checkNotNull(debugString, "debugString"); |
Carl Mastrangelo | 3a39b81 | 2019-02-04 10:03:50 -0800 | [diff] [blame] | 338 | return new Key<>(debugString, defaultValue); |
elandau | 90323ad | 2016-06-02 09:14:48 -0700 | [diff] [blame] | 339 | } |
| 340 | } |
| 341 | |
| 342 | /** |
Jakob Buchgraber | 33cd6be | 2016-06-15 10:29:05 +0200 | [diff] [blame] | 343 | * Sets a custom option. Any existing value for the key is overwritten. |
| 344 | * |
elandau | 90323ad | 2016-06-02 09:14:48 -0700 | [diff] [blame] | 345 | * @param key The option key |
| 346 | * @param value The option value. |
zpencer | 04a90bc | 2018-05-15 10:03:46 -0700 | [diff] [blame] | 347 | * @since 1.13.0 |
elandau | 90323ad | 2016-06-02 09:14:48 -0700 | [diff] [blame] | 348 | */ |
elandau | 90323ad | 2016-06-02 09:14:48 -0700 | [diff] [blame] | 349 | public <T> CallOptions withOption(Key<T> key, T value) { |
Carl Mastrangelo | 1285477 | 2016-08-12 14:52:00 -0700 | [diff] [blame] | 350 | Preconditions.checkNotNull(key, "key"); |
| 351 | Preconditions.checkNotNull(value, "value"); |
Jakob Buchgraber | 33cd6be | 2016-06-15 10:29:05 +0200 | [diff] [blame] | 352 | |
pandaapo | 1b94f48 | 2022-11-18 05:11:27 +0800 | [diff] [blame] | 353 | Builder builder = toBuilder(this); |
Jakob Buchgraber | 33cd6be | 2016-06-15 10:29:05 +0200 | [diff] [blame] | 354 | int existingIdx = -1; |
| 355 | for (int i = 0; i < customOptions.length; i++) { |
| 356 | if (key.equals(customOptions[i][0])) { |
| 357 | existingIdx = i; |
| 358 | break; |
| 359 | } |
elandau | 90323ad | 2016-06-02 09:14:48 -0700 | [diff] [blame] | 360 | } |
Jakob Buchgraber | 33cd6be | 2016-06-15 10:29:05 +0200 | [diff] [blame] | 361 | |
pandaapo | 1b94f48 | 2022-11-18 05:11:27 +0800 | [diff] [blame] | 362 | builder.customOptions = new Object[customOptions.length + (existingIdx == -1 ? 1 : 0)][2]; |
| 363 | System.arraycopy(customOptions, 0, builder.customOptions, 0, customOptions.length); |
Jakob Buchgraber | 33cd6be | 2016-06-15 10:29:05 +0200 | [diff] [blame] | 364 | |
| 365 | if (existingIdx == -1) { |
| 366 | // Add a new option |
pandaapo | 1b94f48 | 2022-11-18 05:11:27 +0800 | [diff] [blame] | 367 | builder.customOptions[customOptions.length] = new Object[] {key, value}; |
Jakob Buchgraber | 33cd6be | 2016-06-15 10:29:05 +0200 | [diff] [blame] | 368 | } else { |
| 369 | // Replace an existing option |
pandaapo | 1b94f48 | 2022-11-18 05:11:27 +0800 | [diff] [blame] | 370 | builder.customOptions[existingIdx] = new Object[] {key, value}; |
Jakob Buchgraber | 33cd6be | 2016-06-15 10:29:05 +0200 | [diff] [blame] | 371 | } |
| 372 | |
pandaapo | 1b94f48 | 2022-11-18 05:11:27 +0800 | [diff] [blame] | 373 | return builder.build(); |
elandau | 90323ad | 2016-06-02 09:14:48 -0700 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | /** |
| 377 | * Get the value for a custom option or its inherent default. |
| 378 | * @param key Key identifying option |
| 379 | */ |
| 380 | @ExperimentalApi("https://github.com/grpc/grpc-java/issues/1869") |
| 381 | @SuppressWarnings("unchecked") |
| 382 | public <T> T getOption(Key<T> key) { |
Carl Mastrangelo | 1285477 | 2016-08-12 14:52:00 -0700 | [diff] [blame] | 383 | Preconditions.checkNotNull(key, "key"); |
elandau | 90323ad | 2016-06-02 09:14:48 -0700 | [diff] [blame] | 384 | for (int i = 0; i < customOptions.length; i++) { |
| 385 | if (key.equals(customOptions[i][0])) { |
| 386 | return (T) customOptions[i][1]; |
| 387 | } |
| 388 | } |
| 389 | return key.defaultValue; |
| 390 | } |
Eric Anderson | f59e04f | 2015-08-04 12:27:36 -0700 | [diff] [blame] | 391 | |
yifeizhuang | 7eeb411 | 2022-02-11 15:07:09 -0800 | [diff] [blame] | 392 | /** |
| 393 | * Returns the executor override to use for this specific call, or {@code null} if there is no |
| 394 | * override. The executor is only for servicing this one call, so is not safe to use after |
| 395 | * {@link ClientCall.Listener#onClose}. |
| 396 | */ |
Eric Anderson | f59e04f | 2015-08-04 12:27:36 -0700 | [diff] [blame] | 397 | @Nullable |
| 398 | public Executor getExecutor() { |
| 399 | return executor; |
| 400 | } |
| 401 | |
Kun Zhang | d3c5b00 | 2015-06-25 20:24:01 -0700 | [diff] [blame] | 402 | /** |
zpencer | 347eb09 | 2017-05-17 17:12:45 -0700 | [diff] [blame] | 403 | * Returns whether <a href="https://github.com/grpc/grpc/blob/master/doc/wait-for-ready.md"> |
| 404 | * 'wait for ready'</a> option is enabled for the call. 'Fail fast' is the default option for gRPC |
| 405 | * calls and 'wait for ready' is the opposite to it. |
ZHANG Dapeng | 9d4a43f | 2016-06-11 09:30:16 -0700 | [diff] [blame] | 406 | */ |
ZHANG Dapeng | 9d4a43f | 2016-06-11 09:30:16 -0700 | [diff] [blame] | 407 | public boolean isWaitForReady() { |
Carl Mastrangelo | 53a3f12 | 2019-04-25 10:56:40 -0700 | [diff] [blame] | 408 | return Boolean.TRUE.equals(waitForReady); |
| 409 | } |
| 410 | |
| 411 | Boolean getWaitForReady() { |
ZHANG Dapeng | 9d4a43f | 2016-06-11 09:30:16 -0700 | [diff] [blame] | 412 | return waitForReady; |
| 413 | } |
| 414 | |
| 415 | /** |
Carl Mastrangelo | 8d49df2 | 2017-01-05 17:23:34 -0800 | [diff] [blame] | 416 | * Sets the maximum allowed message size acceptable from the remote peer. If unset, this will |
| 417 | * default to the value set on the {@link ManagedChannelBuilder#maxInboundMessageSize(int)}. |
| 418 | */ |
| 419 | @ExperimentalApi("https://github.com/grpc/grpc-java/issues/2563") |
| 420 | public CallOptions withMaxInboundMessageSize(int maxSize) { |
| 421 | checkArgument(maxSize >= 0, "invalid maxsize %s", maxSize); |
pandaapo | 1b94f48 | 2022-11-18 05:11:27 +0800 | [diff] [blame] | 422 | Builder builder = toBuilder(this); |
| 423 | builder.maxInboundMessageSize = maxSize; |
| 424 | return builder.build(); |
Carl Mastrangelo | 8d49df2 | 2017-01-05 17:23:34 -0800 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | /** |
| 428 | * Sets the maximum allowed message size acceptable sent to the remote peer. |
| 429 | */ |
| 430 | @ExperimentalApi("https://github.com/grpc/grpc-java/issues/2563") |
| 431 | public CallOptions withMaxOutboundMessageSize(int maxSize) { |
| 432 | checkArgument(maxSize >= 0, "invalid maxsize %s", maxSize); |
pandaapo | 1b94f48 | 2022-11-18 05:11:27 +0800 | [diff] [blame] | 433 | Builder builder = toBuilder(this); |
| 434 | builder.maxOutboundMessageSize = maxSize; |
| 435 | return builder.build(); |
Carl Mastrangelo | 8d49df2 | 2017-01-05 17:23:34 -0800 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | /** |
| 439 | * Gets the maximum allowed message size acceptable from the remote peer. |
| 440 | */ |
| 441 | @Nullable |
| 442 | @ExperimentalApi("https://github.com/grpc/grpc-java/issues/2563") |
| 443 | public Integer getMaxInboundMessageSize() { |
| 444 | return maxInboundMessageSize; |
| 445 | } |
| 446 | |
| 447 | /** |
| 448 | * Gets the maximum allowed message size acceptable to send the remote peer. |
| 449 | */ |
| 450 | @Nullable |
| 451 | @ExperimentalApi("https://github.com/grpc/grpc-java/issues/2563") |
| 452 | public Integer getMaxOutboundMessageSize() { |
| 453 | return maxOutboundMessageSize; |
| 454 | } |
| 455 | |
| 456 | /** |
pandaapo | 1b94f48 | 2022-11-18 05:11:27 +0800 | [diff] [blame] | 457 | * Copy CallOptions. |
Kun Zhang | d3c5b00 | 2015-06-25 20:24:01 -0700 | [diff] [blame] | 458 | */ |
pandaapo | 1b94f48 | 2022-11-18 05:11:27 +0800 | [diff] [blame] | 459 | private static Builder toBuilder(CallOptions other) { |
| 460 | Builder builder = new Builder(); |
| 461 | builder.deadline = other.deadline; |
| 462 | builder.executor = other.executor; |
| 463 | builder.authority = other.authority; |
| 464 | builder.credentials = other.credentials; |
| 465 | builder.compressorName = other.compressorName; |
| 466 | builder.customOptions = other.customOptions; |
| 467 | builder.streamTracerFactories = other.streamTracerFactories; |
| 468 | builder.waitForReady = other.waitForReady; |
| 469 | builder.maxInboundMessageSize = other.maxInboundMessageSize; |
| 470 | builder.maxOutboundMessageSize = other.maxOutboundMessageSize; |
| 471 | return builder; |
Kun Zhang | d3c5b00 | 2015-06-25 20:24:01 -0700 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | @Override |
| 475 | public String toString() { |
Carl Mastrangelo | 8d49df2 | 2017-01-05 17:23:34 -0800 | [diff] [blame] | 476 | return MoreObjects.toStringHelper(this) |
| 477 | .add("deadline", deadline) |
| 478 | .add("authority", authority) |
| 479 | .add("callCredentials", credentials) |
Carl Mastrangelo | 8d49df2 | 2017-01-05 17:23:34 -0800 | [diff] [blame] | 480 | .add("executor", executor != null ? executor.getClass() : null) |
| 481 | .add("compressorName", compressorName) |
| 482 | .add("customOptions", Arrays.deepToString(customOptions)) |
| 483 | .add("waitForReady", isWaitForReady()) |
| 484 | .add("maxInboundMessageSize", maxInboundMessageSize) |
| 485 | .add("maxOutboundMessageSize", maxOutboundMessageSize) |
Kun Zhang | 903197b | 2017-04-07 11:03:24 -0700 | [diff] [blame] | 486 | .add("streamTracerFactories", streamTracerFactories) |
Carl Mastrangelo | 8d49df2 | 2017-01-05 17:23:34 -0800 | [diff] [blame] | 487 | .toString(); |
Kun Zhang | d3c5b00 | 2015-06-25 20:24:01 -0700 | [diff] [blame] | 488 | } |
| 489 | } |