Add max nb requests per client
This commit is contained in:
parent
446c5b0461
commit
9f880f8705
|
@ -47,6 +47,7 @@ const char *gengetopt_args_info_help[] = {
|
||||||
"\nAdvanced daemon options:",
|
"\nAdvanced daemon options:",
|
||||||
" -s, --sockets-per-thread=INT Number of sockets managed by a single thread\n (default=`10')",
|
" -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')",
|
" -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
|
0
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -82,6 +83,7 @@ void clear_given (struct gengetopt_args_info *args_info)
|
||||||
args_info->bind_ip_given = 0 ;
|
args_info->bind_ip_given = 0 ;
|
||||||
args_info->sockets_per_thread_given = 0 ;
|
args_info->sockets_per_thread_given = 0 ;
|
||||||
args_info->sockets_timeout_given = 0 ;
|
args_info->sockets_timeout_given = 0 ;
|
||||||
|
args_info->client_max_requests_given = 0 ;
|
||||||
}
|
}
|
||||||
|
|
||||||
static
|
static
|
||||||
|
@ -101,6 +103,8 @@ void clear_args (struct gengetopt_args_info *args_info)
|
||||||
args_info->sockets_per_thread_orig = NULL;
|
args_info->sockets_per_thread_orig = NULL;
|
||||||
args_info->sockets_timeout_arg = 5;
|
args_info->sockets_timeout_arg = 5;
|
||||||
args_info->sockets_timeout_orig = NULL;
|
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->bind_ip_help = gengetopt_args_info_help[9] ;
|
||||||
args_info->sockets_per_thread_help = gengetopt_args_info_help[11] ;
|
args_info->sockets_per_thread_help = gengetopt_args_info_help[11] ;
|
||||||
args_info->sockets_timeout_help = gengetopt_args_info_help[12] ;
|
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->bind_ip_orig));
|
||||||
free_string_field (&(args_info->sockets_per_thread_orig));
|
free_string_field (&(args_info->sockets_per_thread_orig));
|
||||||
free_string_field (&(args_info->sockets_timeout_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);
|
write_into_file(outfile, "sockets-per-thread", args_info->sockets_per_thread_orig, 0);
|
||||||
if (args_info->sockets_timeout_given)
|
if (args_info->sockets_timeout_given)
|
||||||
write_into_file(outfile, "sockets-timeout", args_info->sockets_timeout_orig, 0);
|
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;
|
i = EXIT_SUCCESS;
|
||||||
|
@ -523,10 +531,11 @@ cmdline_parser_internal (
|
||||||
{ "bind-ip", 1, NULL, 'b' },
|
{ "bind-ip", 1, NULL, 'b' },
|
||||||
{ "sockets-per-thread", 1, NULL, 's' },
|
{ "sockets-per-thread", 1, NULL, 's' },
|
||||||
{ "sockets-timeout", 1, NULL, 't' },
|
{ "sockets-timeout", 1, NULL, 't' },
|
||||||
|
{ "client-max-requests", 1, NULL, 'r' },
|
||||||
{ 0, 0, 0, 0 }
|
{ 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. */
|
if (c == -1) break; /* Exit from `while (1)' loop. */
|
||||||
|
|
||||||
|
@ -632,6 +641,18 @@ cmdline_parser_internal (
|
||||||
goto failure;
|
goto failure;
|
||||||
|
|
||||||
break;
|
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 0: /* Long option with no short option */
|
||||||
case '?': /* Invalid option. */
|
case '?': /* Invalid option. */
|
||||||
|
|
|
@ -15,3 +15,4 @@ option "bind-ip" b "IP to bind to" string optional
|
||||||
section "Advanced daemon options"
|
section "Advanced daemon options"
|
||||||
option "sockets-per-thread" s "Number of sockets managed by a single thread" default="10" int optional
|
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 "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
|
||||||
|
|
|
@ -60,6 +60,9 @@ struct gengetopt_args_info
|
||||||
int sockets_timeout_arg; /**< @brief Close connection after X seconds (default='5'). */
|
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. */
|
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. */
|
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 help_given ; /**< @brief Whether help was given. */
|
||||||
unsigned int version_given ; /**< @brief Whether version 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 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_per_thread_given ; /**< @brief Whether sockets-per-thread was given. */
|
||||||
unsigned int sockets_timeout_given ; /**< @brief Whether sockets-timeout 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. */
|
||||||
|
|
||||||
} ;
|
} ;
|
||||||
|
|
||||||
|
|
BIN
src/ip_to_geo
BIN
src/ip_to_geo
Binary file not shown.
|
@ -18,6 +18,7 @@
|
||||||
typedef struct {
|
typedef struct {
|
||||||
int socket;
|
int socket;
|
||||||
time_t timeout;
|
time_t timeout;
|
||||||
|
int nb_remaining_requests;
|
||||||
} socket_ctx_t;
|
} socket_ctx_t;
|
||||||
|
|
||||||
// TODO : sandbox
|
// TODO : sandbox
|
||||||
|
@ -31,7 +32,6 @@ typedef struct thread_ctx_s{
|
||||||
int nb_available_sockets;
|
int nb_available_sockets;
|
||||||
int max_timeout;
|
int max_timeout;
|
||||||
int max_sockets;
|
int max_sockets;
|
||||||
int nb_remaining_requets; // TODO
|
|
||||||
int stop;
|
int stop;
|
||||||
int quiet;
|
int quiet;
|
||||||
pthread_mutex_t mutex;
|
pthread_mutex_t mutex;
|
||||||
|
@ -245,6 +245,12 @@ static void* thread_loop(void* param)
|
||||||
close(ctx->sockets[i].socket);
|
close(ctx->sockets[i].socket);
|
||||||
ctx->sockets[i].timeout = -1;
|
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
|
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].socket = socket;
|
||||||
thread_ctx->sockets[thread_ctx->nb_cur_sockets].timeout = thread_ctx->max_timeout;
|
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);
|
pthread_mutex_lock(&thread_ctx->mutex);
|
||||||
thread_ctx->nb_cur_sockets++;
|
thread_ctx->nb_cur_sockets++;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user