-
Notifications
You must be signed in to change notification settings - Fork 7
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
Improve http.Client connection handling #1581
base: main
Are you sure you want to change the base?
Changes from 1 commit
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 |
---|---|---|
|
@@ -276,6 +276,12 @@ void on_connect6(uv_connect_t *connect_req, int status) { | |
char errmsg[1024] = "Failed to write to TCP socket: "; | ||
uv_strerror_r(r, errmsg + strlen(errmsg), sizeof(errmsg)-strlen(errmsg)); | ||
log_warn(errmsg); | ||
mzagozen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (strstr(errmsg, "bad file descriptor")) { | ||
mzagozen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// This can happen if the socket is closed, se we raise an exception | ||
// and let the caller retry | ||
$RAISE(((B_BaseException)B_RuntimeErrorG_new(to$str(errmsg)))); | ||
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. We are generally doing Python-like-exception stuff. It's not a specific design, i.e. we can deviate where we think there is good reason but if we have no idea we might as well follow Python. I think Python uses OSError for most of these, not RunTimeError. |
||
return $R_CONT(c$cont, B_None); | ||
} | ||
$action2 f = ($action2)self->on_error; | ||
f->$class->__asyn__(f, self, to$str(errmsg)); | ||
} | ||
|
@@ -311,6 +317,9 @@ static void after_shutdown(uv_shutdown_t* req, int status) { | |
uv_stream_t *stream = (uv_stream_t *)from$int(self->_sock); | ||
// fd == -1 means invalid FD and can happen after __resume__ | ||
if (stream == -1) | ||
// TODO: should we dispatch the on_close callback here too even though | ||
// we did not close anything? This is what we do for TLSConnection too | ||
// and it allows for chaining the callbacks | ||
return $R_CONT(c$cont, B_None); | ||
|
||
log_debug("Closing TCP connection"); | ||
|
@@ -629,9 +638,11 @@ void tls_write_cb(uv_write_t *wreq, int status) { | |
|
||
$R netQ_TLSConnectionD_closeG_local (netQ_TLSConnection self, $Cont c$cont, $action on_close) { | ||
uv_stream_t *stream = (uv_stream_t *)from$int(self->_stream); | ||
// fd == -1 means invalid FD and can happen after __resume__ | ||
if (stream == -1) | ||
// fd == -1 means invalid FD and can happen after __resume__ or if the socket is closed | ||
if (stream == -1) { | ||
on_close->$class->__asyn__(on_close, self); | ||
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. We initially thought about raising an exception here instead, but 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. I don't think you should blindly accept async write as the correct thing. It's not a complete design as-is... 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. Actually, for close, I'm not sure we even need to close on_close. Like why do we end up here with a -1 stream? If you call The stream must have gone to -1 at some earlier point at which the on_error() callback should be called, right? 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. Calling def reconnect():
close(_connect) |
||
return $R_CONT(c$cont, B_None); | ||
} | ||
|
||
self->_on_close = on_close; | ||
|
||
|
@@ -645,9 +656,14 @@ void tls_write_cb(uv_write_t *wreq, int status) { | |
|
||
$R netQ_TLSConnectionD_writeG_local (netQ_TLSConnection self, $Cont c$cont, B_bytes data) { | ||
uv_stream_t *stream = (uv_stream_t *)from$int(self->_stream); | ||
// fd == -1 means invalid FD and can happen after __resume__ | ||
if (stream == -1) | ||
// fd == -1 means invalid FD and can happen after __resume__ or if the socket is closed | ||
if (stream == -1) { | ||
// Raise an exception and let the caller retry | ||
char errmsg[] = "Failed to write to TLS TCP socket: bad stream"; | ||
log_debug(errmsg); | ||
$RAISE(((B_BaseException)B_RuntimeErrorG_new(to$str(errmsg)))); | ||
return $R_CONT(c$cont, B_None); | ||
} | ||
|
||
uv_write_t *wreq = (uv_write_t *)malloc(sizeof(uv_write_t)); | ||
wreq->data = self; | ||
|
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.
Add a TODO entry here. We should inspect our queue, i.e. _on_response list to see if there is more stuff.
I'm also thinking like we can maybe have some basic heuristics, like when we create a http.Client the first time we obviously connect directly then we expect to run at least one query. Maybe we should not reconnect after that (in case of HTTP / 1.0 or not having persistent in later HTTP versions) until there is a second query. Now if we see a second query we can assume "there are multiple requests" and thus reconnect directly after the 2nd and subsequent requests. WDYT?