-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathconn.go
53 lines (43 loc) · 1.21 KB
/
conn.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
package dgrpc
import (
"context"
"fmt"
"go.uber.org/atomic"
"go.uber.org/multierr"
"google.golang.org/grpc"
)
type RoundRobinConnPool struct {
conns []*grpc.ClientConn
idx *atomic.Uint32
}
func NewRoundRobinConnPool(conns []*grpc.ClientConn) *RoundRobinConnPool {
return &RoundRobinConnPool{
conns: conns,
idx: atomic.NewUint32(0),
}
}
func (p *RoundRobinConnPool) Num() int {
return len(p.conns)
}
func (p *RoundRobinConnPool) Conn() *grpc.ClientConn {
newIdx := p.idx.Inc()
return p.conns[newIdx%uint32(len(p.conns))]
}
func (p *RoundRobinConnPool) Close() error {
var errs error
for _, conn := range p.conns {
if err := conn.Close(); err != nil {
errs = multierr.Append(errs, err)
}
}
if errs != nil {
return fmt.Errorf("failed to close all connections: %w", errs)
}
return errs
}
func (p *RoundRobinConnPool) Invoke(ctx context.Context, method string, args interface{}, reply interface{}, opts ...grpc.CallOption) error {
return p.Conn().Invoke(ctx, method, args, reply, opts...)
}
func (p *RoundRobinConnPool) NewStream(ctx context.Context, desc *grpc.StreamDesc, method string, opts ...grpc.CallOption) (grpc.ClientStream, error) {
return p.Conn().NewStream(ctx, desc, method, opts...)
}