Add max nb requests per client

This commit is contained in:
Grégory Soutadé 2016-01-31 11:49:24 +01:00
parent 446c5b0461
commit 9f880f8705
5 changed files with 35 additions and 2 deletions

View File

@ -47,6 +47,7 @@ const char *gengetopt_args_info_help[] = {
"\nAdvanced daemon options:",
" -s, --sockets-per-thread=INT Number of sockets managed by a single thread\n (default=`10')",
" -t, --sockets-timeout=INT Close connection after X seconds (default=`5')",
" -r, --client-max-requests=INT Number of requests allowed for one client\n (default=`100')",
0
};
@ -82,6 +83,7 @@ void clear_given (struct gengetopt_args_info *args_info)
args_info->bind_ip_given = 0 ;
args_info->sockets_per_thread_given = 0 ;
args_info->sockets_timeout_given = 0 ;
args_info->client_max_requests_given = 0 ;
}
static
@ -101,6 +103,8 @@ void clear_args (struct gengetopt_args_info *args_info)
args_info->sockets_per_thread_orig = NULL;
args_info->sockets_timeout_arg = 5;
args_info->sockets_timeout_orig = NULL;
args_info->client_max_requests_arg = 100;
args_info->client_max_requests_orig = NULL;
}
@ -119,6 +123,7 @@ void init_args_info(struct gengetopt_args_info *args_info)
args_info->bind_ip_help = gengetopt_args_info_help[9] ;
args_info->sockets_per_thread_help = gengetopt_args_info_help[11] ;
args_info->sockets_timeout_help = gengetopt_args_info_help[12] ;
args_info->client_max_requests_help = gengetopt_args_info_help[13] ;
}
@ -209,6 +214,7 @@ cmdline_parser_release (struct gengetopt_args_info *args_info)
free_string_field (&(args_info->bind_ip_orig));
free_string_field (&(args_info->sockets_per_thread_orig));
free_string_field (&(args_info->sockets_timeout_orig));
free_string_field (&(args_info->client_max_requests_orig));
@ -259,6 +265,8 @@ cmdline_parser_dump(FILE *outfile, struct gengetopt_args_info *args_info)
write_into_file(outfile, "sockets-per-thread", args_info->sockets_per_thread_orig, 0);
if (args_info->sockets_timeout_given)
write_into_file(outfile, "sockets-timeout", args_info->sockets_timeout_orig, 0);
if (args_info->client_max_requests_given)
write_into_file(outfile, "client-max-requests", args_info->client_max_requests_orig, 0);
i = EXIT_SUCCESS;
@ -523,10 +531,11 @@ cmdline_parser_internal (
{ "bind-ip", 1, NULL, 'b' },
{ "sockets-per-thread", 1, NULL, 's' },
{ "sockets-timeout", 1, NULL, 't' },
{ "client-max-requests", 1, NULL, 'r' },
{ 0, 0, 0, 0 }
};
c = getopt_long (argc, argv, "hVi:qvDp:b:s:t:", long_options, &option_index);
c = getopt_long (argc, argv, "hVi:qvDp:b:s:t:r:", long_options, &option_index);
if (c == -1) break; /* Exit from `while (1)' loop. */
@ -632,6 +641,18 @@ cmdline_parser_internal (
goto failure;
break;
case 'r': /* Number of requests allowed for one client. */
if (update_arg( (void *)&(args_info->client_max_requests_arg),
&(args_info->client_max_requests_orig), &(args_info->client_max_requests_given),
&(local_args_info.client_max_requests_given), optarg, 0, "100", ARG_INT,
check_ambiguity, override, 0, 0,
"client-max-requests", 'r',
additional_error))
goto failure;
break;
case 0: /* Long option with no short option */
case '?': /* Invalid option. */

View File

@ -15,3 +15,4 @@ option "bind-ip" b "IP to bind to" string optional
section "Advanced daemon options"
option "sockets-per-thread" s "Number of sockets managed by a single thread" default="10" int optional
option "sockets-timeout" t "Close connection after X seconds" default="5" int optional
option "client-max-requests" r "Number of requests allowed for one client" default="100" int optional

View File

@ -60,6 +60,9 @@ struct gengetopt_args_info
int sockets_timeout_arg; /**< @brief Close connection after X seconds (default='5'). */
char * sockets_timeout_orig; /**< @brief Close connection after X seconds original value given at command line. */
const char *sockets_timeout_help; /**< @brief Close connection after X seconds help description. */
int client_max_requests_arg; /**< @brief Number of requests allowed for one client (default='100'). */
char * client_max_requests_orig; /**< @brief Number of requests allowed for one client original value given at command line. */
const char *client_max_requests_help; /**< @brief Number of requests allowed for one client help description. */
unsigned int help_given ; /**< @brief Whether help was given. */
unsigned int version_given ; /**< @brief Whether version was given. */
@ -71,6 +74,7 @@ struct gengetopt_args_info
unsigned int bind_ip_given ; /**< @brief Whether bind-ip was given. */
unsigned int sockets_per_thread_given ; /**< @brief Whether sockets-per-thread was given. */
unsigned int sockets_timeout_given ; /**< @brief Whether sockets-timeout was given. */
unsigned int client_max_requests_given ; /**< @brief Whether client-max-requests was given. */
} ;

Binary file not shown.

View File

@ -18,6 +18,7 @@
typedef struct {
int socket;
time_t timeout;
int nb_remaining_requests;
} socket_ctx_t;
// TODO : sandbox
@ -31,7 +32,6 @@ typedef struct thread_ctx_s{
int nb_available_sockets;
int max_timeout;
int max_sockets;
int nb_remaining_requets; // TODO
int stop;
int quiet;
pthread_mutex_t mutex;
@ -245,6 +245,12 @@ static void* thread_loop(void* param)
close(ctx->sockets[i].socket);
ctx->sockets[i].timeout = -1;
}
// No more requests accepted
if (!ctx->sockets[i].nb_remaining_requests--)
{
close (ctx->sockets[i].socket);
ctx->sockets[i].timeout = -1;
}
}
else
{
@ -304,6 +310,7 @@ static void fill_new_socket(struct gengetopt_args_info* params, int socket)
thread_ctx->sockets[thread_ctx->nb_cur_sockets].socket = socket;
thread_ctx->sockets[thread_ctx->nb_cur_sockets].timeout = thread_ctx->max_timeout;
thread_ctx->sockets[thread_ctx->nb_cur_sockets].nb_remaining_requests = params->client_max_requests_arg;
pthread_mutex_lock(&thread_ctx->mutex);
thread_ctx->nb_cur_sockets++;