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

Improve http.Client connection handling #1581

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions base/builtin/registration.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ void $register_builtin() {
$register_force(MEMORYERROR_ID,&B_MemoryErrorG_methods);
$register_force(OSERROR_ID,&B_OSErrorG_methods);
$register_force(RUNTIMEERROR_ID,&B_RuntimeErrorG_methods);
$register_force(CONNECTIONERROR_ID,&B_ConnectionErrorG_methods);
$register_force(NOTIMPLEMENTEDERROR_ID,&B_NotImplementedErrorG_methods);
$register_force(VALUEERROR_ID,&B_ValueErrorG_methods);
// $register_builtin_protocols();
Expand Down
25 changes: 13 additions & 12 deletions base/builtin/registration.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,23 @@
#define KEYERROR_ID 39
#define MEMORYERROR_ID 40
#define OSERROR_ID 41
#define RUNTIMEERROR_ID 42
#define NOTIMPLEMENTEDERROR_ID 43
#define VALUEERROR_ID 44
#define CONNECTIONERROR_ID 42
#define RUNTIMEERROR_ID 43
#define NOTIMPLEMENTEDERROR_ID 44
#define VALUEERROR_ID 45

#define PROC_ID 45
#define ACTION_ID 46
#define MUT_ID 47
#define PURE_ID 48
#define PROC_ID 46
#define ACTION_ID 47
#define MUT_ID 48
#define PURE_ID 49

#define SEQ_ID 49
#define BRK_ID 50
#define CNT_ID 51
#define RET_ID 52
#define SEQ_ID 50
#define BRK_ID 51
#define CNT_ID 52
#define RET_ID 53


#define PREASSIGNED 53
#define PREASSIGNED 54


/*
Expand Down
3 changes: 3 additions & 0 deletions base/src/__builtin__.act
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,9 @@ class MemoryError (Exception):
class OSError (Exception):
pass

class ConnectionError (OSError):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hadn't actually looked much at how Python does things beyond looking at OSError. I see now that it subclasses things quite nicely. Should we not just copy that whole thing verbatim? Like it feels like a better starting point than starting from scratch. In particular, in this case for the error you later raise, I think it's a BrokenPipeError.

We probably want to consider changing the __init__ method of our OSError so we can take errno and stuff like that, again just like in Python. Without default args it'll be a little fiddly, but that's OK I guess.

pass

class RuntimeError (Exception):
pass

Expand Down
13 changes: 7 additions & 6 deletions base/src/http.act
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,10 @@ actor Client(cap: net.TCPConnectCap, scheme: str, address: str, port: ?int, tls_
if "connection" in r.headers and r.headers["connection"] == "close":
# Is this really the right thing to do here? If the client
Copy link
Contributor

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?

# does not make a new request we just reconnected for nothing!
# TODO: inspect the _on_response queue to see if there are more requests
# TODO: for HTTP/1.0 do not reconnect right after the first
# request, wait until the 2nd query and then assume there
# will be more.
_log.debug("Closing TCP connection due to header: Connection: close", None)
_conn_reconnect()
if len(_on_response) == 0:
Expand Down Expand Up @@ -553,13 +557,10 @@ actor Client(cap: net.TCPConnectCap, scheme: str, address: str, port: ?int, tls_
await async tcp_conn.write(data)
elif tls_conn is not None:
await async tls_conn.write(data)
except RuntimeError as exc:
except ConnectionError as exc:
# HTTP/1.0 servers close the connection after each request by default
if "bad file descriptor" in str(exc) or "bad stream" in str(exc):
_log.debug("TCP connection closed, reconnecting", {"error": str(exc)})
_conn_reconnect()
else:
_on_con_error(str(exc))
_log.debug("TCP connection closed, reconnecting", {"error": str(exc)})
_conn_reconnect()

# HTTP methods
def get(path: str, headers: dict[str, str], on_response: action(Client, Response) -> None):
Expand Down
12 changes: 6 additions & 6 deletions base/src/net.ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -275,13 +275,13 @@ void on_connect6(uv_connect_t *connect_req, int status) {
if (r < 0) {
char errmsg[1024] = "Failed to write to TCP socket: ";
uv_strerror_r(r, errmsg + strlen(errmsg), sizeof(errmsg)-strlen(errmsg));
log_warn(errmsg);
if (strstr(errmsg, "bad file descriptor")) {
// 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))));
if (r == UV_EBADF) {
// "bad file descriptor" error occurs when the socket is closed, se
// we raise an exception and let the caller retry
$RAISE(((B_BaseException)B_ConnectionErrorG_new(to$str(errmsg))));
return $R_CONT(c$cont, B_None);
}
log_warn(errmsg);
$action2 f = ($action2)self->on_error;
f->$class->__asyn__(f, self, to$str(errmsg));
}
Expand Down Expand Up @@ -661,7 +661,7 @@ void tls_write_cb(uv_write_t *wreq, int status) {
// 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))));
$RAISE(((B_BaseException)B_ConnectionErrorG_new(to$str(errmsg))));
return $R_CONT(c$cont, B_None);
}

Expand Down