refactor(worker): remove context from notification structure (#503)

This commit is contained in:
Bo-Yi Wu 2020-04-25 15:23:43 +08:00 committed by GitHub
parent 685a87c930
commit 7dbb5c98e7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 9 additions and 17 deletions

View File

@ -1,7 +1,6 @@
package gorush package gorush
import ( import (
"context"
"errors" "errors"
"net/http" "net/http"
"net/url" "net/url"
@ -52,7 +51,6 @@ type RequestPush struct {
// PushNotification is single notification request // PushNotification is single notification request
type PushNotification struct { type PushNotification struct {
Ctx context.Context
wg *sync.WaitGroup wg *sync.WaitGroup
log *[]LogPushEntry log *[]LogPushEntry

View File

@ -16,27 +16,23 @@ func InitWorkers(ctx context.Context, wg *sync.WaitGroup, workerNum int64, queue
} }
// SendNotification is send message to iOS or Android // SendNotification is send message to iOS or Android
func SendNotification(req PushNotification) { func SendNotification(ctx context.Context, req PushNotification) {
if PushConf.Core.Sync { if PushConf.Core.Sync {
defer req.WaitDone() defer req.WaitDone()
} }
select { switch req.Platform {
case <-req.Ctx.Done(): case PlatFormIos:
default: PushToIOS(req)
switch req.Platform { case PlatFormAndroid:
case PlatFormIos: PushToAndroid(req)
PushToIOS(req)
case PlatFormAndroid:
PushToAndroid(req)
}
} }
} }
func startWorker(ctx context.Context, wg *sync.WaitGroup, num int64) { func startWorker(ctx context.Context, wg *sync.WaitGroup, num int64) {
defer wg.Done() defer wg.Done()
for notification := range QueueNotification { for notification := range QueueNotification {
SendNotification(notification) SendNotification(ctx, notification)
} }
LogAccess.Info("closed the worker num ", num) 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) log := make([]LogPushEntry, 0, count)
for _, notification := range newNotification { for _, notification := range newNotification {
notification.Ctx = ctx
if PushConf.Core.Sync { if PushConf.Core.Sync {
notification.wg = &wg notification.wg = &wg
notification.log = &log notification.log = &log

View File

@ -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) { func (s *Server) Send(ctx context.Context, in *proto.NotificationRequest) (*proto.NotificationReply, error) {
var badge = int(in.Badge) var badge = int(in.Badge)
notification := gorush.PushNotification{ notification := gorush.PushNotification{
Ctx: ctx,
Platform: int(in.Platform), Platform: int(in.Platform),
Tokens: in.Tokens, Tokens: in.Tokens,
Message: in.Message, 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{ return &proto.NotificationReply{
Success: true, Success: true,
@ -125,6 +123,7 @@ func RunGRPCServer(ctx context.Context) error {
select { select {
case <-ctx.Done(): case <-ctx.Done():
s.GracefulStop() // graceful shutdown s.GracefulStop() // graceful shutdown
gorush.LogAccess.Info("shutdown the gRPC server")
} }
}() }()
if err = s.Serve(lis); err != nil { if err = s.Serve(lis); err != nil {