Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[core] OnlineFileSource - prevent double scheduling of requests
Browse files Browse the repository at this point in the history
  • Loading branch information
ivovandongen committed Sep 16, 2016
1 parent 72194d6 commit eb89202
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions platform/default/online_file_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,16 @@ class OnlineFileSource::Impl {
pendingRequestsMap.erase(it);
}
}
assert(pendingRequestsMap.size() == pendingRequestsList.size());
}

void activateOrQueueRequest(OnlineFileRequest* request) {
assert(allRequests.find(request) != allRequests.end());
assert(activeRequests.find(request) == activeRequests.end());
assert(!request->request);

//Bail out if this request is already being handled
if (isScheduled(request) || isActive(request)) {
return;
}

if (activeRequests.size() >= HTTPFileSource::maximumConcurrentRequests()) {
queueRequest(request);
Expand All @@ -94,6 +98,7 @@ class OnlineFileSource::Impl {
void queueRequest(OnlineFileRequest* request) {
auto it = pendingRequestsList.insert(pendingRequestsList.end(), request);
pendingRequestsMap.emplace(request, std::move(it));
assert(pendingRequestsMap.size() == pendingRequestsList.size());
}

void activateRequest(OnlineFileRequest* request) {
Expand All @@ -104,6 +109,7 @@ class OnlineFileSource::Impl {
request->request.reset();
request->completed(response);
});
assert(pendingRequestsMap.size() == pendingRequestsList.size());
}

void activatePendingRequest() {
Expand All @@ -117,6 +123,7 @@ class OnlineFileSource::Impl {
pendingRequestsMap.erase(request);

activateRequest(request);
assert(pendingRequestsMap.size() == pendingRequestsList.size());
}

private:
Expand All @@ -125,6 +132,14 @@ class OnlineFileSource::Impl {
request->networkIsReachableAgain();
}
}

inline bool isScheduled(OnlineFileRequest* request) {
return pendingRequestsMap.find(request) != pendingRequestsMap.end();
}

inline bool isActive(OnlineFileRequest* request) {
return activeRequests.find(request) != activeRequests.end();
}

/**
* The lifetime of a request is:
Expand Down

0 comments on commit eb89202

Please sign in to comment.