From 583daeb1328bf69b894175f912164aa87833294b Mon Sep 17 00:00:00 2001 From: Agniva De Sarker Date: Wed, 8 Apr 2020 21:08:57 +0530 Subject: [PATCH] MM-23547: remove redundant JSON parsing in push notifications (#14181) Automatic Merge --- app/notification_push.go | 18 +++--------------- app/notification_push_test.go | 2 +- model/push_notification.go | 5 +++++ 3 files changed, 9 insertions(+), 16 deletions(-) diff --git a/app/notification_push.go b/app/notification_push.go index fae062847d..4841b72145 100644 --- a/app/notification_push.go +++ b/app/notification_push.go @@ -90,17 +90,6 @@ func (a *App) sendPushNotificationToAllSessions(msg *model.PushNotification, use ) } - notification, parseError := model.PushNotificationFromJson(strings.NewReader(msg.ToJson())) - if parseError != nil { - return model.NewAppError( - "pushNotification", - "api.push_notifications.message.parse.app_error", - nil, - parseError.Error(), - http.StatusInternalServerError, - ) - } - for _, session := range sessions { // Don't send notifications to this session if it's expired or we want to skip it if session.IsExpired() || (skipSessionId != "" && skipSessionId == session.Id) { @@ -108,11 +97,11 @@ func (a *App) sendPushNotificationToAllSessions(msg *model.PushNotification, use } // We made a copy to avoid decoding and parsing all the time - tmpMessage := notification + tmpMessage := msg.DeepCopy() tmpMessage.SetDeviceIdAndPlatform(session.DeviceId) tmpMessage.AckId = model.NewId() - err := a.sendToPushProxy(*tmpMessage, session) + err := a.sendToPushProxy(tmpMessage, session) if err != nil { a.NotificationsLog().Error("Notification error", mlog.String("ackId", tmpMessage.AckId), @@ -123,7 +112,6 @@ func (a *App) sendPushNotificationToAllSessions(msg *model.PushNotification, use mlog.String("deviceId", tmpMessage.DeviceId), mlog.String("status", err.Error()), ) - continue } @@ -316,7 +304,7 @@ func (a *App) StopPushNotificationsHubWorkers() { } } -func (a *App) sendToPushProxy(msg model.PushNotification, session *model.Session) error { +func (a *App) sendToPushProxy(msg *model.PushNotification, session *model.Session) error { msg.ServerId = a.DiagnosticId() a.NotificationsLog().Info("Notification will be sent", diff --git a/app/notification_push_test.go b/app/notification_push_test.go index 3ccec8f118..bbd4e29658 100644 --- a/app/notification_push_test.go +++ b/app/notification_push_test.go @@ -1350,7 +1350,7 @@ func TestAllPushNotifications(t *testing.T) { } // Run it with | grep -v '{"level"' to prevent spamming the console. -func BenchmarkPushNotification(b *testing.B) { +func BenchmarkPushNotificationThroughput(b *testing.B) { th := SetupWithStoreMock(b) defer th.TearDown() diff --git a/model/push_notification.go b/model/push_notification.go index a601f85a02..5b0118ceca 100644 --- a/model/push_notification.go +++ b/model/push_notification.go @@ -74,6 +74,11 @@ func (me *PushNotification) ToJson() string { return string(b) } +func (me *PushNotification) DeepCopy() *PushNotification { + copy := *me + return © +} + func (me *PushNotification) SetDeviceIdAndPlatform(deviceId string) { index := strings.Index(deviceId, ":")