|
|
|
@ -50,19 +50,15 @@ type Builder struct { |
|
|
|
|
// Build creates a grpc Picker.
|
|
|
|
|
func (b *Builder) Build(info base.PickerBuildInfo) gBalancer.Picker { |
|
|
|
|
nodes := make([]selector.Node, 0) |
|
|
|
|
subConns := make(map[string]gBalancer.SubConn) |
|
|
|
|
for conn, info := range info.ReadySCs { |
|
|
|
|
if _, ok := subConns[info.Address.Addr]; ok { |
|
|
|
|
continue |
|
|
|
|
} |
|
|
|
|
subConns[info.Address.Addr] = conn |
|
|
|
|
|
|
|
|
|
ins, _ := info.Address.Attributes.Value("rawServiceInstance").(*registry.ServiceInstance) |
|
|
|
|
nodes = append(nodes, selector.NewNode(info.Address.Addr, ins)) |
|
|
|
|
nodes = append(nodes, &grpcNode{ |
|
|
|
|
Node: selector.NewNode(info.Address.Addr, ins), |
|
|
|
|
subConn: conn, |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
p := &Picker{ |
|
|
|
|
selector: b.builder.Build(), |
|
|
|
|
subConns: subConns, |
|
|
|
|
} |
|
|
|
|
p.selector.Apply(nodes) |
|
|
|
|
return p |
|
|
|
@ -70,7 +66,6 @@ func (b *Builder) Build(info base.PickerBuildInfo) gBalancer.Picker { |
|
|
|
|
|
|
|
|
|
// Picker is a grpc picker.
|
|
|
|
|
type Picker struct { |
|
|
|
|
subConns map[string]gBalancer.SubConn |
|
|
|
|
selector selector.Selector |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -87,10 +82,9 @@ func (p *Picker) Pick(info gBalancer.PickInfo) (gBalancer.PickResult, error) { |
|
|
|
|
if err != nil { |
|
|
|
|
return gBalancer.PickResult{}, err |
|
|
|
|
} |
|
|
|
|
sub := p.subConns[n.Address()] |
|
|
|
|
|
|
|
|
|
return gBalancer.PickResult{ |
|
|
|
|
SubConn: sub, |
|
|
|
|
SubConn: n.(*grpcNode).subConn, |
|
|
|
|
Done: func(di gBalancer.DoneInfo) { |
|
|
|
|
done(info.Ctx, selector.DoneInfo{ |
|
|
|
|
Err: di.Err, |
|
|
|
@ -113,3 +107,8 @@ func (t Trailer) Get(k string) string { |
|
|
|
|
} |
|
|
|
|
return "" |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
type grpcNode struct { |
|
|
|
|
selector.Node |
|
|
|
|
subConn gBalancer.SubConn |
|
|
|
|
} |
|
|
|
|