Drop the MAX_CONNS macro and define TCP_CONNECTION_LIMIT, TCP_POLL_CAPACITY, TCP_EVENT_CAPACITY instead

This commit is contained in:
2025-11-22 22:59:32 +01:00
parent 582893be70
commit e1fa3729c6
10 changed files with 54 additions and 31 deletions
+3 -2
View File
@@ -188,8 +188,9 @@ void toasty_free_result(ToastyResult *result);
// OTHER // OTHER
////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////
// This is a hook for the simulation testing framework. #define TOASTY_POLL_CAPACITY 514
// You shouldn't need to use this.
// TODO: comment
int toasty_process_events(ToastyFS *toasty, void **contexts, int toasty_process_events(ToastyFS *toasty, void **contexts,
struct pollfd *polled, int num_polled); struct pollfd *polled, int num_polled);
+1 -1
View File
@@ -919,7 +919,7 @@ int chunk_server_free(ChunkServer *state)
int chunk_server_step(ChunkServer *state, void **contexts, struct pollfd *polled, int num_polled, int *timeout) int chunk_server_step(ChunkServer *state, void **contexts, struct pollfd *polled, int num_polled, int *timeout)
{ {
Event events[MAX_CONNS+1]; Event events[TCP_EVENT_CAPACITY];
int num_events = tcp_translate_events(&state->tcp, events, contexts, polled, num_polled); int num_events = tcp_translate_events(&state->tcp, events, contexts, polled, num_polled);
Time current_time = get_current_time(); Time current_time = get_current_time();
+5 -3
View File
@@ -2061,13 +2061,15 @@ unlock_and_exit:
return ret; return ret;
} }
_Static_assert(TCP_POLL_CAPACITY == TOASTY_POLL_CAPACITY);
int toasty_process_events(ToastyFS *toasty, void **contexts, struct pollfd *polled, int num_polled) int toasty_process_events(ToastyFS *toasty, void **contexts, struct pollfd *polled, int num_polled)
{ {
if (mutex_lock(&toasty->mutex) < 0) if (mutex_lock(&toasty->mutex) < 0)
return -1; return -1;
int num_events; int num_events;
Event events[MAX_CONNS+1]; Event events[TCP_EVENT_CAPACITY];
num_events = tcp_translate_events(&toasty->tcp, events, contexts, polled, num_polled); num_events = tcp_translate_events(&toasty->tcp, events, contexts, polled, num_polled);
for (int i = 0; i < num_events; i++) { for (int i = 0; i < num_events; i++) {
@@ -2199,8 +2201,8 @@ int toasty_wait_result(ToastyFS *toasty, ToastyHandle handle, ToastyResult *resu
return -1; return -1;
} }
void *contexts[MAX_CONNS+1]; void *contexts[TCP_POLL_CAPACITY];
struct pollfd polled[MAX_CONNS+1]; struct pollfd polled[TCP_POLL_CAPACITY];
int num_polled; int num_polled;
num_polled = toasty_process_events(toasty, contexts, polled, 0); num_polled = toasty_process_events(toasty, contexts, polled, 0);
+4 -4
View File
@@ -15,8 +15,8 @@
int metadata_server_main(int argc, char **argv) int metadata_server_main(int argc, char **argv)
{ {
void *contexts[MAX_CONNS+1]; void *contexts[TCP_POLL_CAPACITY];
struct pollfd polled[MAX_CONNS+1]; struct pollfd polled[TCP_POLL_CAPACITY];
int num_polled; int num_polled;
int timeout = -1; int timeout = -1;
MetadataServer state; MetadataServer state;
@@ -37,8 +37,8 @@ int metadata_server_main(int argc, char **argv)
int chunk_server_main(int argc, char **argv) int chunk_server_main(int argc, char **argv)
{ {
void *contexts[MAX_CONNS+1]; void *contexts[TCP_POLL_CAPACITY];
struct pollfd polled[MAX_CONNS+1]; struct pollfd polled[TCP_POLL_CAPACITY];
int num_polled; int num_polled;
int timeout = -1; int timeout = -1;
ChunkServer state; ChunkServer state;
+1 -1
View File
@@ -1040,7 +1040,7 @@ int metadata_server_free(MetadataServer *state)
int metadata_server_step(MetadataServer *state, void **contexts, struct pollfd *polled, int num_polled, int *timeout) int metadata_server_step(MetadataServer *state, void **contexts, struct pollfd *polled, int num_polled, int *timeout)
{ {
Event events[MAX_CONNS+1]; Event events[TCP_EVENT_CAPACITY];
int num_events = tcp_translate_events(&state->tcp, events, contexts, polled, num_polled); int num_events = tcp_translate_events(&state->tcp, events, contexts, polled, num_polled);
Time current_time = get_current_time(); Time current_time = get_current_time();
+6 -6
View File
@@ -343,8 +343,8 @@ int spawn_simulated_process(char *args)
process->desc[i].generation = 0; process->desc[i].generation = 0;
} }
void *contexts[MAX_CONNS+1]; void *contexts[TCP_POLL_CAPACITY];
struct pollfd polled[MAX_CONNS+1]; struct pollfd polled[TCP_POLL_CAPACITY];
int num_polled; int num_polled;
int timeout = -1; int timeout = -1;
@@ -711,8 +711,8 @@ void update_simulation(void)
current_process = processes[i]; current_process = processes[i];
void *contexts[MAX_CONNS+1]; void *contexts[TCP_POLL_CAPACITY];
struct pollfd polled[MAX_CONNS+1]; struct pollfd polled[TCP_POLL_CAPACITY];
int num_polled = setup_poll_array(contexts, polled); int num_polled = setup_poll_array(contexts, polled);
if (num_polled > 0) { if (num_polled > 0) {
@@ -787,8 +787,8 @@ void update_simulation(void)
current_process = next_process; current_process = next_process;
void *contexts[MAX_CONNS+1]; void *contexts[TCP_POLL_CAPACITY];
struct pollfd polled[MAX_CONNS+1]; struct pollfd polled[TCP_POLL_CAPACITY];
int num_polled = setup_poll_array(contexts, polled); int num_polled = setup_poll_array(contexts, polled);
int timeout = -1; int timeout = -1;
+5 -8
View File
@@ -271,16 +271,13 @@ int tcp_register_events(TCP *tcp, void **contexts, struct pollfd *polled)
{ {
int num_polled = 0; int num_polled = 0;
// TODO: Check that there is enough capacity in the polled array.
// At the moment only MAX_CONNS plus 1 for the listener are
// allowed.
polled[num_polled].fd = tcp->wait_fd; polled[num_polled].fd = tcp->wait_fd;
polled[num_polled].events = POLLIN; polled[num_polled].events = POLLIN;
polled[num_polled].revents = 0; polled[num_polled].revents = 0;
contexts[num_polled] = NULL; contexts[num_polled] = NULL;
num_polled++; num_polled++;
if (tcp->listen_fd != INVALID_SOCKET && tcp->num_conns < MAX_CONNS) { if (tcp->listen_fd != INVALID_SOCKET && tcp->num_conns < TCP_CONNECTION_LIMIT) {
polled[num_polled].fd = tcp->listen_fd; polled[num_polled].fd = tcp->listen_fd;
polled[num_polled].events = POLLIN; polled[num_polled].events = POLLIN;
polled[num_polled].revents = 0; polled[num_polled].revents = 0;
@@ -302,11 +299,11 @@ int tcp_register_events(TCP *tcp, void **contexts, struct pollfd *polled)
return num_polled; return num_polled;
} }
// The "events" array must be an array of capacity MAX_CONNS+1 // The "events" array must be an array of capacity TCP_EVENT_CAPACITY,
// TODO: Actually, it should have capacity MAX_CONNS+2 // while "contexts" and "polled" must have capacity TCP_POLL_CAPACITY.
int tcp_translate_events(TCP *tcp, Event *events, void **contexts, struct pollfd *polled, int num_polled) int tcp_translate_events(TCP *tcp, Event *events, void **contexts, struct pollfd *polled, int num_polled)
{ {
bool removed[MAX_CONNS+1]; bool removed[TCP_CONNECTION_LIMIT+1];
int num_events = 0; int num_events = 0;
for (int i = 1; i < num_polled; i++) { for (int i = 1; i < num_polled; i++) {
@@ -431,7 +428,7 @@ ByteQueue *tcp_output_buffer(TCP *tcp, int conn_idx)
int tcp_connect(TCP *tcp, Address addr, int tag, ByteQueue **output) int tcp_connect(TCP *tcp, Address addr, int tag, ByteQueue **output)
{ {
if (tcp->num_conns == MAX_CONNS) if (tcp->num_conns == TCP_CONNECTION_LIMIT)
return -1; return -1;
int conn_idx = tcp->num_conns; int conn_idx = tcp->num_conns;
+18 -2
View File
@@ -12,7 +12,23 @@
#define CLOSE_SOCKET sys_close #define CLOSE_SOCKET sys_close
#endif #endif
#define MAX_CONNS 512 #ifndef TCP_CONNECTION_LIMIT
// Maximum number of connections that can be managed
// simultaneously.
#define TCP_CONNECTION_LIMIT 512
#endif
// This is the maximum number of descriptors that the
// TCP system will want to wait at any given time.
// One descriptor per connection plus a listener socket
// and a self-pipe handle for wakeup.
#define TCP_POLL_CAPACITY (TCP_CONNECTION_LIMIT+2)
// Number of TCP events that can be returned at a given
// time by "tcp_translate_events". There may be a single
// event per connection (MESSAGE, CONNECT, DISCONNECT)
// plus a general WAKEUP event.
#define TCP_EVENT_CAPACITY (TCP_CONNECTION_LIMIT+1)
typedef enum { typedef enum {
EVENT_WAKEUP, EVENT_WAKEUP,
@@ -42,7 +58,7 @@ typedef struct {
SOCKET wait_fd; SOCKET wait_fd;
SOCKET signal_fd; SOCKET signal_fd;
int num_conns; int num_conns;
Connection conns[MAX_CONNS]; Connection conns[TCP_CONNECTION_LIMIT];
} TCP; } TCP;
bool addr_eql(Address a, Address b); bool addr_eql(Address a, Address b);
+11 -1
View File
@@ -526,8 +526,8 @@ int socket_manager_wakeup(SocketManager *sm);
typedef struct { typedef struct {
void **ptrs; void **ptrs;
struct pollfd *polled; struct pollfd *polled;
int max_polled;
int num_polled; int num_polled;
int max_polled;
} EventRegister; } EventRegister;
// Resets the event register with the list of descriptors // Resets the event register with the list of descriptors
@@ -764,6 +764,11 @@ int http_create_test_certificate(HTTP_String C, HTTP_String O, HTTP_String CN,
#define HTTP_CLIENT_CAPACITY (1<<7) #define HTTP_CLIENT_CAPACITY (1<<7)
#endif #endif
// Maximum number of descriptors the client will want
// to wait on. It's one per connection plus the wakeup
// self-pipe.
#define HTTP_CLIENT_POLL_CAPACITY (HTTP_CLIENT_CAPACITY+1)
typedef enum { typedef enum {
HTTP_CLIENT_CONN_FREE, HTTP_CLIENT_CONN_FREE,
HTTP_CLIENT_CONN_WAIT_LINE, HTTP_CLIENT_CONN_WAIT_LINE,
@@ -922,6 +927,11 @@ void http_free_response(HTTP_Response *response);
#define HTTP_SERVER_CAPACITY (1<<9) #define HTTP_SERVER_CAPACITY (1<<9)
#endif #endif
// Maximum number of descriptors the server will want
// to wait on. It's one per connection plus two for the
// TCP and TLS listener, plus one for the wakeup self-pipe.
#define HTTP_SERVER_POLL_CAPACITY (HTTP_SERVER_CAPACITY+3)
typedef enum { typedef enum {
// This struct is unused // This struct is unused
-3
View File
@@ -84,9 +84,6 @@ int main(void)
for (;;) { for (;;) {
#define HTTP_SERVER_POLL_CAPACITY (HTTP_SERVER_CAPACITY+3)
#define TOASTY_POLL_CAPACITY (MAX_CONNS+1)
#define POLL_CAPACITY (HTTP_SERVER_POLL_CAPACITY + TOASTY_POLL_CAPACITY) #define POLL_CAPACITY (HTTP_SERVER_POLL_CAPACITY + TOASTY_POLL_CAPACITY)
EventRegister reg; EventRegister reg;