/
direct.go
78 lines (65 loc) · 1.5 KB
/
direct.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
75
76
77
78
package direct
import (
"context"
"fmt"
"strings"
"github.com/go-kratos/kratos/pkg/conf/env"
"github.com/go-kratos/kratos/pkg/naming"
"github.com/go-kratos/kratos/pkg/net/rpc/warden/resolver"
)
const (
// Name is the name of direct resolver
Name = "direct"
)
var _ naming.Resolver = &Direct{}
// New return Direct
func New() *Direct {
return &Direct{}
}
// Build build direct.
func Build(id string) *Direct {
return &Direct{id: id}
}
// Direct is a resolver for conneting endpoints directly.
// example format: direct://default/192.168.1.1:8080,192.168.1.2:8081
type Direct struct {
id string
}
// Build direct build.
func (d *Direct) Build(id string, opt ...naming.BuildOpt) naming.Resolver {
return &Direct{id: id}
}
// Scheme return the Scheme of Direct
func (d *Direct) Scheme() string {
return Name
}
// Watch a tree.
func (d *Direct) Watch() <-chan struct{} {
ch := make(chan struct{}, 1)
ch <- struct{}{}
return ch
}
// Unwatch a tree.
func (d *Direct) Unwatch(id string) {
}
//Fetch fetch instances.
func (d *Direct) Fetch(ctx context.Context) (res *naming.InstancesInfo, found bool) {
var ins []*naming.Instance
addrs := strings.Split(d.id, ",")
for _, addr := range addrs {
ins = append(ins, &naming.Instance{
Addrs: []string{fmt.Sprintf("%s://%s", resolver.Scheme, addr)},
})
}
if len(ins) > 0 {
found = true
}
res = &naming.InstancesInfo{
Instances: map[string][]*naming.Instance{env.Zone: ins},
}
return
}
//Close close Direct
func (d *Direct) Close() error {
return nil
}