You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
kratos/transport/http/balancer/random/random.go

31 lines
719 B

package random
import (
"context"
"fmt"
"math/rand"
"github.com/go-kratos/kratos/v2/registry"
4 years ago
"github.com/go-kratos/kratos/v2/transport/http/balancer"
)
var _ balancer.Balancer = &Balancer{}
type Balancer struct {
}
func New() *Balancer {
return &Balancer{}
}
func (b *Balancer) Pick(ctx context.Context, nodes []*registry.ServiceInstance) (node *registry.ServiceInstance, done func(context.Context, balancer.DoneInfo), err error) {
if len(nodes) == 0 {
return nil, nil, fmt.Errorf("no instances avaiable")
}
if len(nodes) == 1 {
4 years ago
return nodes[0], func(context.Context, balancer.DoneInfo) {}, nil
}
idx := rand.Intn(len(nodes))
4 years ago
return nodes[idx], func(context.Context, balancer.DoneInfo) {}, nil
}