1 /* 2 * Copyright 2019 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.grpclb; 18 19 import static com.google.common.base.Preconditions.checkArgument; 20 import static com.google.common.base.Preconditions.checkNotNull; 21 22 import com.google.common.base.MoreObjects; 23 import com.google.common.base.Objects; 24 import io.grpc.grpclb.GrpclbState.Mode; 25 import javax.annotation.Nullable; 26 27 final class GrpclbConfig { 28 29 private final Mode mode; 30 @Nullable 31 private final String serviceName; 32 private final long fallbackTimeoutMs; 33 GrpclbConfig(Mode mode, @Nullable String serviceName, long fallbackTimeoutMs)34 private GrpclbConfig(Mode mode, @Nullable String serviceName, long fallbackTimeoutMs) { 35 this.mode = checkNotNull(mode, "mode"); 36 this.serviceName = serviceName; 37 this.fallbackTimeoutMs = fallbackTimeoutMs; 38 } 39 create(Mode mode)40 static GrpclbConfig create(Mode mode) { 41 return create(mode, null, GrpclbState.FALLBACK_TIMEOUT_MS); 42 } 43 create(Mode mode, @Nullable String serviceName, long fallbackTimeoutMs)44 static GrpclbConfig create(Mode mode, @Nullable String serviceName, long fallbackTimeoutMs) { 45 checkArgument(fallbackTimeoutMs > 0, "Invalid timeout (%s)", fallbackTimeoutMs); 46 return new GrpclbConfig(mode, serviceName, fallbackTimeoutMs); 47 } 48 getMode()49 Mode getMode() { 50 return mode; 51 } 52 getFallbackTimeoutMs()53 long getFallbackTimeoutMs() { 54 return fallbackTimeoutMs; 55 } 56 57 /** 58 * If specified, it overrides the name of the sevice name to be sent to the balancer. if not, the 59 * target to be sent to the balancer will continue to be obtained from the target URI passed 60 * to the gRPC client channel. 61 */ 62 @Nullable getServiceName()63 String getServiceName() { 64 return serviceName; 65 } 66 67 @Override equals(Object o)68 public boolean equals(Object o) { 69 if (this == o) { 70 return true; 71 } 72 if (o == null || getClass() != o.getClass()) { 73 return false; 74 } 75 GrpclbConfig that = (GrpclbConfig) o; 76 return mode == that.mode 77 && Objects.equal(serviceName, that.serviceName) 78 && fallbackTimeoutMs == that.fallbackTimeoutMs; 79 } 80 81 @Override hashCode()82 public int hashCode() { 83 return Objects.hashCode(mode, serviceName, fallbackTimeoutMs); 84 } 85 86 @Override toString()87 public String toString() { 88 return MoreObjects.toStringHelper(this) 89 .add("mode", mode) 90 .add("serviceName", serviceName) 91 .add("fallbackTimeoutMs", fallbackTimeoutMs) 92 .toString(); 93 } 94 } 95