mv balancer

pull/961/head
longXboy 4 years ago
parent a24bcdff45
commit cc0214d78b
  1. 2
      transport/http/balancer/balancer.go
  2. 8
      transport/http/balancer/random/random.go
  3. 8
      transport/http/client.go

@ -14,5 +14,5 @@ type DoneInfo struct {
// Balancer is node pick balancer // Balancer is node pick balancer
type Balancer interface { type Balancer interface {
Pick(ctx context.Context, pathPattern string, nodes []*registry.ServiceInstance) (node *registry.ServiceInstance, done func(DoneInfo), err error) Pick(ctx context.Context, pathPattern string, nodes []*registry.ServiceInstance) (node *registry.ServiceInstance, done func(context.Context, DoneInfo), err error)
} }

@ -5,8 +5,8 @@ import (
"fmt" "fmt"
"math/rand" "math/rand"
"github.com/go-kratos/kratos/v2/internal/balancer"
"github.com/go-kratos/kratos/v2/registry" "github.com/go-kratos/kratos/v2/registry"
"github.com/go-kratos/kratos/v2/transport/http/balancer"
) )
var _ balancer.Balancer = &Balancer{} var _ balancer.Balancer = &Balancer{}
@ -18,12 +18,12 @@ func New() *Balancer {
return &Balancer{} return &Balancer{}
} }
func (b *Balancer) Pick(ctx context.Context, pathPattern string, nodes []*registry.ServiceInstance) (node *registry.ServiceInstance, done func(balancer.DoneInfo), err error) { func (b *Balancer) Pick(ctx context.Context, pathPattern string, nodes []*registry.ServiceInstance) (node *registry.ServiceInstance, done func(context.Context, balancer.DoneInfo), err error) {
if len(nodes) == 0 { if len(nodes) == 0 {
return nil, nil, fmt.Errorf("no instances avaiable") return nil, nil, fmt.Errorf("no instances avaiable")
} else if len(nodes) == 1 { } else if len(nodes) == 1 {
return nodes[0], func(di balancer.DoneInfo) {}, nil return nodes[0], func(context.Context, balancer.DoneInfo) {}, nil
} }
idx := rand.Intn(len(nodes)) idx := rand.Intn(len(nodes))
return nodes[idx], func(di balancer.DoneInfo) {}, nil return nodes[idx], func(context.Context, balancer.DoneInfo) {}, nil
} }

@ -12,12 +12,12 @@ import (
"github.com/go-kratos/kratos/v2/encoding" "github.com/go-kratos/kratos/v2/encoding"
"github.com/go-kratos/kratos/v2/errors" "github.com/go-kratos/kratos/v2/errors"
"github.com/go-kratos/kratos/v2/internal/balancer"
"github.com/go-kratos/kratos/v2/internal/balancer/random"
"github.com/go-kratos/kratos/v2/internal/httputil" "github.com/go-kratos/kratos/v2/internal/httputil"
"github.com/go-kratos/kratos/v2/middleware" "github.com/go-kratos/kratos/v2/middleware"
"github.com/go-kratos/kratos/v2/registry" "github.com/go-kratos/kratos/v2/registry"
"github.com/go-kratos/kratos/v2/transport" "github.com/go-kratos/kratos/v2/transport"
"github.com/go-kratos/kratos/v2/transport/http/balancer"
"github.com/go-kratos/kratos/v2/transport/http/balancer/random"
) )
// Client is http client // Client is http client
@ -255,7 +255,7 @@ func (client *Client) Invoke(ctx context.Context, path string, args interface{},
func (client *Client) invoke(ctx context.Context, req *http.Request, args interface{}, reply interface{}, c callInfo) error { func (client *Client) invoke(ctx context.Context, req *http.Request, args interface{}, reply interface{}, c callInfo) error {
h := func(ctx context.Context, in interface{}) (interface{}, error) { h := func(ctx context.Context, in interface{}) (interface{}, error) {
var done func(balancer.DoneInfo) var done func(context.Context, balancer.DoneInfo)
if client.r != nil { if client.r != nil {
nodes := client.r.fetch(ctx) nodes := client.r.fetch(ctx)
if len(nodes) == 0 { if len(nodes) == 0 {
@ -276,7 +276,7 @@ func (client *Client) invoke(ctx context.Context, req *http.Request, args interf
} }
res, err := client.do(ctx, req, c) res, err := client.do(ctx, req, c)
if done != nil { if done != nil {
done(balancer.DoneInfo{Err: err}) done(ctx, balancer.DoneInfo{Err: err})
} }
if err != nil { if err != nil {
return nil, err return nil, err

Loading…
Cancel
Save