The gRPC-go Require
- Go 1.15+
- gRPC 1.36.0
Each version corresponds to the corresponding version of GRPC-go.
For example, tag: v1.36.0 -> grpc-go v1.36.0
v1.45.0 & v1.46.0-dev running panic like this:
panic: runtime error: comparing uncomparable type MyAttribute
goroutine 1 [running]:
google.golang.org/grpc/attributes.(*Attributes).Equal(0xc00015e4b0, 0xc00013c140)
/project/vendor/google.golang.org/grpc/attributes/attributes.go:95 +0x194
google.golang.org/grpc/resolver.addressMapEntryList.find({0xc00013c1b8, 0x1, 0x8000102}, {{0xc00015e4c8, 0x13}, {0xc00015e4c8, 0xd}, 0xc00013c140, 0x0, 0x0, ...})
/project/vendor/google.golang.org/grpc/resolver/map.go:49 +0xb9
google.golang.org/grpc/resolver.(*AddressMap).Get(0xc00013c188, {{0xc00015e4c8, 0x13}, {0xc00015e4c8, 0xd}, 0xc00013c140, 0x0, 0x0, {0x0, 0x0}})
/project/vendor/google.golang.org/grpc/resolver/map.go:59 +0x94
mesh-sidecar/grpclient_balancer/balancer.(*baseBalancer).UpdateClientConnState(0xc00013b500, {{{0xc000414380, 0x3, 0x3}, 0x0, 0x0}, {0x0, 0x0}})
Your attribute
needs implement Equal
function. https://github.com/grpc/grpc-go/blob/v1.46.0-dev/attributes/attributes.go#L91
type MyAttribute struct {
attr string
}
func (ma MyAttribute) Equal(o interface{}) bool {
return true
}
The gRPC client-side load balancing to work need to main components, the naming resolver and the load balancing policy
The infra image source itnext.io
The gRPC client and server applications used in the example are based on the proto/echo & load_balancing examples found on the gRPC-go examples with the following modifications:
- round robin balancer.RoundRobinBalanceName
- weighted round robin balancer.WeightedRobinBalanceName
- random weighted round robin balancer.RandomWeightedRobinBalanceName
- minimum connection number balancer.MinConnectBalanceName
- minimum response consume balancer.MinRespTimeBalanceName, keep 10 response consume time, remove maximum and minimum and then take the average value.
-
Modify naming resolver with your requirements, first set attributes.Attributes for per endpoint address, second when one endpoint attributes.Attributes changed then update subConn state.
-
Implement yourself balancer & picker function, then based on attributes.Attributes picker subConn in
Pick(balancer.PickInfo) (balancer.PickResult, error)
Apache 2.0 license.