-
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?
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ package http | |
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/tikv/pd/client/retry" | ||
) | ||
|
@@ -88,11 +89,14 @@ type requestInfo struct { | |
res any | ||
respHandler respHandleFunc | ||
bo *retry.Backoffer | ||
timeout time.Duration | ||
} | ||
|
||
// 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 commentThe 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 the There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. Do you mean that it is better to set default timeout for
|
||
} | ||
} | ||
|
||
// WithCallerID sets the caller ID of the request. | ||
|
@@ -143,6 +147,14 @@ func (ri *requestInfo) WithBackoffer(bo *retry.Backoffer) *requestInfo { | |
return ri | ||
} | ||
|
||
// WithTimeout sets the timeout of the request. | ||
func (ri *requestInfo) WithTimeout(dur time.Duration) *requestInfo { | ||
if dur > 0 { | ||
ri.timeout = dur | ||
} | ||
return ri | ||
} | ||
|
||
func (ri *requestInfo) getURL(addr string) string { | ||
return fmt.Sprintf("%s%s", addr, ri.uri) | ||
} |
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.