gocron/internal/modules/rpc/grpcpool/grpc_pool.go

120 lines
2.3 KiB
Go
Raw Normal View History

2017-05-28 15:13:22 +00:00
package grpcpool
import (
2019-06-02 03:37:59 +00:00
"context"
2018-01-30 11:26:04 +00:00
"strings"
"sync"
"time"
2018-03-25 05:12:12 +00:00
"github.com/ouqiang/gocron/internal/modules/app"
"github.com/ouqiang/gocron/internal/modules/rpc/auth"
2019-06-02 03:37:59 +00:00
"github.com/ouqiang/gocron/internal/modules/rpc/proto"
2017-09-16 09:58:33 +00:00
"google.golang.org/grpc"
2019-06-02 03:37:59 +00:00
"google.golang.org/grpc/keepalive"
2017-05-28 15:13:22 +00:00
)
2019-06-02 03:37:59 +00:00
const (
backOffMaxDelay = 3 * time.Second
dialTimeout = 2 * time.Second
2017-05-28 15:13:22 +00:00
)
var (
2019-06-02 03:37:59 +00:00
Pool = &GRPCPool{
conns: make(map[string]*Client),
}
2017-05-28 15:13:22 +00:00
2019-06-02 03:37:59 +00:00
keepAliveParams = keepalive.ClientParameters{
Time: 20 * time.Second,
Timeout: 3 * time.Second,
PermitWithoutStream: true,
2017-09-16 09:58:33 +00:00
}
2019-06-02 03:37:59 +00:00
)
type Client struct {
conn *grpc.ClientConn
rpcClient rpc.TaskClient
2017-05-28 15:13:22 +00:00
}
type GRPCPool struct {
2017-09-16 09:58:33 +00:00
// map key格式 ip:port
2019-06-02 03:37:59 +00:00
conns map[string]*Client
mu sync.RWMutex
2017-05-28 15:13:22 +00:00
}
2019-06-02 03:37:59 +00:00
func (p *GRPCPool) Get(addr string) (rpc.TaskClient, error) {
p.mu.RLock()
client, ok := p.conns[addr]
p.mu.RUnlock()
if ok {
return client.rpcClient, nil
2017-09-16 09:58:33 +00:00
}
2019-06-02 03:37:59 +00:00
client, err := p.factory(addr)
2017-09-16 09:58:33 +00:00
if err != nil {
return nil, err
}
2019-06-02 03:37:59 +00:00
p.conns[addr] = client
2017-09-16 09:58:33 +00:00
2019-06-02 03:37:59 +00:00
return client.rpcClient, nil
2017-05-28 15:13:22 +00:00
}
2019-06-02 03:37:59 +00:00
// 释放连接
2017-05-28 15:13:22 +00:00
func (p *GRPCPool) Release(addr string) {
2019-06-02 03:37:59 +00:00
p.mu.Lock()
defer p.mu.Unlock()
client, ok := p.conns[addr]
2017-09-16 09:58:33 +00:00
if !ok {
return
}
delete(p.conns, addr)
2019-06-02 03:37:59 +00:00
client.conn.Close()
2017-05-28 15:13:22 +00:00
}
2019-06-02 03:37:59 +00:00
// 创建连接
func (p *GRPCPool) factory(addr string) (*Client, error) {
p.mu.Lock()
defer p.mu.Unlock()
2017-05-29 09:05:21 +00:00
2019-06-02 03:37:59 +00:00
client, ok := p.conns[addr]
2017-09-16 09:58:33 +00:00
if ok {
2019-06-02 03:37:59 +00:00
return client, nil
2017-09-16 09:58:33 +00:00
}
2019-06-02 03:37:59 +00:00
opts := []grpc.DialOption{
grpc.WithKeepaliveParams(keepAliveParams),
grpc.WithBackoffMaxDelay(backOffMaxDelay),
2017-09-16 09:58:33 +00:00
}
2019-06-02 03:37:59 +00:00
if !app.Setting.EnableTLS {
opts = append(opts, grpc.WithInsecure())
} else {
server := strings.Split(addr, ":")
certificate := auth.Certificate{
CAFile: app.Setting.CAFile,
CertFile: app.Setting.CertFile,
KeyFile: app.Setting.KeyFile,
ServerName: server[0],
}
transportCreds, err := certificate.GetTransportCredsForClient()
if err != nil {
return nil, err
}
opts = append(opts, grpc.WithTransportCredentials(transportCreds))
}
ctx, cancel := context.WithTimeout(context.Background(), dialTimeout)
defer cancel()
conn, err := grpc.DialContext(ctx, addr, opts...)
2017-09-16 09:58:33 +00:00
if err != nil {
2019-06-02 03:37:59 +00:00
return nil, err
2017-09-16 09:58:33 +00:00
}
2019-06-02 03:37:59 +00:00
client = &Client{
conn: conn,
rpcClient: rpc.NewTaskClient(conn),
}
2017-09-16 09:58:33 +00:00
2019-06-02 03:37:59 +00:00
return client, nil
2017-09-16 09:58:33 +00:00
}