Skip to content

Commit 2a1c309

Browse files
committed
fix(mailserver): reduce memory usage by skipping aggregation when not needed
- Applied a minimal, low-risk optimization to reduce backend memory usage during background historic sync. - Kept existing public behavior unchanged for normal/API-triggered sync calls. - Added an internal path that skips building and merging MessengerResponse payloads when results are not needed. - Wired the async background trigger to use that non-aggregating path.
1 parent 515da6a commit 2a1c309

2 files changed

Lines changed: 13 additions & 5 deletions

File tree

protocol/messenger_mailserver.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,13 @@ func (m *Messenger) resetFiltersPriority(filters types2.ChatFilters) error {
263263
return nil
264264
}
265265

266-
// RequestAllHistoricMessages requests all the historic messages for any topic
266+
// RequestAllHistoricMessages requests all the historic messages for any topic.
267+
// It keeps aggregating all responses for callers that need the merged payload.
267268
func (m *Messenger) RequestAllHistoricMessages(withRetries bool) (*MessengerResponse, error) {
269+
return m.requestAllHistoricMessages(withRetries, true)
270+
}
271+
272+
func (m *Messenger) requestAllHistoricMessages(withRetries bool, aggregateResponses bool) (*MessengerResponse, error) {
268273
shouldSync, err := m.shouldSync()
269274
if err != nil {
270275
return nil, err
@@ -307,7 +312,10 @@ func (m *Messenger) RequestAllHistoricMessages(withRetries bool) (*MessengerResp
307312
m.historicSyncMu.Unlock()
308313
}()
309314

310-
allResponses := &MessengerResponse{}
315+
var allResponses *MessengerResponse
316+
if aggregateResponses {
317+
allResponses = &MessengerResponse{}
318+
}
311319

312320
filters := m.messaging.ChatFilters()
313321
err = m.updateFiltersPriority(filters)
@@ -330,7 +338,7 @@ func (m *Messenger) RequestAllHistoricMessages(withRetries bool) (*MessengerResp
330338
if err != nil {
331339
return nil, err
332340
}
333-
if response != nil {
341+
if aggregateResponses && response != nil {
334342
allResponses.AddChats(response.Chats())
335343
allResponses.AddMessages(response.Messages())
336344
}
@@ -341,7 +349,7 @@ func (m *Messenger) RequestAllHistoricMessages(withRetries bool) (*MessengerResp
341349
if err != nil {
342350
return nil, err
343351
}
344-
if response != nil {
352+
if aggregateResponses && response != nil {
345353
allResponses.AddChats(response.Chats())
346354
allResponses.AddMessages(response.Messages())
347355
}

protocol/messenger_mailserver_cycle.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func (m *Messenger) asyncRequestAllHistoricMessages() {
5151

5252
go func() {
5353
defer gocommon.LogOnPanic()
54-
_, err := m.RequestAllHistoricMessages(true)
54+
_, err := m.requestAllHistoricMessages(true, false)
5555
if err != nil {
5656
m.logger.Error("failed to request historic messages", zap.Error(err))
5757
}

0 commit comments

Comments
 (0)