Skip to content

Commit

Permalink
Merge pull request #67 from brave-intl/load_publishers_list-1
Browse files Browse the repository at this point in the history
fixed next interval calculation when downloading publishers list
  • Loading branch information
NejcZdovc authored Sep 2, 2018
2 parents 5fd54cb + dcd4943 commit 4000402
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
2 changes: 2 additions & 0 deletions src/bat_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -759,12 +759,14 @@ namespace braveledger_bat_helper {
min_pubslisher_duration_(braveledger_ledger::_default_min_pubslisher_duration),
min_visits_(1),
allow_non_verified_(true),
pubs_load_timestamp_ (0ull),
allow_videos_(true) {}

PUBLISHER_STATE_ST::PUBLISHER_STATE_ST(const PUBLISHER_STATE_ST& state) {
min_pubslisher_duration_ = state.min_pubslisher_duration_;
min_visits_ = state.min_visits_;
allow_non_verified_ = state.allow_non_verified_;
pubs_load_timestamp_ = state.pubs_load_timestamp_;
allow_videos_ = state.allow_videos_;
monthly_balances_ = state.monthly_balances_;
recurring_donation_ = state.recurring_donation_;
Expand Down
38 changes: 23 additions & 15 deletions src/ledger_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -520,14 +520,14 @@ void LedgerImpl::OnTimer(uint32_t timer_id) {


void LedgerImpl::LoadPublishersListCallback(bool result, const std::string& response) {
bool retryAfterError = true;
if (result && !response.empty()) {
retryAfterError = false;
//no error so far
bat_publishers_->RefreshPublishersList(response);
}

//set timer
RefreshPublishersList(retryAfterError);
else {
//error: retry downloading again
RefreshPublishersList(true);
}
}

void LedgerImpl::RefreshPublishersList(bool retryAfterError) {
Expand All @@ -549,23 +549,31 @@ void LedgerImpl::RefreshPublishersList(bool retryAfterError) {
uint64_t now = std::time(nullptr);
uint64_t lastLoadTimestamp = bat_publishers_->getLastPublishersListLoadTimestamp();

//check if lastLoadTimestamp doesn't exist or have erroneous value
// (start_timer_in == 0) is expected to call callback function immediately
start_timer_in = (lastLoadTimestamp == 0ull || lastLoadTimestamp > now) ? 0ull : now - lastLoadTimestamp;
//check if lastLoadTimestamp doesn't exist or have erroneous value.
//(start_timer_in == 0) is expected to call callback function immediately.

//time since last succesfull download
uint64_t time_since_last_download = (lastLoadTimestamp == 0ull || lastLoadTimestamp > now) ? 0ull : now - lastLoadTimestamp;

if (now == lastLoadTimestamp) {
start_timer_in = braveledger_ledger::_publishers_list_load_interval;
}
else if (time_since_last_download > 0 && time_since_last_download < braveledger_ledger::_publishers_list_load_interval) {
start_timer_in = braveledger_ledger::_publishers_list_load_interval - time_since_last_download;
}
else {
start_timer_in = 0ull;
}
}

//start timer
ledger_client_->SetTimer(start_timer_in, last_pub_load_timer_id_);
}

void LedgerImpl::OnPublishersListSaved(ledger::Result result) {

if (ledger::Result::OK == result) {
bat_publishers_->OnPublishersListSaved(result);
}
else {
RefreshPublishersList(true);
}
bool retryAfterError = (ledger::Result::OK == result) ? false : true;
bat_publishers_->OnPublishersListSaved(result);
RefreshPublishersList(retryAfterError);
}

} // namespace bat_ledger

0 comments on commit 4000402

Please sign in to comment.