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
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

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
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

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) {
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 {