forked from olivere/grpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
static.go
45 lines (38 loc) · 1.15 KB
/
static.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
// Copyright 2016-present Oliver Eilhard. All rights reserved.
// Use of this source code is governed by a MIT-license.
// See http://olivere.mit-license.org/license.txt for details.
package static
import (
"google.golang.org/grpc/naming"
)
// Resolver implements a gRPC resolver/watcher that simply returns
// a list of addresses, then blocks.
type Resolver struct {
addr []*naming.Update
}
// NewResolver initializes and returns a new Resolver.
func NewResolver(addr ...string) *Resolver {
r := &Resolver{}
for _, a := range addr {
r.addr = append(r.addr, &naming.Update{Op: naming.Add, Addr: a})
}
return r
}
// Resolve creates a watcher for target. The watcher interface is implemented
// by Resolver as well, see Next and Close.
func (r *Resolver) Resolve(target string) (naming.Watcher, error) {
return r, nil
}
// Next returns the list of addresses once, then blocks on consecutive calls.
func (r *Resolver) Next() ([]*naming.Update, error) {
if r.addr != nil {
updates := r.addr
r.addr = nil
return updates, nil
}
infinite := make(chan struct{})
<-infinite
return nil, nil
}
// Close is a no-op for a Resolver.
func (r *Resolver) Close() {}