Directly use multiple goroutines to speed up delivery speed

This commit is contained in:
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"
"git.solsynth.dev/hydrogen/dealer/pkg/internal/services"
"git.solsynth.dev/hydrogen/dealer/pkg/proto"
"sync"
)
func (v *Server) DeliverNotification(ctx context.Context, request *proto.DeliverNotificationRequest) (*proto.DeliverResponse, error) {
services.PublishDeliveryTask(request)
services.DealDeliveryTask(request)
return &proto.DeliverResponse{}, nil
}
func (v *Server) DeliverNotificationBatch(ctx context.Context, request *proto.DeliverNotificationBatchRequest) (*proto.DeliverResponse, error) {
var wg sync.WaitGroup
for idx, provider := range request.GetProviders() {
token := request.GetDeviceTokens()[idx]
services.PublishDeliveryTask(&proto.DeliverNotificationRequest{
Provider: provider,
DeviceToken: token,
Notify: request.GetNotify(),
})
provider := provider
go func() {
wg.Add(1)
services.DealDeliveryTask(&proto.DeliverNotificationRequest{
Provider: provider,
DeviceToken: token,
Notify: request.GetNotify(),
})
wg.Done()
}()
}
return &proto.DeliverResponse{}, nil
}
func (v *Server) DeliverEmail(ctx context.Context, request *proto.DeliverEmailRequest) (*proto.DeliverResponse, error) {
services.PublishDeliveryTask(request)
services.DealDeliveryTask(request)
return &proto.DeliverResponse{}, nil
}
func (v *Server) DeliverEmailBatch(ctx context.Context, request *proto.DeliverEmailBatchRequest) (*proto.DeliverResponse, error) {
var wg sync.WaitGroup
for _, to := range request.GetTo() {
services.PublishDeliveryTask(&proto.DeliverEmailRequest{
To: to,
Email: request.GetEmail(),
})
to := to
go func() {
wg.Add(1)
services.DealDeliveryTask(&proto.DeliverEmailRequest{
To: to,
Email: request.GetEmail(),
})
wg.Done()
}()
}
return &proto.DeliverResponse{}, nil