🐛 Prevent wait group race

This commit is contained in:
LittleSheep 2024-09-15 13:03:11 +08:00
parent 4a48b980f0
commit ecf8c574e4

View File

@ -2,9 +2,10 @@ package grpc
import ( import (
"context" "context"
"sync"
"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) {
@ -17,8 +18,8 @@ func (v *Server) DeliverNotificationBatch(ctx context.Context, request *proto.De
for idx, provider := range request.GetProviders() { for idx, provider := range request.GetProviders() {
token := request.GetDeviceTokens()[idx] token := request.GetDeviceTokens()[idx]
provider := provider provider := provider
go func() {
wg.Add(1) wg.Add(1)
go func() {
services.DealDeliveryTask(&proto.DeliverNotificationRequest{ services.DealDeliveryTask(&proto.DeliverNotificationRequest{
Provider: provider, Provider: provider,
DeviceToken: token, DeviceToken: token,
@ -40,8 +41,8 @@ func (v *Server) DeliverEmailBatch(ctx context.Context, request *proto.DeliverEm
var wg sync.WaitGroup var wg sync.WaitGroup
for _, to := range request.GetTo() { for _, to := range request.GetTo() {
to := to to := to
go func() {
wg.Add(1) wg.Add(1)
go func() {
services.DealDeliveryTask(&proto.DeliverEmailRequest{ services.DealDeliveryTask(&proto.DeliverEmailRequest{
To: to, To: to,
Email: request.GetEmail(), Email: request.GetEmail(),