/
client_pool.go
165 lines (136 loc) · 3.81 KB
/
client_pool.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Copyright 2023 StreamNative, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package common
import (
"context"
"crypto/tls"
"io"
"log/slog"
"sync"
"time"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
grpcprometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
"github.com/pkg/errors"
"google.golang.org/grpc"
"google.golang.org/grpc/health/grpc_health_v1"
"google.golang.org/grpc/peer"
"github.com/streamnative/oxia/proto"
)
const DefaultRpcTimeout = 30 * time.Second
type ClientPool interface {
io.Closer
GetClientRpc(target string) (proto.OxiaClientClient, error)
GetHealthRpc(target string) (grpc_health_v1.HealthClient, error)
GetCoordinationRpc(target string) (proto.OxiaCoordinationClient, error)
GetReplicationRpc(target string) (proto.OxiaLogReplicationClient, error)
}
type clientPool struct {
sync.RWMutex
connections map[string]grpc.ClientConnInterface
tls *tls.Config
log *slog.Logger
}
func NewClientPool(tlsConf *tls.Config) ClientPool {
return &clientPool{
connections: make(map[string]grpc.ClientConnInterface),
tls: tlsConf,
log: slog.With(
slog.String("component", "client-pool"),
),
}
}
func (cp *clientPool) Close() error {
cp.Lock()
defer cp.Unlock()
for target, cnx := range cp.connections {
err := cnx.(*grpc.ClientConn).Close()
if err != nil {
cp.log.Warn(
"Failed to close GRPC connection",
slog.String("server_address", target),
slog.Any("error", err),
)
}
}
return nil
}
func (cp *clientPool) GetHealthRpc(target string) (grpc_health_v1.HealthClient, error) {
cnx, err := cp.getConnection(target)
if err != nil {
return nil, err
}
return grpc_health_v1.NewHealthClient(cnx), nil
}
func (cp *clientPool) GetClientRpc(target string) (proto.OxiaClientClient, error) {
cnx, err := cp.getConnection(target)
if err != nil {
return nil, err
}
return &loggingClientRpc{target, proto.NewOxiaClientClient(cnx)}, nil
}
func (cp *clientPool) GetCoordinationRpc(target string) (proto.OxiaCoordinationClient, error) {
cnx, err := cp.getConnection(target)
if err != nil {
return nil, err
}
return proto.NewOxiaCoordinationClient(cnx), nil
}
func (cp *clientPool) GetReplicationRpc(target string) (proto.OxiaLogReplicationClient, error) {
cnx, err := cp.getConnection(target)
if err != nil {
return nil, err
}
return proto.NewOxiaLogReplicationClient(cnx), nil
}
func (cp *clientPool) getConnection(target string) (grpc.ClientConnInterface, error) {
cp.RLock()
cnx, ok := cp.connections[target]
cp.RUnlock()
if ok {
return cnx, nil
}
cp.Lock()
defer cp.Unlock()
cnx, ok = cp.connections[target]
if ok {
return cnx, nil
}
cp.log.Debug(
"Creating new GRPC connection",
slog.String("server_address", target),
)
// tls configure
tcs := insecure.NewCredentials()
if cp.tls != nil {
tcs = credentials.NewTLS(cp.tls)
}
cnx, err := grpc.Dial(target,
grpc.WithTransportCredentials(tcs),
grpc.WithStreamInterceptor(grpcprometheus.StreamClientInterceptor),
grpc.WithUnaryInterceptor(grpcprometheus.UnaryClientInterceptor),
)
if err != nil {
return nil, errors.Wrapf(err, "error connecting to %s", target)
}
cp.connections[target] = cnx
return cnx, nil
}
func GetPeer(ctx context.Context) string {
p, ok := peer.FromContext(ctx)
if !ok {
return ""
}
return p.Addr.String()
}