Skip to content
This repository has been archived by the owner on Jul 30, 2020. It is now read-only.

Commit

Permalink
Merge pull request #3 from algolia/feat/keep-last-n
Browse files Browse the repository at this point in the history
Limit the number of pending requests per searcher
  • Loading branch information
Clément Le Provost committed Dec 16, 2016
2 parents 87e99cd + 0776ef7 commit 65cb317
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions Sources/Searcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,12 @@ import Foundation
/// All currently ongoing requests.
private var pendingRequests: [Int: Operation] = [:]

/// Maximum number of pending requests allowed.
/// If many requests are made in a short time, this will keep only the N most recent and cancel the older ones.
/// This helps to avoid filling up the request queue when the network is slow.
///
@objc public var maxPendingRequests: Int = 3

// MARK: - Initialization, termination

/// Create a new searcher targeting the specified index.
Expand Down Expand Up @@ -339,6 +345,12 @@ import Foundation
Searcher.notificationSeqNoKey: currentSeqNo
]

// Cancel too old requests.
let tooOldRequests = pendingRequests.filter({ $0.0 <= currentSeqNo - maxPendingRequests })
for (seqNo, _) in tooOldRequests {
cancelRequest(seqNo: seqNo)
}

// Run request
// -----------
var operation: Operation!
Expand Down

0 comments on commit 65cb317

Please sign in to comment.