1 /* 2 * Copyright 2020 The gRPC 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 * http://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 io.grpc; 18 19 import static com.google.common.base.Preconditions.checkArgument; 20 import static com.google.common.base.Preconditions.checkNotNull; 21 import static com.google.common.base.Preconditions.checkState; 22 23 import javax.annotation.Nullable; 24 25 // The class can not be located in io.grpc.internal since it is used as a cross-module API. 26 // Otherwise, shading would break it. 27 /** 28 * Per method config selector that the channel or load balancers will use to choose the appropriate 29 * config or take config related actions for an RPC. 30 */ 31 @Internal 32 public abstract class InternalConfigSelector { 33 @NameResolver.ResolutionResultAttr 34 public static final Attributes.Key<io.grpc.InternalConfigSelector> KEY 35 = Attributes.Key.create("internal:io.grpc.config-selector"); 36 37 // Use PickSubchannelArgs for SelectConfigArgs for now. May change over time. 38 /** Selects the config for an PRC. */ selectConfig(LoadBalancer.PickSubchannelArgs args)39 public abstract Result selectConfig(LoadBalancer.PickSubchannelArgs args); 40 41 public static final class Result { 42 private final Status status; 43 private final Object config; 44 @Nullable 45 public ClientInterceptor interceptor; 46 Result( Status status, Object config, ClientInterceptor interceptor)47 private Result( 48 Status status, Object config, ClientInterceptor interceptor) { 49 this.status = checkNotNull(status, "status"); 50 this.config = config; 51 this.interceptor = interceptor; 52 } 53 54 /** 55 * Creates a {@code Result} with the given error status. 56 */ forError(Status status)57 public static Result forError(Status status) { 58 checkArgument(!status.isOk(), "status is OK"); 59 return new Result(status, null, null); 60 } 61 62 /** 63 * Returns the status of the config selection operation. If status is not {@link Status#OK}, 64 * this result should not be used. 65 */ getStatus()66 public Status getStatus() { 67 return status; 68 } 69 70 /** 71 * Returns a parsed config. Must have been returned via 72 * ServiceConfigParser.parseServiceConfig().getConfig() 73 */ getConfig()74 public Object getConfig() { 75 return config; 76 } 77 78 /** 79 * Returns an interceptor that will be applies to calls. 80 */ 81 @Nullable getInterceptor()82 public ClientInterceptor getInterceptor() { 83 return interceptor; 84 } 85 newBuilder()86 public static Builder newBuilder() { 87 return new Builder(); 88 } 89 90 public static final class Builder { 91 private Object config; 92 private ClientInterceptor interceptor; 93 Builder()94 private Builder() {} 95 96 /** 97 * Sets the parsed config. This field is required. 98 * 99 * @return this 100 */ setConfig(Object config)101 public Builder setConfig(Object config) { 102 this.config = checkNotNull(config, "config"); 103 return this; 104 } 105 106 /** 107 * Sets the interceptor. This field is optional. 108 * 109 * @return this 110 */ setInterceptor(ClientInterceptor interceptor)111 public Builder setInterceptor(ClientInterceptor interceptor) { 112 this.interceptor = checkNotNull(interceptor, "interceptor"); 113 return this; 114 } 115 116 /** 117 * Build this {@link Result}. 118 */ build()119 public Result build() { 120 checkState(config != null, "config is not set"); 121 return new Result(Status.OK, config, interceptor); 122 } 123 } 124 } 125 } 126