chore: support single queue and multiple worker (#589)

This commit is contained in:
Bo-Yi Wu
2021-07-16 12:10:34 +08:00
committed by GitHub
parent 0658f18430
commit ab8b1991ab
34 changed files with 1020 additions and 881 deletions

View File

@@ -6,6 +6,7 @@ import (
"strings"
"sync"
"github.com/appleboy/gorush/config"
"github.com/appleboy/gorush/core"
"github.com/appleboy/gorush/gorush"
"github.com/appleboy/gorush/logx"
@@ -19,14 +20,16 @@ import (
// Server is used to implement gorush grpc server.
type Server struct {
mu sync.Mutex
cfg config.ConfYaml
mu sync.Mutex
// statusMap stores the serving status of the services this Server monitors.
statusMap map[string]proto.HealthCheckResponse_ServingStatus
}
// NewServer returns a new Server.
func NewServer() *Server {
func NewServer(cfg config.ConfYaml) *Server {
return &Server{
cfg: cfg,
statusMap: make(map[string]proto.HealthCheckResponse_ServingStatus),
}
}
@@ -98,7 +101,7 @@ func (s *Server) Send(ctx context.Context, in *proto.NotificationRequest) (*prot
}
}
go gorush.SendNotification(ctx, notification)
go gorush.SendNotification(s.cfg, notification)
return &proto.NotificationReply{
Success: true,
@@ -107,26 +110,26 @@ func (s *Server) Send(ctx context.Context, in *proto.NotificationRequest) (*prot
}
// RunGRPCServer run gorush grpc server
func RunGRPCServer(ctx context.Context) error {
if !gorush.PushConf.GRPC.Enabled {
func RunGRPCServer(ctx context.Context, cfg config.ConfYaml) error {
if !cfg.GRPC.Enabled {
logx.LogAccess.Info("gRPC server is disabled.")
return nil
}
s := grpc.NewServer()
rpcSrv := NewServer()
rpcSrv := NewServer(cfg)
proto.RegisterGorushServer(s, rpcSrv)
proto.RegisterHealthServer(s, rpcSrv)
// Register reflection service on gRPC server.
reflection.Register(s)
lis, err := net.Listen("tcp", ":"+gorush.PushConf.GRPC.Port)
lis, err := net.Listen("tcp", ":"+cfg.GRPC.Port)
if err != nil {
logx.LogError.Fatalln(err)
return err
}
logx.LogAccess.Info("gRPC server is running on " + gorush.PushConf.GRPC.Port + " port.")
logx.LogAccess.Info("gRPC server is running on " + cfg.GRPC.Port + " port.")
go func() {
select {
case <-ctx.Done():

View File

@@ -4,7 +4,7 @@ import (
"context"
"testing"
"github.com/appleboy/gorush/gorush"
"github.com/appleboy/gorush/config"
"github.com/appleboy/gorush/logx"
"google.golang.org/grpc"
@@ -13,22 +13,29 @@ import (
const gRPCAddr = "localhost:9000"
func initTest() config.ConfYaml {
cfg, _ := config.LoadConf()
cfg.Core.Mode = "test"
return cfg
}
func TestGracefulShutDownGRPCServer(t *testing.T) {
cfg := initTest()
// server configs
logx.InitLog(
gorush.PushConf.Log.AccessLevel,
gorush.PushConf.Log.AccessLog,
gorush.PushConf.Log.ErrorLevel,
gorush.PushConf.Log.ErrorLog,
cfg.Log.AccessLevel,
cfg.Log.AccessLog,
cfg.Log.ErrorLevel,
cfg.Log.ErrorLog,
)
gorush.PushConf.GRPC.Enabled = true
gorush.PushConf.GRPC.Port = "9000"
gorush.PushConf.Log.Format = "json"
cfg.GRPC.Enabled = true
cfg.GRPC.Port = "9000"
cfg.Log.Format = "json"
// Run gRPC server
ctx, gRPCContextCancel := context.WithCancel(context.Background())
go func() {
if err := RunGRPCServer(ctx); err != nil {
if err := RunGRPCServer(ctx, cfg); err != nil {
panic(err)
}
}()