1
0
mirror of https://github.com/go-kratos/kratos.git synced 2025-03-27 21:49:08 +02:00
kratos/selector/selector.go
longxboy 20f0a07d36
Feat: add load balancer (#1437)
* add balancer
* add p2c balancer
* add http client selector filter

Co-authored-by: yuemoxi <99347745@qq.com>
Co-authored-by: chenzhihui <zhihui_chen@foxmail.com>
2021-09-12 15:14:21 +08:00

67 lines
1.6 KiB
Go

package selector
import (
"context"
"github.com/go-kratos/kratos/v2/errors"
)
// ErrNoAvailable is no available node.
var ErrNoAvailable = errors.ServiceUnavailable("no_available_node", "")
// Selector is node pick balancer.
type Selector interface {
Rebalancer
// Select nodes
// if err == nil, selected and done must not be empty.
Select(ctx context.Context, opts ...SelectOption) (selected Node, done DoneFunc, err error)
}
// Rebalancer is nodes rebalancer.
type Rebalancer interface {
// apply all nodes when any changes happen
Apply(nodes []Node)
}
// Node is node interface.
type Node interface {
// Address is the unique address under the same service
Address() string
// ServiceName is service name
ServiceName() string
// InitialWeight is the initial value of scheduling weight
// if not set return nil
InitialWeight() *int64
// Version is service node version
Version() string
// Metadata is the kv pair metadata associated with the service instance.
// version,namespace,region,protocol etc..
Metadata() map[string]string
}
// DoneInfo is callback info when RPC invoke done.
type DoneInfo struct {
// Response Error
Err error
// Response Metadata
ReplyMeta ReplyMeta
// BytesSent indicates if any bytes have been sent to the server.
BytesSent bool
// BytesReceived indicates if any byte has been received from the server.
BytesReceived bool
}
// ReplyMeta is Reply Metadata.
type ReplyMeta interface {
Get(key string) string
}
// DoneFunc is callback function when RPC invoke done.
type DoneFunc func(ctx context.Context, di DoneInfo)