-
Notifications
You must be signed in to change notification settings - Fork 720
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
base: master
Are you sure you want to change the base?
Conversation
Signed-off-by: Cabinfever_B <[email protected]>
[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 The full list of commands accepted by this bot can be found here. Reviewer can indicate their review by submitting an approval review. |
Skipping CI for Draft Pull Request. |
Codecov Report
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
Flags with carried forward coverage won't be shown. Click here to find out more. |
Signed-off-by: Cabinfever_B <[email protected]>
Signed-off-by: Cabinfever_B <[email protected]>
client/http/request_info.go
Outdated
} | ||
|
||
// newRequestInfo creates a new request info. | ||
func newRequestInfo() *requestInfo { | ||
return &requestInfo{} | ||
return &requestInfo{ | ||
timeout: defaultTimeout, |
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.
What do you think about setting a default timeout for the client and passing it through? Leave therequestInfo
always empty to customize.
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.
+1
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.
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:
- I just want to initialize an
http.Client
- The timeout value of
http.Client
used in TiDB is 0 - The timeout value of
http.Client
used in client-go is 30s
client/http/client.go
Outdated
@@ -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) |
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.
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?
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.
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]>
// 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 | ||
} |
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.
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?
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.
Or we could make the timeout an upper layer's behavior rather than the HTTP client configuration. Like a Context
with a timeout.
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.
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.
Signed-off-by: Cabinfever_B <[email protected]>
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. |
What problem does this PR solve?
Issue Number: ref #7300
What is changed and how does it work?
Check List
Tests
Code changes
Side effects
Related changes
pingcap/docs
/pingcap/docs-cn
:pingcap/tiup
:Release note