Skip to content

Commit

Permalink
lib: msg: make short-circuit connections guaranteed
Browse files Browse the repository at this point in the history
When short circuiting is requested the caller can count on that
being the connection type they get.

Signed-off-by: Christian Hopps <[email protected]>
  • Loading branch information
choppsv1 committed Oct 12, 2023
1 parent 9bc4d9e commit 63f8463
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
19 changes: 9 additions & 10 deletions lib/mgmt_msg.c
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,7 @@ static void msg_client_sched_connect(struct msg_client *client,
&client->conn_retry_tmr);
}

static bool msg_client_connect_short_circuit(struct msg_client *client)
static int msg_client_connect_short_circuit(struct msg_client *client)
{
struct msg_conn *server_conn;
struct msg_server *server;
Expand All @@ -647,19 +647,18 @@ static bool msg_client_connect_short_circuit(struct msg_client *client)
if (!strcmp(server->sopath, client->sopath))
break;
if (!server) {
MGMT_MSG_DBG(dbgtag,
"no short-circuit connection available for %s",
MGMT_MSG_ERR(&client->conn.mstate,
"no short-circuit server available for %s",
client->sopath);

return false;
return -1;
}

if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockets)) {
MGMT_MSG_ERR(
&client->conn.mstate,
"socketpair failed trying to short-circuit connection on %s: %s",
client->sopath, safe_strerror(errno));
return false;
return -1;
}

/* client side */
Expand Down Expand Up @@ -687,7 +686,7 @@ static bool msg_client_connect_short_circuit(struct msg_client *client)
client->sopath, client->conn.mstate.idtag, client->conn.fd,
server_conn->mstate.idtag, server_conn->fd);

return true;
return 0;
}


Expand All @@ -697,11 +696,12 @@ static void msg_client_connect(struct msg_client *client)
struct msg_conn *conn = &client->conn;
const char *dbgtag = conn->debug ? conn->mstate.idtag : NULL;

if (!client->short_circuit_ok ||
!msg_client_connect_short_circuit(client))
if (!client->short_circuit_ok)
conn->fd =
mgmt_msg_connect(client->sopath, MSG_CONN_SEND_BUF_SIZE,
MSG_CONN_RECV_BUF_SIZE, dbgtag);
else if (msg_client_connect_short_circuit(client))
conn->fd = -1;

if (conn->fd == -1)
/* retry the connection */
Expand Down Expand Up @@ -741,7 +741,6 @@ void msg_client_init(struct msg_client *client, struct event_loop *tm,
mgmt_msg_init(&conn->mstate, max_read_buf, max_write_buf, max_msg_sz,
idtag);

/* XXX maybe just have client kick this off */
/* Start trying to connect to server */
msg_client_sched_connect(client, 0);
}
Expand Down
4 changes: 4 additions & 0 deletions lib/mgmt_msg.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ struct msg_client {
extern void msg_client_cleanup(struct msg_client *client);

/*
* If `short_circuit_ok` is true, then the client-server connection will use a
* socketpair() rather than a unix-domain socket. This must be passed true if
* you wish to send messages short-circuit later.
*
* `notify_disconnect` is not called when the user `msg_client_cleanup` is
* called for a client which is currently connected. The socket is closed
* but there is no notification.
Expand Down

0 comments on commit 63f8463

Please sign in to comment.