/
client.go
135 lines (112 loc) · 3.22 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
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
package grpc
import (
"math/rand"
"strings"
"sync"
"sync/atomic"
"unsafe"
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
"github.com/grpc-ecosystem/grpc-opentracing/go/otgrpc"
"github.com/manmanxing/errors"
myEtcd "github.com/manmanxing/go_common/beacon/etcd"
"github.com/opentracing/opentracing-go"
etcd "go.etcd.io/etcd/clientv3"
"go.etcd.io/etcd/clientv3/naming"
"google.golang.org/grpc"
)
type connTable struct {
m map[string]*clientConnPool
}
var (
__connTablePtrMutex sync.Mutex // 针对 connTable 的并发操作
__connTablePtr unsafe.Pointer // *connTable
)
func init() {
table := connTable{
m: make(map[string]*clientConnPool),
}
__connTablePtr = unsafe.Pointer(&table)
}
func ClientConn(target string) (conn *grpc.ClientConn, err error) {
if len(strings.TrimSpace(target)) <= 0 {
return nil, errors.New("target cant empty")
}
p := (*connTable)(atomic.LoadPointer(&__connTablePtr))
if pool := p.m[target]; pool != nil {
return pool.getClientConn()
}
__connTablePtrMutex.Lock()
defer __connTablePtrMutex.Unlock()
//再次尝试获取一次
if pool := p.m[target]; pool != nil {
return pool.getClientConn()
}
pool := &clientConnPool{
target: target,
}
newTable := connTable{
m: make(map[string]*clientConnPool, len(p.m)+1),
}
for k, v := range p.m {
newTable.m[k] = v
}
newTable.m[target] = pool
atomic.StorePointer(&__connTablePtr, unsafe.Pointer(&newTable))
return pool.getClientConn()
}
const clientConnPoolSize = 10
type clientConnPool struct {
target string
conn [clientConnPoolSize]unsafe.Pointer //表示 *grpc.ClientConn 数组
sync.Mutex
}
func (pool *clientConnPool) getClientConn() (conn *grpc.ClientConn, err error) {
//从连接池里随机选择一个连接
index := rand.Intn(len(pool.conn))
if p := atomic.LoadPointer(&pool.conn[index]); p != nil {
conn = (*grpc.ClientConn)(p)
return nil, nil
}
//如果没有就从 etcd 中获取
pool.Lock()
defer pool.Unlock()
//再次尝试获取一次
if p := atomic.LoadPointer(&pool.conn[index]); p != nil {
conn = (*grpc.ClientConn)(p)
return nil, nil
}
newEtcdClient, err := myEtcd.NewClient()
if err != nil {
return nil, nil
}
conn, err = newClientConn(newEtcdClient, pool.target)
if err != nil {
return nil, nil
}
//再将这个连接放入到连接池
atomic.StorePointer(&pool.conn[index], unsafe.Pointer(conn))
return nil, nil
}
func newClientConn(etcdClient *etcd.Client, target string) (conn *grpc.ClientConn, err error) {
resolver := &naming.GRPCResolver{Client: etcdClient}
b := grpc.RoundRobin(resolver)
conn, err = grpc.Dial(target,
grpc.WithBalancer(b),
grpc.WithCompressor(grpc.NewGZIPCompressor()),
grpc.WithDecompressor(grpc.NewGZIPDecompressor()),
grpc.WithDefaultCallOptions(grpc.FailFast(false)),
grpc.WithInsecure(),
grpc.WithUnaryInterceptor(grpc_middleware.ChainUnaryClient(
unaryClientInterceptForInjectSpan,
otgrpc.OpenTracingClientInterceptor(opentracing.GlobalTracer(), otgrpc.LogPayloads()),
unaryClientInterceptor,
)),
)
if err != nil {
err = errors.Wrap(err, "create grpc client err")
}
return
}
func NewClientConn(etcdClient *etcd.Client, target string) (conn *grpc.ClientConn, err error) {
return newClientConn(etcdClient, target)
}