forked from ouqiang/gocron
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
138 additions
and
412 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,140 +1,119 @@ | ||
package grpcpool | ||
|
||
import ( | ||
"errors" | ||
"context" | ||
"strings" | ||
"sync" | ||
"time" | ||
|
||
"github.com/ouqiang/gocron/internal/modules/app" | ||
"github.com/ouqiang/gocron/internal/modules/rpc/auth" | ||
"github.com/silenceper/pool" | ||
"github.com/ouqiang/gocron/internal/modules/rpc/proto" | ||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/keepalive" | ||
) | ||
|
||
var ( | ||
Pool GRPCPool | ||
const ( | ||
backOffMaxDelay = 3 * time.Second | ||
dialTimeout = 2 * time.Second | ||
) | ||
|
||
var ( | ||
ErrInvalidConn = errors.New("invalid connection") | ||
) | ||
Pool = &GRPCPool{ | ||
conns: make(map[string]*Client), | ||
} | ||
|
||
func init() { | ||
Pool = GRPCPool{ | ||
make(map[string]pool.Pool), | ||
sync.RWMutex{}, | ||
keepAliveParams = keepalive.ClientParameters{ | ||
Time: 20 * time.Second, | ||
Timeout: 3 * time.Second, | ||
PermitWithoutStream: true, | ||
} | ||
) | ||
|
||
type Client struct { | ||
conn *grpc.ClientConn | ||
rpcClient rpc.TaskClient | ||
} | ||
|
||
type GRPCPool struct { | ||
// map key格式 ip:port | ||
conns map[string]pool.Pool | ||
sync.RWMutex | ||
conns map[string]*Client | ||
mu sync.RWMutex | ||
} | ||
|
||
func (p *GRPCPool) Get(addr string) (*grpc.ClientConn, error) { | ||
p.RLock() | ||
pool, ok := p.conns[addr] | ||
p.RUnlock() | ||
if !ok { | ||
err := p.newCommonPool(addr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
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 | ||
} | ||
|
||
p.RLock() | ||
pool = p.conns[addr] | ||
p.RUnlock() | ||
conn, err := pool.Get() | ||
client, err := p.factory(addr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
p.conns[addr] = client | ||
|
||
return conn.(*grpc.ClientConn), nil | ||
return client.rpcClient, 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] | ||
p.mu.Lock() | ||
defer p.mu.Unlock() | ||
client, ok := p.conns[addr] | ||
if !ok { | ||
return | ||
} | ||
pool.Release() | ||
delete(p.conns, addr) | ||
client.conn.Close() | ||
} | ||
|
||
// 释放所有连接池 | ||
func (p *GRPCPool) ReleaseAll() { | ||
p.Lock() | ||
defer p.Unlock() | ||
for _, pool := range p.conns { | ||
pool.Release() | ||
} | ||
} | ||
// 创建连接 | ||
func (p *GRPCPool) factory(addr string) (*Client, error) { | ||
p.mu.Lock() | ||
defer p.mu.Unlock() | ||
|
||
// 初始化底层连接池 | ||
func (p *GRPCPool) newCommonPool(addr string) error { | ||
p.Lock() | ||
defer p.Unlock() | ||
commonPool, ok := p.conns[addr] | ||
client, ok := p.conns[addr] | ||
if ok { | ||
return nil | ||
return client, nil | ||
} | ||
poolConfig := &pool.PoolConfig{ | ||
InitialCap: 1, | ||
MaxCap: 30, | ||
Factory: func() (interface{}, error) { | ||
if !app.Setting.EnableTLS { | ||
return grpc.Dial(addr, grpc.WithInsecure()) | ||
} | ||
|
||
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 | ||
} | ||
|
||
return grpc.Dial(addr, grpc.WithTransportCredentials(transportCreds)) | ||
}, | ||
Close: func(v interface{}) error { | ||
conn, ok := v.(*grpc.ClientConn) | ||
if ok && conn != nil { | ||
return conn.Close() | ||
} | ||
return ErrInvalidConn | ||
}, | ||
IdleTimeout: 3 * time.Minute, | ||
opts := []grpc.DialOption{ | ||
grpc.WithKeepaliveParams(keepAliveParams), | ||
grpc.WithBackoffMaxDelay(backOffMaxDelay), | ||
} | ||
|
||
commonPool, err := pool.NewChannelPool(poolConfig) | ||
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...) | ||
if err != nil { | ||
return err | ||
return nil, err | ||
} | ||
|
||
p.conns[addr] = commonPool | ||
client = &Client{ | ||
conn: conn, | ||
rpcClient: rpc.NewTaskClient(conn), | ||
} | ||
|
||
return nil | ||
return client, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.