From 7dbb5c98e7c34c6bbe8d145ab156584154c803d9 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Sat, 25 Apr 2020 15:23:43 +0800 Subject: [PATCH] refactor(worker): remove context from notification structure (#503) --- gorush/notification.go | 2 -- gorush/worker.go | 19 +++++++------------ rpc/server.go | 5 ++--- 3 files changed, 9 insertions(+), 17 deletions(-) diff --git a/gorush/notification.go b/gorush/notification.go index ff2fe7f..f38e66d 100644 --- a/gorush/notification.go +++ b/gorush/notification.go @@ -1,7 +1,6 @@ package gorush import ( - "context" "errors" "net/http" "net/url" @@ -52,7 +51,6 @@ type RequestPush struct { // PushNotification is single notification request type PushNotification struct { - Ctx context.Context wg *sync.WaitGroup log *[]LogPushEntry diff --git a/gorush/worker.go b/gorush/worker.go index 5e0183d..6eb6081 100644 --- a/gorush/worker.go +++ b/gorush/worker.go @@ -16,27 +16,23 @@ func InitWorkers(ctx context.Context, wg *sync.WaitGroup, workerNum int64, queue } // SendNotification is send message to iOS or Android -func SendNotification(req PushNotification) { +func SendNotification(ctx context.Context, req PushNotification) { if PushConf.Core.Sync { defer req.WaitDone() } - select { - case <-req.Ctx.Done(): - default: - switch req.Platform { - case PlatFormIos: - PushToIOS(req) - case PlatFormAndroid: - PushToAndroid(req) - } + switch req.Platform { + case PlatFormIos: + PushToIOS(req) + case PlatFormAndroid: + PushToAndroid(req) } } func startWorker(ctx context.Context, wg *sync.WaitGroup, num int64) { defer wg.Done() for notification := range QueueNotification { - SendNotification(notification) + SendNotification(ctx, notification) } LogAccess.Info("closed the worker num ", num) } @@ -72,7 +68,6 @@ func queueNotification(ctx context.Context, req RequestPush) (int, []LogPushEntr log := make([]LogPushEntry, 0, count) for _, notification := range newNotification { - notification.Ctx = ctx if PushConf.Core.Sync { notification.wg = &wg notification.log = &log diff --git a/rpc/server.go b/rpc/server.go index 3c02289..aedb820 100644 --- a/rpc/server.go +++ b/rpc/server.go @@ -50,8 +50,6 @@ func (s *Server) Check(ctx context.Context, in *proto.HealthCheckRequest) (*prot func (s *Server) Send(ctx context.Context, in *proto.NotificationRequest) (*proto.NotificationReply, error) { var badge = int(in.Badge) notification := gorush.PushNotification{ - Ctx: ctx, - Platform: int(in.Platform), Tokens: in.Tokens, Message: in.Message, @@ -92,7 +90,7 @@ func (s *Server) Send(ctx context.Context, in *proto.NotificationRequest) (*prot } } - go gorush.SendNotification(notification) + go gorush.SendNotification(ctx, notification) return &proto.NotificationReply{ Success: true, @@ -125,6 +123,7 @@ func RunGRPCServer(ctx context.Context) error { select { case <-ctx.Done(): s.GracefulStop() // graceful shutdown + gorush.LogAccess.Info("shutdown the gRPC server") } }() if err = s.Serve(lis); err != nil {