-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsrad.go
More file actions
48 lines (38 loc) · 959 Bytes
/
Copy pathsrad.go
File metadata and controls
48 lines (38 loc) · 959 Bytes
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
package srad
import (
"context"
"sync"
"google.golang.org/grpc"
)
var (
p = make(map[string]*pool)
locker sync.Mutex
)
func Register(ctx context.Context, scheme, service, host string, port int, weight int64, endpoints []string) (*etcdRegistry, error) {
return register(ctx, scheme, service, host, port, endpoints, 10, weight)
}
func Discover(scheme, service string, endpoints []string) (*grpc.ClientConn, error) {
prefix := joinPathPrefix(scheme, service)
curPool, ok := p[prefix]
if !ok {
err := genPool(scheme, service, endpoints)
if err != nil {
return nil, err
}
curPool = p[prefix]
}
return curPool.Get()
}
func genPool(scheme, service string, endpoints []string) error {
locker.Lock()
defer locker.Unlock()
prefix := joinPathPrefix(scheme, service)
if _, ok := p[prefix]; !ok {
onePool, err := discoverPool(scheme, service, endpoints)
if err != nil {
return err
}
p[prefix] = onePool
}
return nil
}