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

client: support to set timeout in http client #7847

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

CabinfeverB
Copy link
Member

@CabinfeverB CabinfeverB commented Feb 22, 2024

What problem does this PR solve?

Issue Number: ref #7300

What is changed and how does it work?

  1. don't set Timeout when create http.Clent
  2. add timeout field in PD http client, and set context timeout

Check List

Tests

  • Unit test
  • Integration test

Code changes

Side effects

  • Possible performance regression
  • Increased code complexity
  • Breaking backward compatibility

Related changes

Release note

None.

Copy link
Contributor

ti-chi-bot bot commented Feb 22, 2024

[REVIEW NOTIFICATION]

This pull request has not been approved.

To complete the pull request process, please ask the reviewers in the list to review by filling /cc @reviewer in the comment.
After your PR has acquired the required number of LGTMs, you can assign this pull request to the committer in the list by filling /assign @committer in the comment to help you merge this pull request.

The full list of commands accepted by this bot can be found here.

Reviewer can indicate their review by submitting an approval review.
Reviewer can cancel approval by submitting a request changes review.

Copy link
Contributor

ti-chi-bot bot commented Feb 22, 2024

Skipping CI for Draft Pull Request.
If you want CI signal for your change, please convert it to an actual PR.
You can still manually trigger a test run with /test all

@ti-chi-bot ti-chi-bot bot added do-not-merge/needs-linked-issue do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. release-note-none Denotes a PR that doesn't merit a release note. labels Feb 22, 2024
@ti-chi-bot ti-chi-bot bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Feb 22, 2024
Copy link

codecov bot commented Feb 22, 2024

Codecov Report

Merging #7847 (2dce385) into master (7179657) will decrease coverage by 0.21%.
The diff coverage is 100.00%.

❗ Current head 2dce385 differs from pull request most recent head d181242. Consider uploading reports for the commit d181242 to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #7847      +/-   ##
==========================================
- Coverage   73.68%   73.48%   -0.21%     
==========================================
  Files         436      432       -4     
  Lines       48525    47928     -597     
==========================================
- Hits        35754    35218     -536     
+ Misses       9723     9666      -57     
+ Partials     3048     3044       -4     
Flag Coverage Δ
unittests 73.48% <100.00%> (-0.21%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Signed-off-by: Cabinfever_B <[email protected]>
@ti-chi-bot ti-chi-bot bot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Feb 23, 2024
@CabinfeverB CabinfeverB marked this pull request as ready for review February 23, 2024 09:57
@ti-chi-bot ti-chi-bot bot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Feb 23, 2024
@CabinfeverB CabinfeverB requested review from rleungx and HuSharp and removed request for HunDunDM February 23, 2024 09:58
Signed-off-by: Cabinfever_B <[email protected]>
@ti-chi-bot ti-chi-bot bot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. do-not-merge/needs-linked-issue labels Feb 23, 2024
}

// newRequestInfo creates a new request info.
func newRequestInfo() *requestInfo {
return &requestInfo{}
return &requestInfo{
timeout: defaultTimeout,
Copy link
Member

Choose a reason for hiding this comment

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

What do you think about setting a default timeout for the client and passing it through? Leave therequestInfo always empty to customize.

Copy link
Member

Choose a reason for hiding this comment

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

+1

Copy link
Member Author

Choose a reason for hiding this comment

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

Do you mean that it is better to set default timeout for http.Client?
I can have a try, but we should replace innter client to do it.
The reason why I made this change is because:

  1. I just want to initialize an http.Client
  2. The timeout value of http.Client used in TiDB is 0
  3. The timeout value of http.Client used in client-go is 30s

@CabinfeverB CabinfeverB changed the title client: support timeout in http client client: support to set timeout in http client Feb 26, 2024
@@ -181,6 +183,8 @@ func (ci *clientInner) doRequest(
zap.String("caller-id", callerID),
}
log.Debug("[pd] request the http url", logFields...)
ctx, cancel = context.WithTimeout(ctx, reqInfo.timeout)
Copy link
Contributor

@niubell niubell Feb 26, 2024

Choose a reason for hiding this comment

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

Set a timeout for each request may not be a good idea. How about just supporting only one way to set timeout: init timeout in client with WithTimeout function, and timeout mechanism also through Timeout in http.Client/http.Transport?

Copy link
Member Author

Choose a reason for hiding this comment

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

Got it. My initial idea was to create as few http.clients as possible, but I've found that this is not a good way to modify it.

Signed-off-by: Cabinfever_B <[email protected]>
Comment on lines +370 to +375
// WithTimeout sets and returns a new client with a new `http.Client` whose timeout is the given one.
func (c *client) WithTimeout(dur time.Duration) Client {
newClient := *c
newClient.inner = c.inner.clone(withInnerTimeout(dur))
return &newClient
}
Copy link
Member

Choose a reason for hiding this comment

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

The initial purpose of introducing clientInner is to allow safe sharing of the same inner core, even with multiple Client interfaces. This ensures that common components such as service discovery are reused effectively. However, this constraint appears to be violated. What about just keeping the timeout option as an initialization parameter instead of a dynamic variable?

Copy link
Member

@JmPotato JmPotato Feb 27, 2024

Choose a reason for hiding this comment

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

Or we could make the timeout an upper layer's behavior rather than the HTTP client configuration. Like a Context with a timeout.

Copy link
Member Author

Choose a reason for hiding this comment

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

Currently, Common components don't rely on clientInner(http.Client). So I think it's safe to clone the clientInner. However, I do not consider this parameter to be a dynamic variable because it creates a new clientInner without affecting the previous clientInner.

In fact, the reason of why I do this is because the timeout of http. Client in the store helper in TiDB is 0. However, the timeout value of http. Client in client-go is 30 seconds. Setting a default timeout for http.Client eliminates the need to set a timeout in context each time for most requests.

@ti-chi-bot ti-chi-bot bot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Apr 2, 2024
Signed-off-by: Cabinfever_B <[email protected]>
@ti-chi-bot ti-chi-bot bot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Apr 7, 2024
Copy link
Contributor

ti-chi-bot bot commented Jul 5, 2024

PR needs rebase.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@ti-chi-bot ti-chi-bot bot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jul 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. release-note-none Denotes a PR that doesn't merit a release note. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants