-
Notifications
You must be signed in to change notification settings - Fork 3
/
direct.go
60 lines (49 loc) · 1.03 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
package grpcx
import (
"sync"
"golang.org/x/net/context"
"google.golang.org/grpc/naming"
)
type localResolver struct {
sync.RWMutex
firstCalled bool
index int
addrs []string
ctx context.Context
cancel context.CancelFunc
}
func newLocalResolver(addrs ...string) naming.Resolver {
ctx, cancel := context.WithCancel(context.Background())
return &localResolver{
addrs: addrs,
ctx: ctx,
cancel: cancel,
}
}
func (lr *localResolver) Resolve(target string) (naming.Watcher, error) {
return lr, nil
}
func (lr *localResolver) Next() ([]*naming.Update, error) {
lr.Lock()
defer lr.Unlock()
if !lr.firstCalled {
return lr.firstNext()
}
// block
<-lr.ctx.Done()
return nil, lr.ctx.Err()
}
func (lr *localResolver) Close() {
lr.cancel()
}
func (lr *localResolver) firstNext() ([]*naming.Update, error) {
var values []*naming.Update
for _, addr := range lr.addrs {
values = append(values, &naming.Update{
Op: naming.Add,
Addr: addr,
})
}
lr.firstCalled = true
return values, nil
}