-
Notifications
You must be signed in to change notification settings - Fork 13
/
client.go
44 lines (38 loc) · 1.09 KB
/
client.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
package grpcclient
import (
"sync"
"google.golang.org/grpc"
)
var (
cconnsMu sync.RWMutex
cconns = map[string]*grpc.ClientConn{}
)
// Conn returns the global grpc.ClientConn for the given service. If the
// connection has not yet been initialized, it will panic.
func Conn(service string) *grpc.ClientConn {
cconnsMu.RLock()
defer cconnsMu.RUnlock()
c, ok := cconns[service]
if !ok {
panic("gRPC client connection not initialized")
}
return c
}
// RegisterConnection registers the given gRPC connection for usage under the
// specified service name.
func RegisterConnection(service string, cconn *grpc.ClientConn) {
cconnsMu.Lock()
defer cconnsMu.Unlock()
cconns[service] = cconn
}
// DeregisterConnection deregisters the gRPC connection for the specified
// service name. The server is not stopped before it's deregistered. If no
// connection is registered for the service name, it will panic.
func DeregisterConnection(service string) {
cconnsMu.Lock()
defer cconnsMu.Unlock()
if _, ok := cconns[service]; !ok {
panic("gRPC client connection not initialized")
}
delete(cconns, service)
}