Skip to content
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

Avoid inadvertently invoking unwrapped request in vendor helper functions #924

Merged
merged 5 commits into from
Sep 22, 2017
Merged

Avoid inadvertently invoking unwrapped request in vendor helper functions #924

merged 5 commits into from
Sep 22, 2017

Conversation

paulmelnikow
Copy link
Member

@paulmelnikow paulmelnikow commented Mar 31, 2017

  • Tweak a couple names to improve readability

Tested locally by manually checking teamcity, and running my experimental recorded tests which cover travis.

- Tweak a couple names to improve readability
@paulmelnikow paulmelnikow added developer-experience Dev tooling, test framework, and CI performance-improvement Related to performance or throughput of the badge servers labels Mar 31, 2017
@paulmelnikow
Copy link
Member Author

Let me take a moment to explain these changes.

The cache function gets run millions of times a day, so clearly it works. 😄

It's 100+ lines, with no tests or doc, so it's difficult to read and reason about. The first thing I tried was finding where its argument was used, which was difficult because it was named f. Then I wondered, is this only used for json requests? No, it's behaving the same as request typically does. Would be clearer with the parameter name from their signature.

All the vendor badges invoke cache with a callback that takes a request parameter. This isn't require('request'), it's the caching request function which is created in cache. Devs might copy and paste code without realizing this, which is fine, though when code is factored into helper functions, as happens occasionally, they might not realize the request they are using has become request = require('request') assigned at the top of the file. That's what happened here with teamcity. Pulling it out of the module scope will prevent this error.

server.js Outdated
@@ -177,7 +176,7 @@ vendorDomain.on('error', function(err) {
});


function cache(f) {
function cache(wrappedFn) {
Copy link
Member

Choose a reason for hiding this comment

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

That's a clearer name. We can probably make it clearer still. What do you think about vendorProcessing?

Copy link
Member Author

Choose a reason for hiding this comment

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

I'd be good with vendorProcessingFn.

Copy link
Member Author

Choose a reason for hiding this comment

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

Or vendorRequestHandler.

@@ -236,7 +235,8 @@ function cache(f) {

// Only call vendor servers when last request is older than…
var cacheInterval = 5000; // milliseconds
var cachedRequest = function (uri, options, callback) {
var cachingRequest = function (uri, options, callback) {
var request = require('request');
Copy link
Member

Choose a reason for hiding this comment

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

Why do you want to require request every time we need it, instead of once and for all?

If you're worried about mistakenly calling it, we can change its name to httpRequest or something like that.

Copy link
Member Author

Choose a reason for hiding this comment

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

We only use it twice. It seems clearer to require it both times than introduce a new name. I don't feel that strongly though.

@paulmelnikow paulmelnikow merged commit eefca67 into badges:master Sep 22, 2017
@paulmelnikow paulmelnikow deleted the request_tweak branch September 22, 2017 18:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
developer-experience Dev tooling, test framework, and CI performance-improvement Related to performance or throughput of the badge servers
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants