Skip to content

Commit f98a17b

Browse files
feat: limit api store queries to 24h (#1257)
1 parent 96702e2 commit f98a17b

File tree

1 file changed

+33
-4
lines changed

1 file changed

+33
-4
lines changed

waku/v2/api/history/history.go

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,26 @@ loop:
159159
}()
160160

161161
queryCtx, queryCancel := context.WithTimeout(ctx, mailserverRequestTimeout)
162-
cursor, envelopesCount, err := hr.createMessagesRequest(queryCtx, storenodeID, w.criteria, w.cursor, w.limit, true, processEnvelopes, logger)
162+
163+
// If time range is greater than 24 hours, limit the range: to - (to-24h)
164+
// TODO: handle cases in which TimeStart/TimeEnd could be nil
165+
// (this type of query does not happen in status-go, though, and
166+
// nwaku might limit query duration to 24h anyway, so perhaps
167+
// it's not worth adding such logic)
168+
timeStart := w.criteria.TimeStart
169+
timeEnd := w.criteria.TimeEnd
170+
exceeds24h := false
171+
if timeStart != nil && timeEnd != nil && *timeEnd-*timeStart > (24*time.Hour).Nanoseconds() {
172+
newTimeStart := *timeEnd - (24 * time.Hour).Nanoseconds()
173+
timeStart = &newTimeStart
174+
exceeds24h = true
175+
}
176+
177+
newCriteria := w.criteria
178+
newCriteria.TimeStart = timeStart
179+
newCriteria.TimeEnd = timeEnd
180+
181+
cursor, envelopesCount, err := hr.createMessagesRequest(queryCtx, storenodeID, newCriteria, w.cursor, w.limit, true, processEnvelopes, logger)
163182
queryCancel()
164183

165184
if err != nil {
@@ -180,18 +199,28 @@ loop:
180199

181200
// Check the cursor after calling `shouldProcessNextPage`.
182201
// The app might use process the fetched envelopes in the callback for own needs.
183-
if cursor == nil {
202+
// If from/to does not exceed 24h and no cursor was returned, we have already
203+
// requested the entire time range
204+
if cursor == nil && !exceeds24h {
184205
return
185206
}
186207

187208
logger.Debug("processBatch producer - creating work (cursor)")
188209

189-
workWg.Add(1)
190-
workCh <- work{
210+
newWork := work{
191211
criteria: w.criteria,
192212
cursor: cursor,
193213
limit: nextPageLimit,
194214
}
215+
216+
// If from/to has exceeded the 24h, but there are no more records within the current
217+
// 24h range, then we update the `to` for the new work to not include it.
218+
if cursor == nil && exceeds24h {
219+
newWork.criteria.TimeEnd = timeStart
220+
}
221+
222+
workWg.Add(1)
223+
workCh <- newWork
195224
}(w)
196225
case err := <-errCh:
197226
logger.Debug("processBatch - received error", zap.Error(err))

0 commit comments

Comments
 (0)