go-zero/zrpc/proxy.go

64 lines
1.4 KiB
Go
Raw Normal View History

2020-09-18 11:41:52 +08:00
package zrpc
2020-07-26 17:09:05 +08:00
import (
"context"
"sync"
"github.com/zeromicro/go-zero/core/syncx"
"github.com/zeromicro/go-zero/zrpc/internal"
"github.com/zeromicro/go-zero/zrpc/internal/auth"
2020-07-29 18:56:03 +08:00
"google.golang.org/grpc"
2020-07-26 17:09:05 +08:00
)
2021-03-01 23:52:44 +08:00
// A RpcProxy is a rpc proxy.
2020-07-26 17:09:05 +08:00
type RpcProxy struct {
backend string
clients map[string]Client
2020-07-29 18:06:57 +08:00
options []internal.ClientOption
sharedCalls syncx.SingleFlight
2020-07-26 17:09:05 +08:00
lock sync.Mutex
}
2021-03-01 23:52:44 +08:00
// NewProxy returns a RpcProxy.
2020-08-26 14:19:16 +08:00
func NewProxy(backend string, opts ...internal.ClientOption) *RpcProxy {
2020-07-26 17:09:05 +08:00
return &RpcProxy{
backend: backend,
clients: make(map[string]Client),
2020-07-26 17:09:05 +08:00
options: opts,
sharedCalls: syncx.NewSingleFlight(),
2020-07-26 17:09:05 +08:00
}
}
2021-03-01 23:52:44 +08:00
// TakeConn returns a grpc.ClientConn.
2020-07-26 17:09:05 +08:00
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
}
2020-08-18 18:36:44 +08:00
opts := append(p.options, WithDialOption(grpc.WithPerRPCCredentials(&auth.Credential{
App: cred.App,
Token: cred.Token,
})))
client, err := NewClientWithTarget(p.backend, opts...)
2020-07-26 17:09:05 +08:00
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
}
2020-08-26 14:19:16 +08:00
return val.(Client).Conn(), nil
2020-07-26 17:09:05 +08:00
}