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

Handle HTTP calls asynchronously, add response caching #163

Merged
merged 6 commits into from
Jun 21, 2023
Merged

Conversation

m90
Copy link
Contributor

@m90 m90 commented Jun 20, 2023

Ticket https://phabricator.wikimedia.org/T339906

Having asynchronous resolvers in redbird is not documented, but is done in the tests: https://github.com/OptimalBits/redbird/blob/cb7781e9c471bcb497ea4af1ddabf4fdf4432caa/test/test_custom_resolver.js#L225-L233 so I would assume this works as expected.

I tried to follow the existing behavior as closely as possible, which is why the code is still a bit quirky in places. For review, it might help to look at the commits which isolate the two changes munged together into this PR.

I was able to test these changes successfully in my local env.

return null
}

let backend = response.data.wiki_queryservice_namespace.backend
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We could also cache this computation. I steered away from it as it would mean even more noise in this changeset. Any opinions?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is probably the right call. IMHO if we start trying to optimise this computation we might also want to use native URL parsing etc. https://nodejs.org/api/url.html before adding the complexity of more caching.

// Try and log on failures (so this process keeps running...)
try {
var responseCache = new LRUCache({
ttl: parseInt(process.env.RESPONSE_CACHE_TTL || 60 * 60, 10) * 1000
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This needs a chart update. Any opinions on how long to cache such an item? I'd think 60 minutes is a good value to have.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

60mins seems fine to me. These mappings are currently very stable. We have never changed them so from historical data these could be cache indefinitely but 60mins means that we shan't have to worry too much if for some reason we do change them (e.g. if in future we allow reusing domains)

@m90
Copy link
Contributor Author

m90 commented Jun 20, 2023

Seems returning a Promise from a resolver is expected and would work just like the synchronous version before https://github.com/OptimalBits/redbird/blob/cb7781e9c471bcb497ea4af1ddabf4fdf4432caa/lib/proxy.js#L578

resolver.js Show resolved Hide resolved
resolver.js Outdated Show resolved Hide resolved
Copy link
Contributor

@tarrow tarrow left a comment

Choose a reason for hiding this comment

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

looks good!

@m90 m90 merged commit c28d975 into main Jun 21, 2023
4 checks passed
@m90 m90 deleted the fr/async-request branch June 21, 2023 10:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants