Skip to content

Commit

Permalink
[#473] Fix connection status output
Browse files Browse the repository at this point in the history
`pgagroal-cli` was reporting the wrong status for connections because
it was decoding the connection status via
`pgagroal_server_state_as_string` that is used to decode the status of
a server.
This commit renames the already existent function
`pgagroal_get_state_string` as `pgagroal_connection_state_as_string`
for consistency with the server
one (`pgagroal_server_state_as_string``) and makes the management
decoding using such function to get the effective status of the
connection.
Since the function `pgagroal_connection_state_as_string` keeps the
already existent statuses, the text output is not changed.
However, in the case `Unknwon` is reported, the function places also
the numeric constant in the output so that it will be easy to debug
edge cases.

Close #473
  • Loading branch information
fluca1978 committed Sep 26, 2024
1 parent 40acff6 commit bcd6c68
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 23 deletions.
19 changes: 11 additions & 8 deletions src/include/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,6 @@ pgagroal_extract_message(char type, struct message* msg, struct message** extrac
int
pgagroal_extract_error_message(struct message* msg, char** error);

/**
* Get a string for the state
* @param state
* @return The string
*/
char*
pgagroal_get_state_string(signed char state);

/**
* Read a byte
* @param data Pointer to the data
Expand Down Expand Up @@ -524,6 +516,17 @@ parse_command(int argc,
char*
pgagroal_server_state_as_string(signed char state);

/**
* Utility function to convert the status of a connection
* into a descriptive string. Useful to spurt the status
* in command line output.
*
* @param state the actual state of the connection
* @returns the (allocated) buffer with the string
*/
char*
pgagroal_connection_state_as_string(signed char state);

#ifdef __cplusplus
}
#endif
Expand Down
2 changes: 1 addition & 1 deletion src/libpgagroal/management.c
Original file line number Diff line number Diff line change
Expand Up @@ -948,7 +948,7 @@ pgagroal_management_json_read_status_details(SSL* ssl, int socket, bool include_
cJSON* current_connection_json = cJSON_CreateObject();

cJSON_AddNumberToObject(current_connection_json, "number", i + 1);
cJSON_AddStringToObject(current_connection_json, "state", pgagroal_server_state_as_string(state));
cJSON_AddStringToObject(current_connection_json, "state", pgagroal_connection_state_as_string(state));
cJSON_AddStringToObject(current_connection_json, "time", time > 0 ? ts : "");
cJSON_AddStringToObject(current_connection_json, "pid", pid > 0 ? p : "");
cJSON_AddStringToObject(current_connection_json, "fd", fd > 0 ? f : "");
Expand Down
24 changes: 10 additions & 14 deletions src/libpgagroal/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,11 @@ pgagroal_extract_error_message(struct message* msg, char** error)
}

char*
pgagroal_get_state_string(signed char state)
pgagroal_connection_state_as_string(signed char state)
{
char* buf;
int buf_size = strlen("Unknown") + 1 + 4 + 1; // 'unknown' + <space> + <number> + \0

switch (state)
{
case STATE_NOTINIT:
Expand All @@ -273,9 +276,12 @@ pgagroal_get_state_string(signed char state)
return "Validating";
case STATE_REMOVE:
return "Removing";
default:
buf = malloc(buf_size);
memset(buf, 0, buf_size);
snprintf(buf, buf_size, "Unknown %02d", state);
return buf;
}

return "Unknown";
}

signed char
Expand Down Expand Up @@ -1098,16 +1104,6 @@ parse_command(int argc,
#undef EMPTY_STR
}

/**
* Given a server state, it returns a string that
* described the state in a human-readable form.
*
* If the state cannot be determined, the numeric
* form of the state is returned as a string.
*
* @param state the value of the sate for the server
* @returns the string representing the state
*/
char*
pgagroal_server_state_as_string(signed char state)
{
Expand Down Expand Up @@ -1167,4 +1163,4 @@ pgagroal_compare_string(const char* str1, const char* str2)
return false;
}
return strcmp(str1, str2) == 0;
}
}

0 comments on commit bcd6c68

Please sign in to comment.