try grpc lb interface

master
kevin 4 years ago
parent a1c9db0f80
commit 353cb5000f

@ -1,7 +1,8 @@
package balancer package roundrobin
import ( import (
"context" "context"
"fmt"
"math/rand" "math/rand"
"sync" "sync"
"time" "time"
@ -11,16 +12,16 @@ import (
"google.golang.org/grpc/resolver" "google.golang.org/grpc/resolver"
) )
const Name = "roundrobin" const Name = "zero_rr"
func init() { func init() {
balancer.Register(newBuilder()) balancer.Register(newRoundRobinBuilder())
} }
type roundRobinPickerBuilder struct { type roundRobinPickerBuilder struct {
} }
func newBuilder() balancer.Builder { func newRoundRobinBuilder() balancer.Builder {
return base.NewBalancerBuilder(Name, new(roundRobinPickerBuilder)) return base.NewBalancerBuilder(Name, new(roundRobinPickerBuilder))
} }
@ -48,6 +49,7 @@ type roundRobinPicker struct {
func (p *roundRobinPicker) Pick(ctx context.Context, info balancer.PickInfo) ( func (p *roundRobinPicker) Pick(ctx context.Context, info balancer.PickInfo) (
conn balancer.SubConn, done func(balancer.DoneInfo), err error) { conn balancer.SubConn, done func(balancer.DoneInfo), err error) {
fmt.Println(p.conns)
p.lock.Lock() p.lock.Lock()
defer p.lock.Unlock() defer p.lock.Unlock()

@ -0,0 +1,36 @@
package internal
import (
"zero/core/discov"
"zero/rpcx/internal/balancer/roundrobin"
"zero/rpcx/internal/resolver"
"google.golang.org/grpc"
"google.golang.org/grpc/connectivity"
)
type DiscovClient struct {
conn *grpc.ClientConn
}
func NewDiscovClient(etcd discov.EtcdConf, opts ...ClientOption) (*DiscovClient, error) {
resolver.RegisterResolver(etcd)
opts = append(opts, WithDialOption(grpc.WithBalancerName(roundrobin.Name)))
conn, err := dial("discov:///", opts...)
if err != nil {
return nil, err
}
return &DiscovClient{
conn: conn,
}, nil
}
func (c *DiscovClient) Next() (*grpc.ClientConn, bool) {
state := c.conn.GetState()
if state == connectivity.Ready {
return c.conn, true
} else {
return nil, false
}
}

@ -6,15 +6,19 @@ import (
"google.golang.org/grpc/resolver" "google.golang.org/grpc/resolver"
) )
type discovResolver struct { const discovScheme = "discov"
scheme string
etcd discov.EtcdConf type discovBuilder struct {
cc resolver.ClientConn etcd discov.EtcdConf
} }
func (r *discovResolver) Build(target resolver.Target, cc resolver.ClientConn, opts resolver.BuildOptions) ( func (b *discovBuilder) Scheme() string {
return discovScheme
}
func (b *discovBuilder) Build(target resolver.Target, cc resolver.ClientConn, opts resolver.BuildOptions) (
resolver.Resolver, error) { resolver.Resolver, error) {
sub, err := discov.NewSubscriber(r.etcd.Hosts, r.etcd.Key) sub, err := discov.NewSubscriber(b.etcd.Hosts, b.etcd.Key)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -27,27 +31,28 @@ func (r *discovResolver) Build(target resolver.Target, cc resolver.ClientConn, o
Addr: val, Addr: val,
}) })
} }
r.cc.UpdateState(resolver.State{ cc.UpdateState(resolver.State{
Addresses: addrs, Addresses: addrs,
}) })
}) })
return r, nil return &discovResolver{
cc: cc,
}, nil
} }
func (r *discovResolver) Close() { type discovResolver struct {
cc resolver.ClientConn
} }
func (r *discovResolver) ResolveNow(options resolver.ResolveNowOptions) { func (r *discovResolver) Close() {
} }
func (r *discovResolver) Scheme() string { func (r *discovResolver) ResolveNow(options resolver.ResolveNowOptions) {
return r.scheme
} }
func RegisterResolver(scheme string, etcd discov.EtcdConf) { func RegisterResolver(etcd discov.EtcdConf) {
resolver.Register(&discovResolver{ resolver.Register(&discovBuilder{
scheme: scheme, etcd: etcd,
etcd: etcd,
}) })
} }

@ -0,0 +1,17 @@
package main
import (
"zero/core/discov"
"zero/core/lang"
"zero/rpcx/internal"
)
func main() {
cli, err := internal.NewDiscovClient(discov.EtcdConf{
Hosts: []string{"localhost:2379"},
Key: "rpcx",
})
lang.Must(err)
cli.Next()
}
Loading…
Cancel
Save