dubbo.apache.org/dubbo-go/v3@v3.1.1/xds/utils/balancer/stub/stub.go (about) 1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 /* 19 * 20 * Copyright 2020 gRPC authors. 21 * 22 */ 23 24 // Package stub implements a balancer for testing purposes. 25 package stub 26 27 import ( 28 "google.golang.org/grpc/balancer" 29 ) 30 31 // BalancerFuncs contains all balancer.Balancer functions with a preceding 32 // *BalancerData parameter for passing additional instance information. Any 33 // nil functions will never be called. 34 type BalancerFuncs struct { 35 // Init is called after ClientConn and BuildOptions are set in 36 // BalancerData. It may be used to initialize BalancerData.Data. 37 Init func(*BalancerData) 38 39 UpdateClientConnState func(*BalancerData, balancer.ClientConnState) error 40 ResolverError func(*BalancerData, error) 41 UpdateSubConnState func(*BalancerData, balancer.SubConn, balancer.SubConnState) 42 Close func(*BalancerData) 43 ExitIdle func(*BalancerData) 44 } 45 46 // BalancerData contains data relevant to a stub balancer. 47 type BalancerData struct { 48 // ClientConn is set by the builder. 49 ClientConn balancer.ClientConn 50 // BuildOptions is set by the builder. 51 BuildOptions balancer.BuildOptions 52 // Data may be used to store arbitrary user data. 53 Data interface{} 54 } 55 56 type bal struct { 57 bf BalancerFuncs 58 bd *BalancerData 59 } 60 61 func (b *bal) UpdateClientConnState(c balancer.ClientConnState) error { 62 if b.bf.UpdateClientConnState != nil { 63 return b.bf.UpdateClientConnState(b.bd, c) 64 } 65 return nil 66 } 67 68 func (b *bal) ResolverError(e error) { 69 if b.bf.ResolverError != nil { 70 b.bf.ResolverError(b.bd, e) 71 } 72 } 73 74 func (b *bal) UpdateSubConnState(sc balancer.SubConn, scs balancer.SubConnState) { 75 if b.bf.UpdateSubConnState != nil { 76 b.bf.UpdateSubConnState(b.bd, sc, scs) 77 } 78 } 79 80 func (b *bal) Close() { 81 if b.bf.Close != nil { 82 b.bf.Close(b.bd) 83 } 84 } 85 86 func (b *bal) ExitIdle() { 87 if b.bf.ExitIdle != nil { 88 b.bf.ExitIdle(b.bd) 89 } 90 } 91 92 type bb struct { 93 name string 94 bf BalancerFuncs 95 } 96 97 func (bb bb) Build(cc balancer.ClientConn, opts balancer.BuildOptions) balancer.Balancer { 98 b := &bal{bf: bb.bf, bd: &BalancerData{ClientConn: cc, BuildOptions: opts}} 99 if b.bf.Init != nil { 100 b.bf.Init(b.bd) 101 } 102 return b 103 } 104 105 func (bb bb) Name() string { return bb.name } 106 107 // Register registers a stub balancer builder which will call the provided 108 // functions. The name used should be unique. 109 func Register(name string, bf BalancerFuncs) { 110 balancer.Register(bb{name: name, bf: bf}) 111 }