-
Notifications
You must be signed in to change notification settings - Fork 51
/
client.go
118 lines (98 loc) · 3.17 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
package rpc
import (
"context"
"sync"
"time"
"github.com/ibinarytree/koala/loadbalance"
"github.com/ibinarytree/koala/logs"
"github.com/ibinarytree/koala/meta"
"github.com/ibinarytree/koala/middleware"
"github.com/ibinarytree/koala/registry"
"golang.org/x/time/rate"
)
var initRegistryOnce sync.Once
var globalRegister registry.Registry
type KoalaClient struct {
opts *RpcOptions
register registry.Registry
limiter *rate.Limiter
balance loadbalance.LoadBalance
}
func NewKoalaClient(serviceName string, optfunc ...RpcOptionFunc) *KoalaClient {
client := &KoalaClient{
opts: &RpcOptions{
ConnTimeout: DefaultConnTimeout,
WriteTimeout: DefaultWriteTimeout,
ReadTimeout: DefaultReadTimeout,
ServiceName: serviceName,
RegisterName: "etcd",
RegisterAddr: "127.0.0.1:2379",
RegisterPath: "/ibinarytree/koala/service/",
TraceReportAddr: "http://60.205.218.189:9411/api/v1/spans",
TraceSampleType: "const",
TraceSampleRate: 1,
ClientServiceName: "default",
},
balance: loadbalance.NewRandomBalance(),
}
for _, opt := range optfunc {
opt(client.opts)
}
initRegistryOnce.Do(func() {
ctx := context.TODO()
var err error
globalRegister, err = registry.InitRegistry(ctx,
client.opts.RegisterName,
registry.WithAddrs([]string{client.opts.RegisterAddr}),
registry.WithTimeout(time.Second),
registry.WithRegistryPath(client.opts.RegisterPath),
registry.WithHeartBeat(10),
)
if err != nil {
logs.Error(ctx, "init registry failed, err:%v", err)
return
}
})
if client.opts.MaxLimitQps > 0 {
client.limiter = rate.NewLimiter(rate.Limit(client.opts.MaxLimitQps),
client.opts.MaxLimitQps)
}
middleware.InitTrace(client.opts.ClientServiceName, client.opts.TraceReportAddr, client.opts.TraceSampleType,
client.opts.TraceSampleRate)
client.register = globalRegister
return client
}
func (k *KoalaClient) getCaller(ctx context.Context) string {
serverMeta := meta.GetServerMeta(ctx)
if serverMeta == nil {
return ""
}
return serverMeta.ServiceName
}
func (k *KoalaClient) buildMiddleware(handle middleware.MiddlewareFunc) middleware.MiddlewareFunc {
var mids []middleware.Middleware
mids = append(mids, middleware.PrepareMiddleware)
mids = append(mids, middleware.RpcLogMiddleware)
mids = append(mids, middleware.TraceRpcMiddleware)
mids = append(mids, middleware.PrometheusRpcMiddleware)
if k.limiter != nil {
mids = append(mids, middleware.NewRateLimitMiddleware(k.limiter))
}
mids = append(mids, middleware.HystrixMiddleware)
mids = append(mids, middleware.NewDiscoveryMiddleware(k.register))
mids = append(mids, middleware.NewLoadBalanceMiddleware(k.balance))
mids = append(mids, middleware.ShortConnectMiddleware)
m := middleware.Chain(mids[0], mids[1:]...)
return m(handle)
}
func (k *KoalaClient) Call(ctx context.Context, method string, r interface{}, handle middleware.MiddlewareFunc) (resp interface{}, err error) {
//构建中间件
caller := k.getCaller(ctx)
ctx = meta.InitRpcMeta(ctx, k.opts.ServiceName, method, caller)
middlewareFunc := k.buildMiddleware(handle)
resp, err = middlewareFunc(ctx, r)
if err != nil {
return nil, err
}
return resp, err
}