This commit is contained in:
2025-06-25 00:01:48 +08:00
parent 3d53c9ec59
commit 53106465ed
30 changed files with 2108 additions and 6 deletions

View File

@@ -0,0 +1,41 @@
package grpc_client
import (
"common/log"
"google.golang.org/grpc"
"google.golang.org/grpc/keepalive"
"time"
)
type GrpcConnection struct {
sid string
conn *grpc.ClientConn
}
func NewGrpcConnection(sid, address string) (*GrpcConnection, error) {
p := &GrpcConnection{
sid: sid,
}
conn, err := grpc.Dial(
address,
grpc.WithInsecure(),
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
}

View File

@@ -0,0 +1,62 @@
package grpc_client
import (
"common/log"
"fmt"
"google.golang.org/grpc"
"math/rand"
)
type GrpcConnectionMgr struct {
poolM map[string]*GrpcConnection
poolS []*GrpcConnection
}
func NewGrpcConnectionMgr() *GrpcConnectionMgr {
return &GrpcConnectionMgr{
poolM: make(map[string]*GrpcConnection),
poolS: make([]*GrpcConnection, 0),
}
}
func (p *GrpcConnectionMgr) Store(sid, 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 string) 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 ...string) (*grpc.ClientConn, error) {
var pool *GrpcConnection
if len(sid) > 0 && len(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[string]*grpc.ClientConn {
sidM := make(map[string]*grpc.ClientConn)
for sid, pool := range p.poolM {
sidM[sid] = pool.GetConnection()
}
return sidM
}

View File

@@ -0,0 +1,45 @@
package grpc_client
import (
"context"
"fmt"
"google.golang.org/grpc/stats"
)
// 1. 实现 stats.Handler 接口
type StatsHandler struct{}
func (h *StatsHandler) TagRPC(ctx context.Context, info *stats.RPCTagInfo) context.Context {
// 给 RPC 调用打标签(例如记录方法名)
return context.WithValue(ctx, "rpc_method", info.FullMethodName)
}
func (h *StatsHandler) HandleRPC(ctx context.Context, s stats.RPCStats) {
// 处理 RPC 统计信息
switch t := s.(type) {
case *stats.Begin:
fmt.Printf("RPC started: %s\n", ctx.Value("rpc_method"))
case *stats.End:
fmt.Printf("RPC finished: %s (duration: %v)\n",
ctx.Value("rpc_method"), t.EndTime.Sub(t.BeginTime))
case *stats.InPayload:
fmt.Printf("Received %d bytes\n", t.WireLength)
case *stats.OutPayload:
fmt.Printf("Sent %d bytes\n", t.WireLength)
}
}
func (h *StatsHandler) TagConn(ctx context.Context, info *stats.ConnTagInfo) context.Context {
// 给连接打标签
return ctx
}
func (h *StatsHandler) HandleConn(ctx context.Context, s stats.ConnStats) {
// 处理连接事件
switch s.(type) {
case *stats.ConnBegin:
fmt.Println("Connection established")
case *stats.ConnEnd:
fmt.Println("Connection closed")
}
}