⚡ Optimize batch notification speed
This commit is contained in:
@ -3,7 +3,9 @@ package grpc
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"git.solsynth.dev/hydrogen/passport/pkg/internal/database"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
"github.com/samber/lo"
|
||||
|
||||
"git.solsynth.dev/hydrogen/dealer/pkg/proto"
|
||||
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
|
||||
@ -45,3 +47,82 @@ func (v *Server) NotifyUser(_ context.Context, in *proto.NotifyUserRequest) (*pr
|
||||
IsSuccess: true,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (v *Server) NotifyUserBatch(_ context.Context, in *proto.NotifyUserBatchRequest) (*proto.NotifyResponse, error) {
|
||||
var err error
|
||||
var users []models.Account
|
||||
if users, err = services.GetAccountList(lo.Map(in.GetUserId(), func(item uint64, index int) uint {
|
||||
return uint(item)
|
||||
})); err != nil {
|
||||
return nil, fmt.Errorf("unable to get account: %v", err)
|
||||
}
|
||||
|
||||
var metadata map[string]any
|
||||
_ = jsoniter.Unmarshal(in.GetNotify().GetMetadata(), &metadata)
|
||||
|
||||
var notifications []models.Notification
|
||||
for _, user := range users {
|
||||
notification := models.Notification{
|
||||
Topic: in.GetNotify().GetTopic(),
|
||||
Title: in.GetNotify().GetTitle(),
|
||||
Subtitle: in.GetNotify().Subtitle,
|
||||
Body: in.GetNotify().GetBody(),
|
||||
Metadata: metadata,
|
||||
IsRealtime: in.GetNotify().GetIsRealtime(),
|
||||
IsForcePush: in.GetNotify().GetIsForcePush(),
|
||||
AccountID: user.ID,
|
||||
}
|
||||
|
||||
notifications = append(notifications, notification)
|
||||
}
|
||||
|
||||
if in.GetNotify().GetIsRealtime() {
|
||||
services.PushNotificationBatch(notifications)
|
||||
} else {
|
||||
if err := services.NewNotificationBatch(notifications); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return &proto.NotifyResponse{
|
||||
IsSuccess: true,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (v *Server) NotifyAllUser(_ context.Context, in *proto.NotifyRequest) (*proto.NotifyResponse, error) {
|
||||
var users []models.Account
|
||||
if err := database.C.Find(&users).Error; err != nil {
|
||||
return nil, fmt.Errorf("unable to get account: %v", err)
|
||||
}
|
||||
|
||||
var metadata map[string]any
|
||||
_ = jsoniter.Unmarshal(in.GetMetadata(), &metadata)
|
||||
|
||||
var notifications []models.Notification
|
||||
for _, user := range users {
|
||||
notification := models.Notification{
|
||||
Topic: in.GetTopic(),
|
||||
Title: in.GetTitle(),
|
||||
Subtitle: in.Subtitle,
|
||||
Body: in.GetBody(),
|
||||
Metadata: metadata,
|
||||
IsRealtime: in.GetIsRealtime(),
|
||||
IsForcePush: in.GetIsForcePush(),
|
||||
AccountID: user.ID,
|
||||
}
|
||||
|
||||
notifications = append(notifications, notification)
|
||||
}
|
||||
|
||||
if in.GetIsRealtime() {
|
||||
services.PushNotificationBatch(notifications)
|
||||
} else {
|
||||
if err := services.NewNotificationBatch(notifications); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return &proto.NotifyResponse{
|
||||
IsSuccess: true,
|
||||
}, nil
|
||||
}
|
||||
|
@ -25,6 +25,15 @@ func GetAccount(id uint) (models.Account, error) {
|
||||
return account, nil
|
||||
}
|
||||
|
||||
func GetAccountList(id []uint) ([]models.Account, error) {
|
||||
var accounts []models.Account
|
||||
if err := database.C.Where("id IN ?", id).Find(&accounts).Error; err != nil {
|
||||
return accounts, err
|
||||
}
|
||||
|
||||
return accounts, nil
|
||||
}
|
||||
|
||||
func GetAccountWithName(alias string) (models.Account, error) {
|
||||
var account models.Account
|
||||
if err := database.C.Where(models.Account{
|
||||
|
@ -6,6 +6,7 @@ import (
|
||||
"git.solsynth.dev/hydrogen/dealer/pkg/proto"
|
||||
"git.solsynth.dev/hydrogen/passport/pkg/internal/gap"
|
||||
"reflect"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"firebase.google.com/go/messaging"
|
||||
@ -60,6 +61,15 @@ func NewNotification(notification models.Notification) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewNotificationBatch(notifications []models.Notification) error {
|
||||
if err := database.C.CreateInBatches(notifications, 1000).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
PushNotificationBatch(notifications)
|
||||
return nil
|
||||
}
|
||||
|
||||
// PushNotification will push the notification whatever it exists record in the
|
||||
// database Recommend pushing another goroutine when you need to push a lot of
|
||||
// notifications And just use a block statement when you just push one
|
||||
@ -153,3 +163,15 @@ func PushNotification(notification models.Notification) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func PushNotificationBatch(notifications []models.Notification) {
|
||||
var wg sync.WaitGroup
|
||||
for _, notification := range notifications {
|
||||
wg.Add(1)
|
||||
item := notification
|
||||
go func() {
|
||||
_ = PushNotification(item)
|
||||
wg.Done()
|
||||
}()
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user