|
| 1 | +/* |
| 2 | + * Copyright 2023-2024 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 | +package org.springframework.grpc.client; |
| 17 | + |
| 18 | +import java.time.Duration; |
| 19 | +import java.util.function.Consumer; |
| 20 | + |
| 21 | +import org.springframework.util.unit.DataSize; |
| 22 | + |
| 23 | +import io.grpc.ManagedChannel; |
| 24 | + |
| 25 | +/** |
| 26 | + * Represents the configuration for a {@link ManagedChannel gRPC channel}. |
| 27 | + * |
| 28 | + * @author Chris Bono |
| 29 | + */ |
| 30 | +public class NamedChannel { |
| 31 | + |
| 32 | + /** |
| 33 | + * The target address uri to connect to. |
| 34 | + */ |
| 35 | + private String address = "static://localhost:9090"; |
| 36 | + |
| 37 | + public String getAddress() { |
| 38 | + return this.address; |
| 39 | + } |
| 40 | + |
| 41 | + public void setAddress(final String address) { |
| 42 | + this.address = address; |
| 43 | + } |
| 44 | + |
| 45 | + // -------------------------------------------------- |
| 46 | + // defaultLoadBalancingPolicy |
| 47 | + // -------------------------------------------------- |
| 48 | + |
| 49 | + /** |
| 50 | + * The default load balancing policy the channel should use. |
| 51 | + */ |
| 52 | + private String defaultLoadBalancingPolicy = "round_robin"; |
| 53 | + |
| 54 | + public String getDefaultLoadBalancingPolicy() { |
| 55 | + return this.defaultLoadBalancingPolicy; |
| 56 | + } |
| 57 | + |
| 58 | + public void setDefaultLoadBalancingPolicy(final String defaultLoadBalancingPolicy) { |
| 59 | + this.defaultLoadBalancingPolicy = defaultLoadBalancingPolicy; |
| 60 | + } |
| 61 | + |
| 62 | + // -------------------------------------------------- |
| 63 | + |
| 64 | + private final Health health = new Health(); |
| 65 | + |
| 66 | + public Health getHealth() { |
| 67 | + return this.health; |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * The negotiation type for the channel. |
| 72 | + */ |
| 73 | + private NegotiationType negotiationType = NegotiationType.PLAINTEXT; |
| 74 | + |
| 75 | + public NegotiationType getNegotiationType() { |
| 76 | + return this.negotiationType; |
| 77 | + } |
| 78 | + |
| 79 | + public void setNegotiationType(NegotiationType negotiationType) { |
| 80 | + this.negotiationType = negotiationType; |
| 81 | + } |
| 82 | + |
| 83 | + // -------------------------------------------------- |
| 84 | + // KeepAlive |
| 85 | + // -------------------------------------------------- |
| 86 | + |
| 87 | + /** |
| 88 | + * Whether keep alive is enabled on the channel. |
| 89 | + */ |
| 90 | + private boolean enableKeepAlive = false; |
| 91 | + |
| 92 | + public boolean isEnableKeepAlive() { |
| 93 | + return this.enableKeepAlive; |
| 94 | + } |
| 95 | + |
| 96 | + public void setEnableKeepAlive(boolean enableKeepAlive) { |
| 97 | + this.enableKeepAlive = enableKeepAlive; |
| 98 | + } |
| 99 | + |
| 100 | + // -------------------------------------------------- |
| 101 | + |
| 102 | + /** |
| 103 | + * The duration without ongoing RPCs before going to idle mode. |
| 104 | + */ |
| 105 | + private Duration idleTimeout = Duration.ofSeconds(20); |
| 106 | + |
| 107 | + public Duration getIdleTimeout() { |
| 108 | + return this.idleTimeout; |
| 109 | + } |
| 110 | + |
| 111 | + public void setIdleTimeout(Duration idleTimeout) { |
| 112 | + this.idleTimeout = idleTimeout; |
| 113 | + } |
| 114 | + |
| 115 | + // -------------------------------------------------- |
| 116 | + |
| 117 | + /** |
| 118 | + * The delay before sending a keepAlive. Note that shorter intervals increase the |
| 119 | + * network burden for the server and this value can not be lower than |
| 120 | + * 'permitKeepAliveTime' on the server. |
| 121 | + */ |
| 122 | + private Duration keepAliveTime = Duration.ofMinutes(5); |
| 123 | + |
| 124 | + public Duration getKeepAliveTime() { |
| 125 | + return this.keepAliveTime; |
| 126 | + } |
| 127 | + |
| 128 | + public void setKeepAliveTime(Duration keepAliveTime) { |
| 129 | + this.keepAliveTime = keepAliveTime; |
| 130 | + } |
| 131 | + |
| 132 | + // -------------------------------------------------- |
| 133 | + |
| 134 | + /** |
| 135 | + * The default timeout for a keepAlives ping request. |
| 136 | + */ |
| 137 | + private Duration keepAliveTimeout = Duration.ofSeconds(20); |
| 138 | + |
| 139 | + public Duration getKeepAliveTimeout() { |
| 140 | + return this.keepAliveTimeout; |
| 141 | + } |
| 142 | + |
| 143 | + public void setKeepAliveTimeout(Duration keepAliveTimeout) { |
| 144 | + this.keepAliveTimeout = keepAliveTimeout; |
| 145 | + } |
| 146 | + |
| 147 | + // -------------------------------------------------- |
| 148 | + |
| 149 | + /** |
| 150 | + * Whether a keepAlive will be performed when there are no outstanding RPC on a |
| 151 | + * connection. |
| 152 | + */ |
| 153 | + private boolean keepAliveWithoutCalls = false; |
| 154 | + |
| 155 | + public boolean isKeepAliveWithoutCalls() { |
| 156 | + return this.keepAliveWithoutCalls; |
| 157 | + } |
| 158 | + |
| 159 | + public void setKeepAliveWithoutCalls(boolean keepAliveWithoutCalls) { |
| 160 | + this.keepAliveWithoutCalls = keepAliveWithoutCalls; |
| 161 | + } |
| 162 | + |
| 163 | + // -------------------------------------------------- |
| 164 | + // Message Transfer |
| 165 | + // -------------------------------------------------- |
| 166 | + |
| 167 | + /** |
| 168 | + * Maximum message size allowed to be received by the channel (default 4MiB). Set to |
| 169 | + * '-1' to use the highest possible limit (not recommended). |
| 170 | + */ |
| 171 | + private DataSize maxInboundMessageSize = DataSize.ofBytes(4194304); |
| 172 | + |
| 173 | + /** |
| 174 | + * Maximum metadata size allowed to be received by the channel (default 8KiB). Set to |
| 175 | + * '-1' to use the highest possible limit (not recommended). |
| 176 | + */ |
| 177 | + private DataSize maxInboundMetadataSize = DataSize.ofBytes(8192); |
| 178 | + |
| 179 | + public DataSize getMaxInboundMessageSize() { |
| 180 | + return this.maxInboundMessageSize; |
| 181 | + } |
| 182 | + |
| 183 | + public void setMaxInboundMessageSize(final DataSize maxInboundMessageSize) { |
| 184 | + this.setMaxInboundSize(maxInboundMessageSize, (s) -> this.maxInboundMessageSize = s, "maxInboundMesssageSize"); |
| 185 | + } |
| 186 | + |
| 187 | + public DataSize getMaxInboundMetadataSize() { |
| 188 | + return this.maxInboundMetadataSize; |
| 189 | + } |
| 190 | + |
| 191 | + public void setMaxInboundMetadataSize(DataSize maxInboundMetadataSize) { |
| 192 | + this.setMaxInboundSize(maxInboundMetadataSize, (s) -> this.maxInboundMetadataSize = s, |
| 193 | + "maxInboundMetadataSize"); |
| 194 | + } |
| 195 | + |
| 196 | + private void setMaxInboundSize(DataSize maxSize, Consumer<DataSize> setter, String propertyName) { |
| 197 | + if (maxSize != null && maxSize.toBytes() >= 0) { |
| 198 | + setter.accept(maxSize); |
| 199 | + } |
| 200 | + else if (maxSize != null && maxSize.toBytes() == -1) { |
| 201 | + setter.accept(DataSize.ofBytes(Integer.MAX_VALUE)); |
| 202 | + } |
| 203 | + else { |
| 204 | + throw new IllegalArgumentException("Unsupported %s: %s".formatted(propertyName, maxSize)); |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + // -------------------------------------------------- |
| 209 | + |
| 210 | + /** |
| 211 | + * The custom User-Agent for the channel. |
| 212 | + */ |
| 213 | + private String userAgent = null; |
| 214 | + |
| 215 | + public String getUserAgent() { |
| 216 | + return this.userAgent; |
| 217 | + } |
| 218 | + |
| 219 | + public void setUserAgent(final String userAgent) { |
| 220 | + this.userAgent = userAgent; |
| 221 | + } |
| 222 | + |
| 223 | + /** |
| 224 | + * Provide a copy of the channel instance. |
| 225 | + * @return a copy of the channel instance. |
| 226 | + */ |
| 227 | + public NamedChannel copy() { |
| 228 | + NamedChannel copy = new NamedChannel(); |
| 229 | + copy.address = this.address; |
| 230 | + copy.defaultLoadBalancingPolicy = this.defaultLoadBalancingPolicy; |
| 231 | + copy.negotiationType = this.negotiationType; |
| 232 | + copy.enableKeepAlive = this.enableKeepAlive; |
| 233 | + copy.idleTimeout = this.idleTimeout; |
| 234 | + copy.keepAliveTime = this.keepAliveTime; |
| 235 | + copy.keepAliveTimeout = this.keepAliveTimeout; |
| 236 | + copy.keepAliveWithoutCalls = this.keepAliveWithoutCalls; |
| 237 | + copy.maxInboundMessageSize = this.maxInboundMessageSize; |
| 238 | + copy.maxInboundMetadataSize = this.maxInboundMetadataSize; |
| 239 | + copy.userAgent = this.userAgent; |
| 240 | + copy.health.copyValuesFrom(this.getHealth()); |
| 241 | + copy.ssl.copyValuesFrom(this.getSsl()); |
| 242 | + return copy; |
| 243 | + } |
| 244 | + |
| 245 | + // -------------------------------------------------- |
| 246 | + |
| 247 | + /** |
| 248 | + * Flag to say that strict SSL checks are not enabled (so the remote certificate could |
| 249 | + * be anonymous). |
| 250 | + */ |
| 251 | + private boolean secure = true; |
| 252 | + |
| 253 | + public boolean isSecure() { |
| 254 | + return this.secure; |
| 255 | + } |
| 256 | + |
| 257 | + public void setSecure(boolean secure) { |
| 258 | + this.secure = secure; |
| 259 | + } |
| 260 | + |
| 261 | + // -------------------------------------------------- |
| 262 | + |
| 263 | + private final Ssl ssl = new Ssl(); |
| 264 | + |
| 265 | + public Ssl getSsl() { |
| 266 | + return this.ssl; |
| 267 | + } |
| 268 | + |
| 269 | + public static class Ssl { |
| 270 | + |
| 271 | + /** |
| 272 | + * Whether to enable SSL support. Enabled automatically if "bundle" is provided |
| 273 | + * unless specified otherwise. |
| 274 | + */ |
| 275 | + private Boolean enabled; |
| 276 | + |
| 277 | + /** |
| 278 | + * SSL bundle name. |
| 279 | + */ |
| 280 | + private String bundle; |
| 281 | + |
| 282 | + public boolean isEnabled() { |
| 283 | + return (this.enabled != null) ? this.enabled : this.bundle != null; |
| 284 | + } |
| 285 | + |
| 286 | + public void setEnabled(boolean enabled) { |
| 287 | + this.enabled = enabled; |
| 288 | + } |
| 289 | + |
| 290 | + public String getBundle() { |
| 291 | + return this.bundle; |
| 292 | + } |
| 293 | + |
| 294 | + public void setBundle(String bundle) { |
| 295 | + this.bundle = bundle; |
| 296 | + } |
| 297 | + |
| 298 | + /** |
| 299 | + * Copies the values from another instance. |
| 300 | + * @param other instance to copy values from |
| 301 | + */ |
| 302 | + public void copyValuesFrom(Ssl other) { |
| 303 | + this.enabled = other.enabled; |
| 304 | + this.bundle = other.bundle; |
| 305 | + } |
| 306 | + |
| 307 | + } |
| 308 | + |
| 309 | + public static class Health { |
| 310 | + |
| 311 | + /** |
| 312 | + * Whether to enable client-side health check for the channel. |
| 313 | + */ |
| 314 | + private boolean enabled = false; |
| 315 | + |
| 316 | + /** |
| 317 | + * Name of the service to check health on. |
| 318 | + */ |
| 319 | + private String serviceName; |
| 320 | + |
| 321 | + public boolean isEnabled() { |
| 322 | + return this.enabled; |
| 323 | + } |
| 324 | + |
| 325 | + public void setEnabled(boolean enabled) { |
| 326 | + this.enabled = enabled; |
| 327 | + } |
| 328 | + |
| 329 | + public String getServiceName() { |
| 330 | + return this.serviceName; |
| 331 | + } |
| 332 | + |
| 333 | + public void setServiceName(String serviceName) { |
| 334 | + this.serviceName = serviceName; |
| 335 | + } |
| 336 | + |
| 337 | + /** |
| 338 | + * Copies the values from another instance. |
| 339 | + * @param other instance to copy values from |
| 340 | + */ |
| 341 | + public void copyValuesFrom(Health other) { |
| 342 | + this.enabled = other.enabled; |
| 343 | + this.serviceName = other.serviceName; |
| 344 | + } |
| 345 | + |
| 346 | + } |
| 347 | + |
| 348 | +} |
0 commit comments