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

refactor: make use of contexts in more places #1261

Merged
merged 3 commits into from
Nov 21, 2023

Conversation

ThinkChaos
Copy link
Collaborator

@ThinkChaos ThinkChaos commented Nov 19, 2023

  • CacheControl.FlushCaches
  • Querier.Query
  • Resolver.Resolve

Besides all the API churn, this leads to ParallelBestResolver,
StrictResolver and UpstreamResolver simplification: timeouts only
need to be setup in one place, upstreamResolverStatus.

We also benefit from using HTTP request contexts, so if the client
closes the connection we stop processing on our side.

I split the changes to make reviewing easier since most of the changes are just adding ctx to function definitions and calls. Here's the interesting commits:

I'd say it's easier to read the commits mentioned above individually, and then scan through the whole lot of changes more quickly.

Progress #1264

Copy link

codecov bot commented Nov 19, 2023

Codecov Report

Attention: 10 lines in your changes are missing coverage. Please review.

Comparison is base (1a1891c) 93.92% compared to head (be5fa5c) 93.78%.

❗ Current head be5fa5c differs from pull request most recent head 674bbce. Consider uploading reports for the commit 674bbce to get more accurate results

Files Patch % Lines
resolver/upstream_resolver.go 88.37% 4 Missing and 1 partial ⚠️
resolver/blocking_resolver.go 92.59% 1 Missing and 1 partial ⚠️
server/server_endpoints.go 33.33% 2 Missing ⚠️
resolver/caching_resolver.go 94.11% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1261      +/-   ##
==========================================
- Coverage   93.92%   93.78%   -0.14%     
==========================================
  Files          75       75              
  Lines        6041     6006      -35     
==========================================
- Hits         5674     5633      -41     
- Misses        284      289       +5     
- Partials       83       84       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@kwitsch
Copy link
Collaborator

kwitsch commented Nov 20, 2023

Since the redis package is also heavily context based and has the somewhat worst implementation(sorry for that 🫣) would you consider also refactoring it?

Or is this out of scope and should be done in a follow up PR?

@ThinkChaos ThinkChaos mentioned this pull request Nov 21, 2023
11 tasks
@ThinkChaos
Copy link
Collaborator Author

Yeah there's a couple other places where we should do a pass. I have more changes locally but didn't want to make a too giant PR since this is already big!
Made issue #1264 to track this.

@0xERR0R 0xERR0R added this to the v0.23 milestone Nov 21, 2023
@0xERR0R 0xERR0R added the 🧰 technical debts Technical debts, refactoring label Nov 21, 2023
Comment on lines +169 to +174
response, rtt, err = r.tcpClient.ExchangeContext(ctx, msg, upstreamURL)
if err != nil && r.udpClient != nil {
// try UDP as fallback
var opErr *net.OpError
if errors.As(err, &opErr) && opErr.Op == "dial" && r.udpClient != nil {
return r.udpClient.Exchange(msg, upstreamURL)
if errors.As(err, &opErr) && opErr.Op == "dial" {
return r.udpClient.ExchangeContext(ctx, msg, upstreamURL)
Copy link
Collaborator Author

@ThinkChaos ThinkChaos Nov 21, 2023

Choose a reason for hiding this comment

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

This changes the behavior: we now have a single timeout for both calls. Meaning if the first one fails because of a timeout, then the second one won't even really start because the context's deadline has already been reached.
I think the best solution would be to start both queries at the same time (or remember which protocol worked last time and use something like weighted random -- but that's more complex and more state).
That's more work than I want to do in this PR though, so I'll make an issue and get back to it.

In the mean I'll write a test to trigger that condition and do a very ugly thing: ignore the passed-in context for the second query.

Copy link
Collaborator

Choose a reason for hiding this comment

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

How about doubling the context timout and splitting it to different contexts for each request? 🤔

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

How about doubling the context timout and splitting it to different contexts for each request? 🤔

I'd rather not mess with the timeout value the user provides if we can.

When writing the test, I realized this isn't an issue: the only case we retry is in case of an error during "dial", aka connection refused which shouldn't take long so we'll be fine.
I still think racing TCP and UDP could be worth it and will create an issue.

@ThinkChaos ThinkChaos marked this pull request as draft November 21, 2023 14:24
@ThinkChaos ThinkChaos marked this pull request as ready for review November 21, 2023 17:10
- `CacheControl.FlushCaches`
- `Querier.Query`
- `Resolver.Resolve`

Besides all the API churn, this leads to `ParallelBestResolver`,
`StrictResolver` and `UpstreamResolver` simplification: timeouts only
need to be setup in one place, `UpstreamResolver`.

We also benefit from using HTTP request contexts, so if the client
closes the connection we stop processing on our side.
@ThinkChaos ThinkChaos merged commit 2c71f91 into 0xERR0R:main Nov 21, 2023
8 checks passed
@ThinkChaos ThinkChaos deleted the feat/resolve-ctx branch November 21, 2023 17:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🧰 technical debts Technical debts, refactoring
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants