Directly use multiple goroutines to speed up delivery speed

This commit is contained in:
LittleSheep 2024-07-23 18:27:43 +08:00
parent 7541292f68
commit fbfb340058
4 changed files with 38 additions and 42 deletions

View File

@ -4,37 +4,50 @@ import (
"context" "context"
"git.solsynth.dev/hydrogen/dealer/pkg/internal/services" "git.solsynth.dev/hydrogen/dealer/pkg/internal/services"
"git.solsynth.dev/hydrogen/dealer/pkg/proto" "git.solsynth.dev/hydrogen/dealer/pkg/proto"
"sync"
) )
func (v *Server) DeliverNotification(ctx context.Context, request *proto.DeliverNotificationRequest) (*proto.DeliverResponse, error) { func (v *Server) DeliverNotification(ctx context.Context, request *proto.DeliverNotificationRequest) (*proto.DeliverResponse, error) {
services.PublishDeliveryTask(request) services.DealDeliveryTask(request)
return &proto.DeliverResponse{}, nil return &proto.DeliverResponse{}, nil
} }
func (v *Server) DeliverNotificationBatch(ctx context.Context, request *proto.DeliverNotificationBatchRequest) (*proto.DeliverResponse, error) { func (v *Server) DeliverNotificationBatch(ctx context.Context, request *proto.DeliverNotificationBatchRequest) (*proto.DeliverResponse, error) {
var wg sync.WaitGroup
for idx, provider := range request.GetProviders() { for idx, provider := range request.GetProviders() {
token := request.GetDeviceTokens()[idx] token := request.GetDeviceTokens()[idx]
services.PublishDeliveryTask(&proto.DeliverNotificationRequest{ provider := provider
Provider: provider, go func() {
DeviceToken: token, wg.Add(1)
Notify: request.GetNotify(), services.DealDeliveryTask(&proto.DeliverNotificationRequest{
}) Provider: provider,
DeviceToken: token,
Notify: request.GetNotify(),
})
wg.Done()
}()
} }
return &proto.DeliverResponse{}, nil return &proto.DeliverResponse{}, nil
} }
func (v *Server) DeliverEmail(ctx context.Context, request *proto.DeliverEmailRequest) (*proto.DeliverResponse, error) { func (v *Server) DeliverEmail(ctx context.Context, request *proto.DeliverEmailRequest) (*proto.DeliverResponse, error) {
services.PublishDeliveryTask(request) services.DealDeliveryTask(request)
return &proto.DeliverResponse{}, nil return &proto.DeliverResponse{}, nil
} }
func (v *Server) DeliverEmailBatch(ctx context.Context, request *proto.DeliverEmailBatchRequest) (*proto.DeliverResponse, error) { func (v *Server) DeliverEmailBatch(ctx context.Context, request *proto.DeliverEmailBatchRequest) (*proto.DeliverResponse, error) {
var wg sync.WaitGroup
for _, to := range request.GetTo() { for _, to := range request.GetTo() {
services.PublishDeliveryTask(&proto.DeliverEmailRequest{ to := to
To: to, go func() {
Email: request.GetEmail(), wg.Add(1)
}) services.DealDeliveryTask(&proto.DeliverEmailRequest{
To: to,
Email: request.GetEmail(),
})
wg.Done()
}()
} }
return &proto.DeliverResponse{}, nil return &proto.DeliverResponse{}, nil

View File

@ -17,29 +17,20 @@ import (
"time" "time"
) )
var deliveryTaskQueue = make(chan any, 256) func DealDeliveryTask(task any) {
switch tk := task.(type) {
func PublishDeliveryTask(task any) { case *proto.DeliverEmailRequest:
deliveryTaskQueue <- task if tk.GetEmail().HtmlBody != nil {
} _ = SendMailHTML(tk.GetTo(), tk.GetEmail().GetSubject(), tk.GetEmail().GetHtmlBody())
} else {
func ConsumeDeliveryTasks() { _ = SendMail(tk.GetTo(), tk.GetEmail().GetSubject(), tk.GetEmail().GetTextBody())
for { }
task := <-deliveryTaskQueue case *proto.DeliverNotificationRequest:
switch tk := task.(type) { switch tk.GetProvider() {
case *proto.DeliverEmailRequest: case "firebase":
if tk.GetEmail().HtmlBody != nil { _ = PushFirebaseNotify(tk.GetDeviceToken(), tk.GetNotify())
_ = SendMailHTML(tk.GetTo(), tk.GetEmail().GetSubject(), tk.GetEmail().GetHtmlBody()) case "apple":
} else { _ = PushAppleNotify(tk.GetDeviceToken(), tk.GetNotify())
_ = SendMail(tk.GetTo(), tk.GetEmail().GetSubject(), tk.GetEmail().GetTextBody())
}
case *proto.DeliverNotificationRequest:
switch tk.GetProvider() {
case "firebase":
_ = PushFirebaseNotify(tk.GetDeviceToken(), tk.GetNotify())
case "apple":
_ = PushAppleNotify(tk.GetDeviceToken(), tk.GetNotify())
}
} }
} }
} }

View File

@ -41,11 +41,6 @@ func main() {
log.Warn().Err(err).Msg("An error occurred when setup APNs, apple notification push is unavailable...") log.Warn().Err(err).Msg("An error occurred when setup APNs, apple notification push is unavailable...")
} }
// Set up tasks queue consumers
for idx := 0; idx < max(1, viper.GetInt("performance.notification_deliver.worker_count")); idx++ {
go services.ConsumeDeliveryTasks()
}
// Server // Server
go server.NewServer().Listen() go server.NewServer().Listen()

View File

@ -16,9 +16,6 @@ firebase_credentials = ""
use_registration_magic_token = false use_registration_magic_token = false
[performance]
notification_deliver.worker_count = 4
[dealer] [dealer]
addr = "127.0.0.1:8442" addr = "127.0.0.1:8442"