Browse Source

grpc keepalive

master
tangmingyou 3 years ago
parent
commit
ee61d70419
  1. 6
      Dockerfile
  2. 7
      cmd/auth/config.toml
  3. 7
      cmd/chat/config.toml
  4. 8
      cmd/gateway_ws/config.toml
  5. 7
      cmd/mahjong/config.toml
  6. 1
      internal/gateway_http/logic/postal_balancer.go
  7. 25
      internal/gateway_ws/dao/group.go
  8. 13
      pkg/config/config.go
  9. 39
      pkg/config/grpc_options.go

6
Dockerfile

@ -8,9 +8,9 @@ WORKDIR /opt
COPY target/${APP}/${APP} /opt/app COPY target/${APP}/${APP} /opt/app
COPY target/${APP}/config/ /opt/config/ COPY target/${APP}/config/ /opt/config/
# cp /etc/localtime .
COPY localtime /etc/localtime
RUN chmod +x /opt/app \ RUN chmod +x /opt/app
&& ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&& echo 'Asia/Shanghai' > /etc/timezone
CMD ["./app", "-conf=config/config.toml"] CMD ["./app", "-conf=config/config.toml"]

7
cmd/auth/config.toml

@ -11,6 +11,13 @@ writeBufferSize = "8Ki"
[grpc.register.attrs] [grpc.register.attrs]
weight = 10 weight = 10
[grpc.keepalive]
idleTimeout = "60s"
forceCloseWait = "20s"
keepAliveInterval = "60s"
keepAliveTimeout = "20s"
maxLifeTime = "2h"
[etcd] [etcd]
endpoints = ["124.222.131.236:3279"] endpoints = ["124.222.131.236:3279"]
username = "root" username = "root"

7
cmd/chat/config.toml

@ -10,6 +10,13 @@ writeBufferSize = "8Ki"
[grpc.register.attrs] [grpc.register.attrs]
weight = 10 weight = 10
[grpc.keepalive]
idleTimeout = "60s"
forceCloseWait = "20s"
keepAliveInterval = "60s"
keepAliveTimeout = "20s"
maxLifeTime = "2h"
[etcd] [etcd]
endpoints = ["124.222.131.236:3279"] endpoints = ["124.222.131.236:3279"]
username = "root" username = "root"

8
cmd/gateway_ws/config.toml

@ -15,6 +15,13 @@ writeBufferSize = "8Ki"
[grpc.register.attrs] [grpc.register.attrs]
weight = 10 weight = 10
[grpc.keepalive]
idleTimeout = "60s"
forceCloseWait = "20s"
keepAliveInterval = "60s"
keepAliveTimeout = "20s"
maxLifeTime = "2h"
[etcd] [etcd]
endpoints = ["124.222.131.236:3279"] endpoints = ["124.222.131.236:3279"]
username = "root" username = "root"
@ -28,6 +35,7 @@ MinIdleConns = 3
[nats] [nats]
Url = "nats://nats.sopod@124.222.131.236:3222" Url = "nats://nats.sopod@124.222.131.236:3222"
RetryOnFailedConnect = true
[gorm] [gorm]
logMode=true logMode=true

7
cmd/mahjong/config.toml

@ -10,6 +10,13 @@ writeBufferSize = "8Ki"
[grpc.register.attrs] [grpc.register.attrs]
weight = 10 weight = 10
[grpc.keepalive]
idleTimeout = "60s"
forceCloseWait = "20s"
keepAliveInterval = "60s"
keepAliveTimeout = "20s"
maxLifeTime = "2h"
[etcd] [etcd]
endpoints = ["124.222.131.236:3279"] endpoints = ["124.222.131.236:3279"]
username = "root" username = "root"

1
internal/gateway_http/logic/postal_balancer.go

@ -40,6 +40,7 @@ func (h *PostalBalancer) Init(ctx context.Context, resolver resolver.Builder) {
h.postalClient = postal.NewPostalClient(conn) h.postalClient = postal.NewPostalClient(conn)
} }
// Endpoint 根据consistent hash负载均衡策略调用到 postal 集群的一个节点,拿到节点的客户端连接地址
func (h *PostalBalancer) Endpoint(c *gin.Context) { func (h *PostalBalancer) Endpoint(c *gin.Context) {
subject, err := config.GetSubject(c) subject, err := config.GetSubject(c)
if err != nil { if err != nil {

25
internal/gateway_ws/dao/group.go

@ -0,0 +1,25 @@
package dao
import (
"errors"
"github.com/redis/go-redis/v9"
)
var ErrNotExists = errors.New("not exists")
// PostalDao persistent postal group api
type PostalDao interface {
LoadGroupIdsByUid(uid string) ([]string, error)
GroupCreate(gid string) (string, error)
GroupJoin(gid, uid string) error
GroupLeave(gid, uid string) error
GroupDismiss(gid string) error
}
type RedisPostalDao struct {
rdb *redis.Client
}
func (d *RedisPostalDao) LoadGroupIdsByUid(uid string) (gids []string, err error) {
return
}

13
pkg/config/config.go

@ -13,7 +13,6 @@ import (
type Configuration struct { type Configuration struct {
App map[string]any App map[string]any
Grpc GrpcConfig Grpc GrpcConfig
GrpcClient GrpcClientConfig
Gorm GormConfig Gorm GormConfig
Snowflake SnowflakeConfig Snowflake SnowflakeConfig
Etcd clientv3.Config Etcd clientv3.Config
@ -30,13 +29,15 @@ type GrpcConfig struct {
ReadBufferSize string ReadBufferSize string
WriteBufferSize string WriteBufferSize string
Register discovery.Server Register discovery.Server
keepalive GrpcKeepalive
} }
type GrpcKeepaliveConfig struct { type GrpcKeepalive struct {
// todo... IdleTimeout string
} ForceCloseWait string
KeepAliveInterval string
type GrpcClientConfig struct { KeepAliveTimeout string
MaxLifeTime string
} }
type GormConfig struct { type GormConfig struct {

39
pkg/config/grpc_options.go

@ -2,22 +2,43 @@ package config
import ( import (
"google.golang.org/grpc" "google.golang.org/grpc"
"google.golang.org/grpc/keepalive"
"sonet/pkg/utils/conver" "sonet/pkg/utils/conver"
) )
func GetGrpcOptions(config GrpcConfig, customOpts ...grpc.ServerOption) (opts []grpc.ServerOption) { func GetGrpcOptions(c GrpcConfig, customOpts ...grpc.ServerOption) (opts []grpc.ServerOption) {
if config.MaxSendMsgSize != "" { if c.MaxSendMsgSize != "" {
opts = append(opts, grpc.MaxSendMsgSize(conver.MustParseDataUnitInt(config.MaxSendMsgSize))) opts = append(opts, grpc.MaxSendMsgSize(conver.MustParseDataUnitInt(c.MaxSendMsgSize)))
} }
if config.MaxRecvMsgSize != "" { if c.MaxRecvMsgSize != "" {
opts = append(opts, grpc.MaxSendMsgSize(conver.MustParseDataUnitInt(config.MaxRecvMsgSize))) opts = append(opts, grpc.MaxSendMsgSize(conver.MustParseDataUnitInt(c.MaxRecvMsgSize)))
} }
if config.ReadBufferSize != "" { if c.ReadBufferSize != "" {
opts = append(opts, grpc.MaxSendMsgSize(conver.MustParseDataUnitInt(config.ReadBufferSize))) opts = append(opts, grpc.MaxSendMsgSize(conver.MustParseDataUnitInt(c.ReadBufferSize)))
} }
if config.WriteBufferSize != "" { if c.WriteBufferSize != "" {
opts = append(opts, grpc.MaxSendMsgSize(conver.MustParseDataUnitInt(config.WriteBufferSize))) opts = append(opts, grpc.MaxSendMsgSize(conver.MustParseDataUnitInt(c.WriteBufferSize)))
} }
// grpc server keepalive
keep := keepalive.ServerParameters{}
if c.keepalive.IdleTimeout != "" {
keep.MaxConnectionIdle = conver.MustParseDuration(c.keepalive.IdleTimeout)
}
if c.keepalive.ForceCloseWait != "" {
keep.MaxConnectionAgeGrace = conver.MustParseDuration(c.keepalive.ForceCloseWait)
}
if c.keepalive.KeepAliveInterval != "" {
keep.Time = conver.MustParseDuration(c.keepalive.KeepAliveInterval)
}
if c.keepalive.KeepAliveTimeout != "" {
keep.Timeout = conver.MustParseDuration(c.keepalive.KeepAliveTimeout)
}
if c.keepalive.MaxLifeTime != "" {
keep.MaxConnectionAge = conver.MustParseDuration(c.keepalive.MaxLifeTime)
}
opts = append(opts, grpc.KeepaliveParams(keep))
opts = append(opts, customOpts...) opts = append(opts, customOpts...)
return return
} }

Loading…
Cancel
Save