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 all commits
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
27 changes: 22 additions & 5 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('request');
const request = require('retry-request');
// [END iot_http_includes]

console.log('Google Cloud IoT Core HTTP example.');
Expand Down Expand Up @@ -141,19 +141,28 @@ function publishAsync (authToken, messageCount, numMessages) {
binary_data: binaryData
}
};

const options = {
url: url,
headers: {
'authorization': `Bearer ${authToken}`,
'content-type': 'application/json',
'cache-control': 'no-cache'
},
body: postData,
json: true,
body: postData
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));
request(options, function (error, response, body) {
if (error) {
console.error('Received error: ', error);
} else if (response.body.error) {
Expand Down Expand Up @@ -182,6 +191,7 @@ function publishAsync (authToken, messageCount, numMessages) {
// [START iot_http_getconfig]
function getConfig (authToken, version) {
console.log(`Getting config from URL: ${urlBase}`);

const options = {
url: urlBase + '/config?local_version=' + version,
headers: {
Expand All @@ -190,9 +200,16 @@ function getConfig (authToken, version) {
'cache-control': 'no-cache'

},
json: true
json: true,
retries: 5,
shouldRetryFn:
function (incomingHttpMessage) {
console.log('Retry?');
return incomingHttpMessage.statusMessage !== 'OK';
}
};
request.get(options, function (error, response, body) {
console.log(JSON.stringify(request.RetryStrategies));
request(options, function (error, response, body) {
if (error) {
console.error('Received error: ', error);
} else if (response.body.error) {
Expand Down
6 changes: 3 additions & 3 deletions iot/http_example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
"@google-cloud/pubsub": "0.13.2",
"@google-cloud/nodejs-repo-tools": "1.4.17",
"ava": "0.22.0",
"yargs": "8.0.2",
"jsonwebtoken": "7.4.1",
"request": "2.82.0",
"uuid": "3.1.0"
"retry-request": "3.3.1",
"uuid": "3.1.0",
"yargs": "8.0.2"
},
"testDependencies": {
},
Expand Down