Skip to content
This repository has been archived by the owner on Mar 4, 2024. It is now read-only.

Commit

Permalink
Add review suggestions
Browse files Browse the repository at this point in the history
Signed-off-by: Norbert Heusser <[email protected]>
  • Loading branch information
NorbertHeusser committed Aug 31, 2022
1 parent c4e3819 commit b44f904
Showing 1 changed file with 17 additions and 25 deletions.
42 changes: 17 additions & 25 deletions src/uv_tcp_connect.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ struct uvTcpConnect
struct uv_getaddrinfo_s getaddrinfo; /* DNS resolve request */
struct uv_connect_s connect; /* TCP connection request */
struct uv_write_s write; /* TCP handshake request */
uv_check_t delayedtcpclose; /* A check handle required to delay closing tcp */
int status; /* Returned to the request callback */
queue queue; /* Pending connect queue */
bool resolving; /* Indicate name resolving in progress */
queue queue; /* Pending connect queue */
};

/* Encode an handshake message into the given buffer. */
Expand Down Expand Up @@ -66,7 +66,6 @@ static void uvTcpConnectFinish(struct uvTcpConnect *connect)
struct raft_uv_connect *req = connect->req;
int status = connect->status;
QUEUE_REMOVE(&connect->queue);
uv_close((struct uv_handle_s *)&connect->delayedtcpclose, NULL);
RaftHeapFree(connect->handshake.base);
uv_freeaddrinfo(connect->getaddrinfo.addrinfo);
raft_free(connect);
Expand All @@ -87,27 +86,16 @@ static void uvTcpConnectUvCloseCb(struct uv_handle_s *handle)
UvTcpMaybeFireCloseCb(t);
}

static void uvTcpConnectCheckMayClose(uv_check_t *handle)
{
struct uvTcpConnect *connect = handle->data;
if (connect->status || uv_is_active((uv_handle_t *)connect->tcp)) {
uv_check_stop(&connect->delayedtcpclose);
uv_close((struct uv_handle_s *)connect->tcp, uvTcpConnectUvCloseCb);
}
}

/* Abort a connection request. */
static void uvTcpConnectAbort(struct uvTcpConnect *connect)
{
QUEUE_REMOVE(&connect->queue);
QUEUE_PUSH(&connect->t->aborting, &connect->queue);
if (uv_cancel((struct uv_req_s *)&connect->getaddrinfo) == 0 ||
!uv_is_active((uv_handle_t *)connect->tcp)) {
/* If canceling the addrinfo call was not successfull, but the tcp
handle is not active the getaddrinfo is in progress and we need to
delay closing the tcp handle until it's finished */
uv_check_start(&connect->delayedtcpclose, uvTcpConnectCheckMayClose);
} else {
uv_cancel((struct uv_req_s *)&connect->getaddrinfo);
/* Only call uv_close in the tcp handle, if there is no getaddrinfo request
in flight. data structures may only be freed after the uvGetAddrInfoCb was
triggered. tcp handle will be closed in the uvGetAddrInfoCb in this case. */
if (!connect->resolving) {
uv_close((struct uv_handle_s *)connect->tcp, uvTcpConnectUvCloseCb);
}
}
Expand Down Expand Up @@ -167,6 +155,7 @@ static void uvTcpConnectUvConnectCb(struct uv_connect_s *req, int status)
uvTcpConnectAbort(connect);
}

/* The hostname resolve is finished */
static void uvGetAddrInfoCb(uv_getaddrinfo_t *req,
int status,
struct addrinfo *res)
Expand All @@ -175,8 +164,14 @@ static void uvGetAddrInfoCb(uv_getaddrinfo_t *req,
struct UvTcp *t = connect->t;
int rv;

if (t->closing || status == UV_ECANCELED) {
connect->resolving =
false; /* INdicate we are in the name resolving phase */

if (t->closing) {
connect->status = RAFT_CANCELED;

/* We need to close the tcp handle to to connection attempt */
uv_close((struct uv_handle_s *)connect->tcp, uvTcpConnectUvCloseCb);
return;
}

Expand Down Expand Up @@ -233,9 +228,6 @@ static int uvTcpConnectStart(struct uvTcpConnect *r, const char *address)
goto err;
}

rv = uv_check_init(t->loop, &r->delayedtcpclose);
assert(rv == 0);

rv = uv_tcp_init(r->t->loop, r->tcp);
assert(rv == 0);
r->tcp->data = r;
Expand All @@ -258,12 +250,12 @@ static int uvTcpConnectStart(struct uvTcpConnect *r, const char *address)
rv = RAFT_NOCONNECTION;
goto err_after_tcp_init;
}
r->resolving = true; /* INdicate we are in the name resolving phase */

return 0;

err_after_tcp_init:
uv_close((uv_handle_t *)r->tcp, (uv_close_cb)RaftHeapFree);
uv_close((uv_handle_t *)&r->delayedtcpclose, NULL);

err:
RaftHeapFree(r->handshake.base);
Expand Down Expand Up @@ -295,8 +287,8 @@ int UvTcpConnect(struct raft_uv_transport *transport,
r->status = 0;
r->write.data = r;
r->getaddrinfo.data = r;
r->resolving = false;
r->connect.data = r;
r->delayedtcpclose.data = r;
req->cb = cb;

/* Keep track of the pending request */
Expand Down

0 comments on commit b44f904

Please sign in to comment.