-
Notifications
You must be signed in to change notification settings - Fork 23
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
add timeout in http request #20
base: master
Are you sure you want to change the base?
Conversation
// set http connection timeout | ||
switch timeout := options.RawGet(lua.LString("timeout")).(type) { | ||
case lua.LNumber: | ||
h.client.Timeout = time.Duration(timeout) * time.Second |
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.
This is not the correct way to implement a per request timeout. When implemented like this it would mean setting a timeout for one request would also affect requests after it that don't explicitly set another timeout.
The correct way is to use the Request.WithContext and a WithTimeout context.
@@ -173,6 +174,11 @@ func (h *httpModule) doRequest(L *lua.LState, method string, url string, options | |||
req.Header.Set(key.String(), value.String()) | |||
}) | |||
} | |||
// set http connection timeout | |||
switch timeout := options.RawGet(lua.LString("timeout")).(type) { | |||
case lua.LNumber: |
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.
Can you also add support for LString using time.ParseDuration.
@@ -67,6 +67,7 @@ func main() { | |||
| query | String | URL encoded query params | | |||
| cookies | Table | Additional cookies to send with the request | | |||
| headers | Table | Additional headers to send with the request | | |||
| timeout | Number | request 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.
Please capitalize the first letter in "request" to match the style of the rest of the table.
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.
I'm extremely sorry that I just saw your reply now,I hope I can contribute to this project. I will follow your advice to modify pull request.
* Add timeout option for requests Based on an old PR at #20 that seems to have been deleted. The timeout option can be set to a number, which will be cast to an int and interpreted as seconds, or a string which will be parsed by time.ParseDuration. Add tests, one case should timeout and one that shouldn't. * Update readme with info about timeout option * Use WithContext() instead of Clone() Clone is not supported in older versions of Go and WithContext seems to work just as well. * Catch duration parsing errors, clean up imports Co-authored-by: Johannes Larsson <[email protected]>
* Add timeout option for requests Based on an old PR at cjoudrey#20 that seems to have been deleted. The timeout option can be set to a number, which will be cast to an int and interpreted as seconds, or a string which will be parsed by time.ParseDuration. Add tests, one case should timeout and one that shouldn't. * Update readme with info about timeout option * Use WithContext() instead of Clone() Clone is not supported in older versions of Go and WithContext seems to work just as well. * Catch duration parsing errors, clean up imports Co-authored-by: Johannes Larsson <[email protected]>
when I use the gluahttp in my project, I found that no param of http request timeout in the gluahttp.so I hope gluahttp can support this param. I have done the test.