Skip to content

Commit

Permalink
Ads Serve failures should retry (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
tmancey authored Jan 24, 2019
1 parent 114619c commit 0ef7164
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 8 deletions.
68 changes: 60 additions & 8 deletions src/confirmations_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ ConfirmationsImpl::ConfirmationsImpl(
step_2_refill_confirmations_timer_id_(0),
step_4_retrieve_payment_ious_timer_id_(0),
step_5_cash_in_payment_ious_timer_id_(0),
fetch_tokens_timer_id_(0),
confirmations_client_(confirmations_client) {
BLOG(INFO) << "Initializing Confirmations";

Expand All @@ -49,6 +50,7 @@ ConfirmationsImpl::~ConfirmationsImpl() {
BLOG(INFO) << "Deinitializing Confirmations";

StopRefillingConfirmations();
StopFetchingTokens();
StopRetrievingPaymentIOUs();
StopCashingInPaymentIOUs();
}
Expand Down Expand Up @@ -306,21 +308,27 @@ void ConfirmationsImpl::Step2bRefillConfirmationsIfNecessary(
// STEP 2.3 This is done blocking and assumes success but we need to
// separate it more and account for the possibility of failures

BLOG(INFO) << " Step2.3: GET /v1/confirmation/token/{payment_id}?"
<< "nonce={nonce}";

std::string endpoint = std::string("/v1/confirmation/token/").append(
real_wallet_address_).append("?nonce=").append(nonce);
std::string server_url = GetServerUrl().append(endpoint);
fetch_tokens_server_url_ = GetServerUrl().append(endpoint);

FetchTokens();
}

void ConfirmationsImpl::FetchTokens() {
BLOG(INFO) << "FetchTokens";

BLOG(INFO) << " Step2.3: GET /v1/confirmation/token/{payment_id}?"
<< "nonce={nonce}";

BLOG(INFO) << " URL Request:";
BLOG(INFO) << " URL: " << server_url;
BLOG(INFO) << " URL: " << fetch_tokens_server_url_;

auto callback = std::bind(
&ConfirmationsImpl::Step2cRefillConfirmationsIfNecessary,
this, server_url, _1, _2, _3);
this, fetch_tokens_server_url_, _1, _2, _3);

confirmations_client_->URLRequest(server_url, {}, "", "",
confirmations_client_->URLRequest(fetch_tokens_server_url_, {}, "", "",
URLRequestMethod::GET, callback);
}

Expand All @@ -345,6 +353,12 @@ void ConfirmationsImpl::Step2cRefillConfirmationsIfNecessary(
BLOG(INFO) << " " << header.first << ": " << header.second;
}

if (response_status_code != 200) {
BLOG(WARNING) << "Failed to fetch tokens";
StartFetchingTokens(kRetryFetchingTokensAfterSeconds);
return;
}

std::unique_ptr<base::Value> value(base::JSONReader::Read(response));
base::DictionaryValue* dict;
if (!value->GetAsDictionary(&dict)) {
Expand Down Expand Up @@ -1497,14 +1511,18 @@ void ConfirmationsImpl::OnTimer(const uint32_t timer_id) {
<< " step_4_retrieve_payment_ious_timer_id_: "
<< std::to_string(step_4_retrieve_payment_ious_timer_id_) << std::endl
<< " step_5_cash_in_payment_ious_timer_id_: "
<< std::to_string(step_5_cash_in_payment_ious_timer_id_) << std::endl;
<< std::to_string(step_5_cash_in_payment_ious_timer_id_) << std::endl
<< " fetch_tokens_timer_id_: "
<< std::to_string(fetch_tokens_timer_id_);

if (timer_id == step_2_refill_confirmations_timer_id_) {
RefillConfirmations();
} else if (timer_id == step_4_retrieve_payment_ious_timer_id_) {
RetrievePaymentIOUs();
} else if (timer_id == step_5_cash_in_payment_ious_timer_id_) {
CashInPaymentIOUs();
} else if (timer_id == fetch_tokens_timer_id_) {
FetchTokens();
} else {
LOG(WARNING) << "Unexpected OnTimer: " << std::to_string(timer_id);
}
Expand Down Expand Up @@ -1657,4 +1675,38 @@ bool ConfirmationsImpl::IsCashingInPaymentIOUs() const {
return true;
}

void ConfirmationsImpl::StartFetchingTokens(
const uint64_t start_timer_in) {
StopFetchingTokens();

fetch_tokens_timer_id_ = confirmations_client_->SetTimer(start_timer_in);
if (fetch_tokens_timer_id_ == 0) {
BLOG(ERROR) <<
"Failed to start fetching tokens due to an invalid timer";

return;
}

BLOG(INFO) << "Start fetching tokens in " << start_timer_in << " seconds";
}

void ConfirmationsImpl::StopFetchingTokens() {
if (!IsFetchingTokens()) {
return;
}

BLOG(INFO) << "Stopped fetching tokens";

confirmations_client_->KillTimer(fetch_tokens_timer_id_);
fetch_tokens_timer_id_ = 0;
}

bool ConfirmationsImpl::IsFetchingTokens() const {
if (fetch_tokens_timer_id_ == 0) {
return false;
}

return true;
}

} // namespace confirmations
7 changes: 7 additions & 0 deletions src/confirmations_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ class ConfirmationsImpl : public Confirmations {
void StopCashingInPaymentIOUs();
bool IsCashingInPaymentIOUs() const;

uint32_t fetch_tokens_timer_id_;
std::string fetch_tokens_server_url_;
void StartFetchingTokens(const uint64_t start_timer_in);
void FetchTokens();
void StopFetchingTokens();
bool IsFetchingTokens() const;

/////////////////////////////////////////////////////////////////////////////
// persist these properties

Expand Down
1 change: 1 addition & 0 deletions src/static_values.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ static const uint64_t kRefillConfirmationsAfterSeconds = kOneHourInSeconds;
static const uint64_t kRetrievePaymentIOUsAfterSeconds =
5 * kOneMinuteInSeconds;
static const uint64_t kCashInPaymentIOUsAfterSeconds = kOneDayInSeconds;
static const uint64_t kRetryFetchingTokensAfterSeconds = 15;

} // namespace confirmations

Expand Down

0 comments on commit 0ef7164

Please sign in to comment.