加入网络层
This commit is contained in:
41
Server/common/net/grpc/grpc_conn/conn.go
Normal file
41
Server/common/net/grpc/grpc_conn/conn.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package grpc_conn
|
||||
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user