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

add timeout in http request #20

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

add timeout in http request #20

wants to merge 5 commits into from

Conversation

jssyjam
Copy link

@jssyjam jssyjam commented Jan 17, 2018

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.

// set http connection timeout
switch timeout := options.RawGet(lua.LString("timeout")).(type) {
case lua.LNumber:
h.client.Timeout = time.Duration(timeout) * time.Second
Copy link
Collaborator

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:
Copy link
Collaborator

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 |
Copy link
Collaborator

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.

Copy link
Author

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.

erikdubbelboer pushed a commit that referenced this pull request Jun 26, 2020
* 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]>
fatalbanana pushed a commit to fatalbanana/gluahttp that referenced this pull request Oct 18, 2023
* 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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants