-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Changes from 2 commits
cf1da6a
3183147
d3a9fb8
be80033
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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.'); | ||
|
@@ -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)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
|
@@ -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: { | ||
|
@@ -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)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto - is this intended? |
||
request(options, function (error, response, body) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
|
There was a problem hiding this comment.
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)? MayberetryRequest?