-
-
Notifications
You must be signed in to change notification settings - Fork 4k
/
selector.go
74 lines (57 loc) · 1.68 KB
/
selector.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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 is apply all nodes when any changes happen
Apply(nodes []Node)
}
// Builder build selector
type Builder interface {
Build() Selector
}
// Node is node interface.
type Node interface {
// Scheme is service node scheme
Scheme() string
// 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
ReplyMD ReplyMD
// 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
}
// ReplyMD is Reply Metadata.
type ReplyMD interface {
Get(key string) string
}
// DoneFunc is callback function when RPC invoke done.
type DoneFunc func(ctx context.Context, di DoneInfo)