2017-05-28 15:13:22 +00:00
|
|
|
package grpcpool
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/silenceper/pool"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
"errors"
|
2017-09-06 03:27:54 +00:00
|
|
|
"google.golang.org/grpc/credentials"
|
2017-09-06 14:46:56 +00:00
|
|
|
"golang.org/x/net/context"
|
2017-09-06 03:27:54 +00:00
|
|
|
"strings"
|
2017-05-28 15:13:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
Pool GRPCPool
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrInvalidConn = errors.New("invalid connection")
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
Pool = GRPCPool{
|
|
|
|
make(map[string]pool.Pool),
|
|
|
|
sync.RWMutex{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type GRPCPool struct {
|
|
|
|
// map key格式 ip:port
|
|
|
|
conns map[string]pool.Pool
|
|
|
|
sync.RWMutex
|
|
|
|
}
|
|
|
|
|
2017-09-06 14:46:56 +00:00
|
|
|
func (p *GRPCPool) Get(addr, certFile, token string) (*grpc.ClientConn, error) {
|
2017-05-28 15:13:22 +00:00
|
|
|
p.RLock()
|
|
|
|
pool, ok := p.conns[addr]
|
2017-06-26 10:07:56 +00:00
|
|
|
p.RUnlock()
|
2017-05-28 15:13:22 +00:00
|
|
|
if !ok {
|
2017-09-06 14:46:56 +00:00
|
|
|
err := p.newCommonPool(addr, certFile, token)
|
2017-05-28 15:13:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-26 10:07:56 +00:00
|
|
|
p.RLock()
|
2017-05-28 15:13:22 +00:00
|
|
|
pool = p.conns[addr]
|
2017-06-26 10:07:56 +00:00
|
|
|
p.RUnlock()
|
2017-05-28 15:13:22 +00:00
|
|
|
conn, err := pool.Get()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return conn.(*grpc.ClientConn), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *GRPCPool) Put(addr string, conn *grpc.ClientConn) error {
|
|
|
|
p.RLock()
|
|
|
|
defer p.RUnlock()
|
|
|
|
pool, ok := p.conns[addr]
|
|
|
|
if ok {
|
|
|
|
return pool.Put(conn)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ErrInvalidConn
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 释放连接池
|
|
|
|
func (p *GRPCPool) Release(addr string) {
|
|
|
|
p.Lock()
|
|
|
|
defer p.Unlock()
|
|
|
|
pool, ok := p.conns[addr]
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
pool.Release()
|
|
|
|
delete(p.conns, addr)
|
|
|
|
}
|
|
|
|
|
2017-05-29 09:05:21 +00:00
|
|
|
// 释放所有连接池
|
|
|
|
func (p *GRPCPool) ReleaseAll() {
|
|
|
|
p.Lock()
|
|
|
|
defer p.Unlock()
|
|
|
|
for _, pool := range(p.conns) {
|
|
|
|
pool.Release()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-28 15:13:22 +00:00
|
|
|
// 初始化底层连接池
|
2017-09-06 14:46:56 +00:00
|
|
|
func (p *GRPCPool) newCommonPool(addr, certFile, token string) (error) {
|
2017-05-28 15:13:22 +00:00
|
|
|
p.Lock()
|
|
|
|
defer p.Unlock()
|
|
|
|
commonPool, ok := p.conns[addr]
|
|
|
|
if ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
poolConfig := &pool.PoolConfig{
|
|
|
|
InitialCap: 1,
|
|
|
|
MaxCap: 30,
|
|
|
|
Factory: func() (interface{}, error) {
|
2017-09-06 03:27:54 +00:00
|
|
|
if certFile == "" {
|
|
|
|
return grpc.Dial(addr, grpc.WithInsecure())
|
|
|
|
}
|
|
|
|
|
|
|
|
server := strings.Split(addr, ":")
|
|
|
|
creds, err := credentials.NewClientTLSFromFile(certFile, server[0])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-09-06 14:46:56 +00:00
|
|
|
customCredential := &CustomCredential{Token: token}
|
|
|
|
|
|
|
|
|
|
|
|
return grpc.Dial(addr,
|
|
|
|
grpc.WithTransportCredentials(creds),
|
|
|
|
grpc.WithPerRPCCredentials(customCredential),
|
|
|
|
)
|
2017-05-28 15:13:22 +00:00
|
|
|
},
|
|
|
|
Close: func(v interface{}) error {
|
|
|
|
conn, ok := v.(*grpc.ClientConn)
|
|
|
|
if ok && conn != nil {
|
|
|
|
return conn.Close()
|
|
|
|
}
|
|
|
|
return ErrInvalidConn
|
|
|
|
},
|
2017-05-30 01:46:35 +00:00
|
|
|
IdleTimeout: 3 * time.Minute,
|
2017-05-28 15:13:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
commonPool, err := pool.NewChannelPool(poolConfig)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
p.conns[addr] = commonPool
|
|
|
|
|
|
|
|
return nil
|
2017-09-06 14:46:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type CustomCredential struct
|
|
|
|
{
|
|
|
|
Token string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c CustomCredential) GetRequestMetadata(ctx context.Context, uri ...string) (map[string]string, error) {
|
|
|
|
return map[string]string{
|
|
|
|
"token": c.Token,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c CustomCredential) RequireTransportSecurity() bool {
|
|
|
|
return true
|
2017-05-28 15:13:22 +00:00
|
|
|
}
|