Skip to content

Adds backoff / retry to HTTP calls. #545

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 9, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 16 additions & 37 deletions iot/http_example/cloudiot_http_example.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
// [START iot_http_includes]
const fs = require('fs');
const jwt = require('jsonwebtoken');
const request = require('requestretry');
const request = require('retry-request');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: can we name this something other than request (to avoid confusion with the library of the same name)? Maybe retryRequest?

// [END iot_http_includes]

console.log('Google Cloud IoT Core HTTP example.');
Expand Down Expand Up @@ -142,20 +142,6 @@ function publishAsync (authToken, messageCount, numMessages) {
}
};

// Custom backoff functions
function retryNotSuccess (err, response, body) {
if (err || response.statusCode !== 200) {
console.log(`Retrying publish on error: ${response.statusCode}`);
}
return response.statusCode !== 200;
}
function exponentialDelay (err, response, body) {
if (err) {
// No need to report again here.
}
return Math.floor(Math.random() * (3500 - 500 + 1) + 500);
}

const options = {
url: url,
headers: {
Expand All @@ -164,15 +150,19 @@ function publishAsync (authToken, messageCount, numMessages) {
'cache-control': 'no-cache'
},
body: postData,
delayStrategy: exponentialDelay,
json: true,
maxAttempts: 5,
retryStrategy: retryNotSuccess
method: 'POST',
retries: 5,
shouldRetryFn:
function (incomingHttpMessage) {
return incomingHttpMessage.statusMessage !== 'OK';
}
};

// Send events for high-frequency updates, update state only occasionally.
const delayMs = argv.messageType === 'events' ? 1000 : 2000;
request.post(options, function (error, response, body) {
console.log(JSON.stringify(request));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this intended?

request(options, function (error, response, body) {
if (error) {
console.error('Received error: ', error);
} else if (response.body.error) {
Expand Down Expand Up @@ -202,20 +192,6 @@ function publishAsync (authToken, messageCount, numMessages) {
function getConfig (authToken, version) {
console.log(`Getting config from URL: ${urlBase}`);

// Custom backoff functions
function retryNotSuccess (err, response, body) {
if (err || response.statusCode !== 200) {
console.log(`Retrying get config on error: ${response.statusCode}`);
}
return response.statusCode !== 200;
}
function exponentialDelay (err, response, body) {
if (err) {
// Reported in retry.
}
return Math.floor(Math.random() * (3500 - 500 + 1) + 500);
}

const options = {
url: urlBase + '/config?local_version=' + version,
headers: {
Expand All @@ -225,12 +201,15 @@ function getConfig (authToken, version) {

},
json: true,
maxAttempts: 5,
retryStrategy: retryNotSuccess,
delayStrategy: exponentialDelay
retries: 5,
shouldRetryFn:
function (incomingHttpMessage) {
console.log('Retry?');
return incomingHttpMessage.statusMessage !== 'OK';
}
};
console.log(JSON.stringify(request.RetryStrategies));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto - is this intended?

request.get(options, function (error, response, body) {
request(options, function (error, response, body) {
Copy link
Contributor

@ace-n ace-n Jan 5, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: arrow function?

(Apply this comment throughout your PR if you decide to make any changes.)

if (error) {
console.error('Received error: ', error);
} else if (response.body.error) {
Expand Down
2 changes: 1 addition & 1 deletion iot/http_example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"@google-cloud/nodejs-repo-tools": "1.4.17",
"ava": "0.22.0",
"jsonwebtoken": "7.4.1",
"requestretry": "requestretry",
"retry-request": "3.3.1",
"uuid": "3.1.0",
"yargs": "8.0.2"
},
Expand Down