forked from zeromicro/go-zero
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy.go
60 lines (52 loc) · 1.28 KB
/
proxy.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
package zrpc
import (
"context"
"sync"
"github.com/tal-tech/go-zero/core/syncx"
"github.com/tal-tech/go-zero/zrpc/internal"
"github.com/tal-tech/go-zero/zrpc/internal/auth"
"google.golang.org/grpc"
)
type RpcProxy struct {
backend string
clients map[string]Client
options []internal.ClientOption
sharedCalls syncx.SharedCalls
lock sync.Mutex
}
func NewProxy(backend string, opts ...internal.ClientOption) *RpcProxy {
return &RpcProxy{
backend: backend,
clients: make(map[string]Client),
options: opts,
sharedCalls: syncx.NewSharedCalls(),
}
}
func (p *RpcProxy) TakeConn(ctx context.Context) (*grpc.ClientConn, error) {
cred := auth.ParseCredential(ctx)
key := cred.App + "/" + cred.Token
val, err := p.sharedCalls.Do(key, func() (interface{}, error) {
p.lock.Lock()
client, ok := p.clients[key]
p.lock.Unlock()
if ok {
return client, nil
}
opts := append(p.options, WithDialOption(grpc.WithPerRPCCredentials(&auth.Credential{
App: cred.App,
Token: cred.Token,
})))
client, err := NewClientWithTarget(p.backend, opts...)
if err != nil {
return nil, err
}
p.lock.Lock()
p.clients[key] = client
p.lock.Unlock()
return client, nil
})
if err != nil {
return nil, err
}
return val.(Client).Conn(), nil
}