feat 初次提交
This commit is contained in:
42
net/grpc/grpc_conn/conn.go
Normal file
42
net/grpc/grpc_conn/conn.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package grpc_conn
|
||||
|
||||
import (
|
||||
"common/log"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/credentials/insecure"
|
||||
"google.golang.org/grpc/keepalive"
|
||||
"time"
|
||||
)
|
||||
|
||||
type GrpcConnection struct {
|
||||
sid int64
|
||||
conn *grpc.ClientConn
|
||||
}
|
||||
|
||||
func NewGrpcConnection(sid int64, address string) (*GrpcConnection, error) {
|
||||
p := &GrpcConnection{
|
||||
sid: sid,
|
||||
}
|
||||
conn, err := grpc.NewClient(
|
||||
address,
|
||||
grpc.WithTransportCredentials(insecure.NewCredentials()),
|
||||
grpc.WithKeepaliveParams(
|
||||
keepalive.ClientParameters{
|
||||
Time: 30 * time.Second, // 保活探测包发送的时间间隔
|
||||
Timeout: 10 * time.Second, // 保活探测包的超时时间
|
||||
PermitWithoutStream: true,
|
||||
},
|
||||
),
|
||||
//grpc.WithStatsHandler(&StatsHandler{}),
|
||||
)
|
||||
if err != nil {
|
||||
log.Errorf("create grpc err: %v, sid: %v, addr: %v", err, sid, address)
|
||||
return nil, err
|
||||
}
|
||||
p.conn = conn
|
||||
return p, nil
|
||||
}
|
||||
|
||||
func (g *GrpcConnection) GetConnection() *grpc.ClientConn {
|
||||
return g.conn
|
||||
}
|
||||
62
net/grpc/grpc_conn/conn_mgr.go
Normal file
62
net/grpc/grpc_conn/conn_mgr.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package grpc_conn
|
||||
|
||||
import (
|
||||
"common/log"
|
||||
"fmt"
|
||||
"google.golang.org/grpc"
|
||||
"math/rand"
|
||||
)
|
||||
|
||||
type GrpcConnectionMgr struct {
|
||||
poolM map[int64]*GrpcConnection
|
||||
poolS []*GrpcConnection
|
||||
}
|
||||
|
||||
func NewGrpcConnectionMgr() *GrpcConnectionMgr {
|
||||
return &GrpcConnectionMgr{
|
||||
poolM: make(map[int64]*GrpcConnection),
|
||||
poolS: make([]*GrpcConnection, 0),
|
||||
}
|
||||
}
|
||||
|
||||
func (p *GrpcConnectionMgr) Store(sid int64, addr string) {
|
||||
pool, err := NewGrpcConnection(sid, addr)
|
||||
if err != nil {
|
||||
log.Errorf("create grpc err: %v, sid: %v, addr: %v", err, sid, addr)
|
||||
return
|
||||
}
|
||||
p.poolM[sid] = pool
|
||||
p.poolS = append(p.poolS, pool)
|
||||
}
|
||||
|
||||
func (p *GrpcConnectionMgr) Delete(sid int64) int {
|
||||
delete(p.poolM, sid)
|
||||
for i, pool := range p.poolS {
|
||||
if pool.sid == sid {
|
||||
p.poolS = append(p.poolS[:i], p.poolS[i+1:]...)
|
||||
break
|
||||
}
|
||||
}
|
||||
return len(p.poolS)
|
||||
}
|
||||
|
||||
func (p *GrpcConnectionMgr) Load(sid ...int64) (*grpc.ClientConn, error) {
|
||||
var pool *GrpcConnection
|
||||
if len(sid) > 0 && sid[0] > 0 {
|
||||
pool = p.poolM[sid[0]]
|
||||
} else {
|
||||
pool = p.poolS[rand.Intn(len(p.poolS))]
|
||||
}
|
||||
if pool == nil {
|
||||
return nil, fmt.Errorf("cannot find connection")
|
||||
}
|
||||
return pool.GetConnection(), nil
|
||||
}
|
||||
|
||||
func (p *GrpcConnectionMgr) LoadAll() map[int64]*grpc.ClientConn {
|
||||
sidM := make(map[int64]*grpc.ClientConn)
|
||||
for sid, pool := range p.poolM {
|
||||
sidM[sid] = pool.GetConnection()
|
||||
}
|
||||
return sidM
|
||||
}
|
||||
Reference in New Issue
Block a user