support section for network config

connection_manager
YuQing 2020-09-16 21:10:16 +08:00
parent 4db063335f
commit 04e2449545
4 changed files with 118 additions and 83 deletions

View File

@ -41,6 +41,10 @@ IdempotencyClientConfig g_idempotency_client_cfg = {false, 3, 300};
static int load_client_channel_config(IniFullContext *ini_ctx) static int load_client_channel_config(IniFullContext *ini_ctx)
{ {
g_idempotency_client_cfg.enabled = iniGetBoolValue(
ini_ctx->section_name, "enabled",
ini_ctx->context, false);
g_idempotency_client_cfg.channel_htable_capacity = iniGetIntValue( g_idempotency_client_cfg.channel_htable_capacity = iniGetIntValue(
ini_ctx->section_name, "channel_htable_capacity", ini_ctx->section_name, "channel_htable_capacity",
ini_ctx->context, 1361); ini_ctx->context, 1361);
@ -72,9 +76,11 @@ static int load_client_channel_config(IniFullContext *ini_ctx)
void idempotency_client_channel_config_to_string_ex( void idempotency_client_channel_config_to_string_ex(
char *output, const int size, const bool add_comma) char *output, const int size, const bool add_comma)
{ {
snprintf(output, size, "channel_htable_capacity=%d, " snprintf(output, size, "idempotency enabled: %d, "
"channel_htable_capacity=%d, "
"channel_heartbeat_interval=%ds, " "channel_heartbeat_interval=%ds, "
"channel_max_idle_time=%ds%s", "channel_max_idle_time=%ds%s",
g_idempotency_client_cfg.enabled,
g_idempotency_client_cfg.channel_htable_capacity, g_idempotency_client_cfg.channel_htable_capacity,
g_idempotency_client_cfg.channel_heartbeat_interval, g_idempotency_client_cfg.channel_heartbeat_interval,
g_idempotency_client_cfg.channel_max_idle_time, g_idempotency_client_cfg.channel_max_idle_time,

View File

@ -8,6 +8,7 @@
#include "fastcommon/fc_queue.h" #include "fastcommon/fc_queue.h"
typedef struct idempotency_client_config { typedef struct idempotency_client_config {
bool enabled;
int channel_htable_capacity; int channel_htable_capacity;
int channel_heartbeat_interval; int channel_heartbeat_interval;
int channel_max_idle_time; int channel_max_idle_time;

View File

@ -41,7 +41,7 @@ static inline void set_config_str_value(const char *value,
} }
} }
static int load_network_parameters(IniContext *pIniContext) static int load_network_parameters(IniFullContext *ini_ctx)
{ {
int result; int result;
char *pMaxPkgSize; char *pMaxPkgSize;
@ -51,20 +51,20 @@ static int load_network_parameters(IniContext *pIniContext)
int64_t min_buff_size; int64_t min_buff_size;
int64_t max_buff_size; int64_t max_buff_size;
g_sf_global_vars.connect_timeout = iniGetIntValue(NULL, g_sf_global_vars.connect_timeout = iniGetIntValueEx(ini_ctx->section_name,
"connect_timeout", pIniContext, DEFAULT_CONNECT_TIMEOUT); "connect_timeout", ini_ctx->context, DEFAULT_CONNECT_TIMEOUT, true);
if (g_sf_global_vars.connect_timeout <= 0) { if (g_sf_global_vars.connect_timeout <= 0) {
g_sf_global_vars.connect_timeout = DEFAULT_CONNECT_TIMEOUT; g_sf_global_vars.connect_timeout = DEFAULT_CONNECT_TIMEOUT;
} }
g_sf_global_vars.network_timeout = iniGetIntValue(NULL, g_sf_global_vars.network_timeout = iniGetIntValueEx(ini_ctx->section_name,
"network_timeout", pIniContext, DEFAULT_NETWORK_TIMEOUT); "network_timeout", ini_ctx->context, DEFAULT_NETWORK_TIMEOUT, true);
if (g_sf_global_vars.network_timeout <= 0) { if (g_sf_global_vars.network_timeout <= 0) {
g_sf_global_vars.network_timeout = DEFAULT_NETWORK_TIMEOUT; g_sf_global_vars.network_timeout = DEFAULT_NETWORK_TIMEOUT;
} }
g_sf_global_vars.max_connections = iniGetIntValue(NULL, g_sf_global_vars.max_connections = iniGetIntValue(ini_ctx->section_name,
"max_connections", pIniContext, DEFAULT_MAX_CONNECTONS); "max_connections", ini_ctx->context, DEFAULT_MAX_CONNECTONS);
if (g_sf_global_vars.max_connections <= 0) { if (g_sf_global_vars.max_connections <= 0) {
g_sf_global_vars.max_connections = DEFAULT_MAX_CONNECTONS; g_sf_global_vars.max_connections = DEFAULT_MAX_CONNECTONS;
} }
@ -75,8 +75,8 @@ static int load_network_parameters(IniContext *pIniContext)
return result; return result;
} }
pMaxPkgSize = iniGetStrValue(NULL, pMaxPkgSize = iniGetStrValue(ini_ctx->section_name,
"max_pkg_size", pIniContext); "max_pkg_size", ini_ctx->context);
if (pMaxPkgSize == NULL) { if (pMaxPkgSize == NULL) {
max_pkg_size = SF_DEF_MAX_PACKAGE_SIZE; max_pkg_size = SF_DEF_MAX_PACKAGE_SIZE;
} }
@ -92,8 +92,8 @@ static int load_network_parameters(IniContext *pIniContext)
} }
g_sf_global_vars.max_pkg_size = (int)max_pkg_size; g_sf_global_vars.max_pkg_size = (int)max_pkg_size;
pMinBuffSize = iniGetStrValue(NULL, pMinBuffSize = iniGetStrValue(ini_ctx->section_name,
"min_buff_size", pIniContext); "min_buff_size", ini_ctx->context);
if (pMinBuffSize == NULL) { if (pMinBuffSize == NULL) {
min_buff_size = SF_DEF_MIN_BUFF_SIZE; min_buff_size = SF_DEF_MIN_BUFF_SIZE;
} }
@ -109,8 +109,8 @@ static int load_network_parameters(IniContext *pIniContext)
} }
g_sf_global_vars.min_buff_size = (int)min_buff_size; g_sf_global_vars.min_buff_size = (int)min_buff_size;
pMaxBuffSize = iniGetStrValue(NULL, pMaxBuffSize = iniGetStrValue(ini_ctx->section_name,
"max_buff_size", pIniContext); "max_buff_size", ini_ctx->context);
if (pMaxBuffSize == NULL) { if (pMaxBuffSize == NULL) {
max_buff_size = SF_DEF_MAX_BUFF_SIZE; max_buff_size = SF_DEF_MAX_BUFF_SIZE;
} }
@ -143,8 +143,8 @@ static int load_network_parameters(IniContext *pIniContext)
return 0; return 0;
} }
int sf_load_global_config_ex(const char *server_name, const char *filename, int sf_load_global_config_ex(const char *server_name,
IniContext *pIniContext, const bool load_network_params) IniFullContext *ini_ctx, const bool load_network_params)
{ {
int result; int result;
char *pBasePath; char *pBasePath;
@ -153,11 +153,11 @@ int sf_load_global_config_ex(const char *server_name, const char *filename,
char *pThreadStackSize; char *pThreadStackSize;
int64_t thread_stack_size; int64_t thread_stack_size;
pBasePath = iniGetStrValue(NULL, "base_path", pIniContext); pBasePath = iniGetStrValue(NULL, "base_path", ini_ctx->context);
if (pBasePath == NULL) { if (pBasePath == NULL) {
logError("file: "__FILE__", line: %d, " logError("file: "__FILE__", line: %d, "
"conf file \"%s\" must have item " "conf file \"%s\" must have item "
"\"base_path\"!", __LINE__, filename); "\"base_path\"!", __LINE__, ini_ctx->filename);
return ENOENT; return ENOENT;
} }
@ -178,13 +178,13 @@ int sf_load_global_config_ex(const char *server_name, const char *filename,
} }
if (load_network_params) { if (load_network_params) {
if ((result=load_network_parameters(pIniContext)) != 0) { if ((result=load_network_parameters(ini_ctx)) != 0) {
return result; return result;
} }
} }
pRunByGroup = iniGetStrValue(NULL, "run_by_group", pIniContext); pRunByGroup = iniGetStrValue(NULL, "run_by_group", ini_ctx->context);
pRunByUser = iniGetStrValue(NULL, "run_by_user", pIniContext); pRunByUser = iniGetStrValue(NULL, "run_by_user", ini_ctx->context);
if (pRunByGroup == NULL) { if (pRunByGroup == NULL) {
*g_sf_global_vars.run_by_group = '\0'; *g_sf_global_vars.run_by_group = '\0';
} }
@ -246,14 +246,14 @@ int sf_load_global_config_ex(const char *server_name, const char *filename,
} }
g_sf_global_vars.sync_log_buff_interval = iniGetIntValue(NULL, g_sf_global_vars.sync_log_buff_interval = iniGetIntValue(NULL,
"sync_log_buff_interval", pIniContext, "sync_log_buff_interval", ini_ctx->context,
SYNC_LOG_BUFF_DEF_INTERVAL); SYNC_LOG_BUFF_DEF_INTERVAL);
if (g_sf_global_vars.sync_log_buff_interval <= 0) { if (g_sf_global_vars.sync_log_buff_interval <= 0) {
g_sf_global_vars.sync_log_buff_interval = SYNC_LOG_BUFF_DEF_INTERVAL; g_sf_global_vars.sync_log_buff_interval = SYNC_LOG_BUFF_DEF_INTERVAL;
} }
pThreadStackSize = iniGetStrValue(NULL, pThreadStackSize = iniGetStrValue(ini_ctx->section_name,
"thread_stack_size", pIniContext); "thread_stack_size", ini_ctx->context);
if (pThreadStackSize == NULL) { if (pThreadStackSize == NULL) {
thread_stack_size = SF_DEF_THREAD_STACK_SIZE; thread_stack_size = SF_DEF_THREAD_STACK_SIZE;
} }
@ -265,11 +265,11 @@ int sf_load_global_config_ex(const char *server_name, const char *filename,
g_sf_global_vars.thread_stack_size = (int)thread_stack_size; g_sf_global_vars.thread_stack_size = (int)thread_stack_size;
g_sf_global_vars.rotate_error_log = iniGetBoolValue(NULL, g_sf_global_vars.rotate_error_log = iniGetBoolValue(NULL,
"rotate_error_log", pIniContext, false); "rotate_error_log", ini_ctx->context, false);
g_sf_global_vars.log_file_keep_days = iniGetIntValue(NULL, g_sf_global_vars.log_file_keep_days = iniGetIntValue(NULL,
"log_file_keep_days", pIniContext, 0); "log_file_keep_days", ini_ctx->context, 0);
load_log_level(pIniContext); load_log_level(ini_ctx->context);
if ((result=log_set_prefix(g_sf_global_vars.base_path, server_name)) != 0) { if ((result=log_set_prefix(g_sf_global_vars.base_path, server_name)) != 0) {
return result; return result;
} }
@ -277,32 +277,19 @@ int sf_load_global_config_ex(const char *server_name, const char *filename,
return 0; return 0;
} }
int sf_load_config_ex(const char *server_name, const char *filename, int sf_load_config_ex(const char *server_name, SFContextIniConfig *config)
IniContext *pIniContext, const char *section_name,
const int default_inner_port, const int default_outer_port)
{ {
int result; int result;
if ((result=sf_load_global_config(server_name, if ((result=sf_load_global_config_ex(server_name,
filename, pIniContext)) != 0) &config->ini_ctx, true)) != 0)
{ {
return result; return result;
} }
return sf_load_context_from_config(&g_sf_context, filename, pIniContext, return sf_load_context_from_config_ex(&g_sf_context, config);
section_name, default_inner_port, default_outer_port);
} }
int sf_load_config(const char *server_name, const char *filename, int sf_load_context_from_config_ex(SFContext *sf_context,
IniContext *pIniContext, const int default_inner_port, SFContextIniConfig *config)
const int default_outer_port)
{
return sf_load_config_ex(server_name, filename, pIniContext, "",
default_inner_port, default_outer_port);
}
int sf_load_context_from_config(SFContext *sf_context,
const char *filename, IniContext *pIniContext,
const char *section_name, const int default_inner_port,
const int default_outer_port)
{ {
char *inner_port; char *inner_port;
char *outer_port; char *outer_port;
@ -313,10 +300,13 @@ int sf_load_context_from_config(SFContext *sf_context,
sf_context->inner_port = sf_context->outer_port = 0; sf_context->inner_port = sf_context->outer_port = 0;
inner_port = iniGetStrValue(section_name, "inner_port", pIniContext); inner_port = iniGetStrValue(config->ini_ctx.section_name,
outer_port = iniGetStrValue(section_name, "outer_port", pIniContext); "inner_port", config->ini_ctx.context);
outer_port = iniGetStrValue(config->ini_ctx.section_name,
"outer_port", config->ini_ctx.context);
if (inner_port == NULL && outer_port == NULL) { if (inner_port == NULL && outer_port == NULL) {
port = iniGetIntValue(section_name, "port", pIniContext, 0); port = iniGetIntValue(config->ini_ctx.section_name,
"port", config->ini_ctx.context, 0);
if (port > 0) { if (port > 0) {
sf_context->inner_port = sf_context->outer_port = port; sf_context->inner_port = sf_context->outer_port = port;
} }
@ -330,19 +320,19 @@ int sf_load_context_from_config(SFContext *sf_context,
} }
if (sf_context->inner_port <= 0) { if (sf_context->inner_port <= 0) {
sf_context->inner_port = default_inner_port; sf_context->inner_port = config->default_inner_port;
} }
if (sf_context->outer_port <= 0) { if (sf_context->outer_port <= 0) {
sf_context->outer_port = default_outer_port; sf_context->outer_port = config->default_outer_port;
} }
inner_bind_addr = iniGetStrValue(section_name, inner_bind_addr = iniGetStrValue(config->ini_ctx.section_name,
"inner_bind_addr", pIniContext); "inner_bind_addr", config->ini_ctx.context);
outer_bind_addr = iniGetStrValue(section_name, outer_bind_addr = iniGetStrValue(config->ini_ctx.section_name,
"outer_bind_addr", pIniContext); "outer_bind_addr", config->ini_ctx.context);
if (inner_bind_addr == NULL && outer_bind_addr == NULL) { if (inner_bind_addr == NULL && outer_bind_addr == NULL) {
bind_addr = iniGetStrValue(section_name, bind_addr = iniGetStrValue(config->ini_ctx.section_name,
"bind_addr", pIniContext); "bind_addr", config->ini_ctx.context);
if (bind_addr != NULL) { if (bind_addr != NULL) {
inner_bind_addr = outer_bind_addr = bind_addr; inner_bind_addr = outer_bind_addr = bind_addr;
} }
@ -352,25 +342,27 @@ int sf_load_context_from_config(SFContext *sf_context,
set_config_str_value(outer_bind_addr, sf_context->outer_bind_addr, set_config_str_value(outer_bind_addr, sf_context->outer_bind_addr,
sizeof(sf_context->outer_bind_addr)); sizeof(sf_context->outer_bind_addr));
sf_context->accept_threads = iniGetIntValue(section_name, sf_context->accept_threads = iniGetIntValue(
"accept_threads", pIniContext, 1); config->ini_ctx.section_name,
"accept_threads", config->ini_ctx.context, 1);
if (sf_context->accept_threads <= 0) { if (sf_context->accept_threads <= 0) {
logError("file: "__FILE__", line: %d, " logError("file: "__FILE__", line: %d, "
"config file: %s, section: %s, " "config file: %s, section: %s, item \"accept_threads\" "
"item \"accept_threads\" is invalid, " "is invalid, value: %d <= 0!", __LINE__, config->
"value: %d <= 0!", __LINE__, filename, ini_ctx.filename, config->ini_ctx.section_name,
section_name, sf_context->accept_threads); sf_context->accept_threads);
return EINVAL; return EINVAL;
} }
sf_context->work_threads = iniGetIntValue(section_name, sf_context->work_threads = iniGetIntValue(
"work_threads", pIniContext, DEFAULT_WORK_THREADS); config->ini_ctx.section_name, "work_threads",
config->ini_ctx.context, config->default_work_threads);
if (sf_context->work_threads <= 0) { if (sf_context->work_threads <= 0) {
logError("file: "__FILE__", line: %d, " logError("file: "__FILE__", line: %d, "
"config file: %s, section: %s, " "config file: %s, section: %s, item \"work_threads\" "
"item \"work_threads\" is invalid, " "is invalid, value: %d <= 0!", __LINE__, config->
"value: %d <= 0!", __LINE__, filename, ini_ctx.filename, config->ini_ctx.section_name,
section_name, sf_context->work_threads); sf_context->work_threads);
return EINVAL; return EINVAL;
} }

View File

@ -39,6 +39,13 @@ typedef struct sf_global_variables {
SFConnectionStat connection_stat; SFConnectionStat connection_stat;
} SFGlobalVariables; } SFGlobalVariables;
typedef struct sf_context_ini_config {
IniFullContext ini_ctx;
int default_inner_port;
int default_outer_port;
int default_work_threads;
} SFContextIniConfig;
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
@ -80,29 +87,58 @@ extern SFContext g_sf_context;
} \ } \
} while (0) } while (0)
int sf_load_global_config_ex(const char *server_name, const char *filename, #define SF_SET_CONTEXT_INI_CONFIG(config, filename, pIniContext, \
IniContext *pIniContext, const bool load_network_params); section_name, def_inner_port, def_outer_port, def_work_threads) \
do { \
FAST_INI_SET_FULL_CTX_EX(config.ini_ctx, filename, \
section_name, pIniContext); \
config.default_inner_port = def_inner_port; \
config.default_outer_port = def_outer_port; \
config.default_work_threads = def_work_threads; \
} while (0)
int sf_load_global_config_ex(const char *server_name,
IniFullContext *ini_ctx, const bool load_network_params);
static inline int sf_load_global_config(const char *server_name, static inline int sf_load_global_config(const char *server_name,
const char *filename, IniContext *pIniContext) IniFullContext *ini_ctx)
{ {
const bool load_network_params = true; const bool load_network_params = true;
return sf_load_global_config_ex(server_name, filename, return sf_load_global_config_ex(server_name,
pIniContext, load_network_params); ini_ctx, load_network_params);
} }
int sf_load_config(const char *server_name, const char *filename, int sf_load_config_ex(const char *server_name,
IniContext *pIniContext, const int default_inner_port, SFContextIniConfig *config);
const int default_outer_port);
int sf_load_config_ex(const char *server_name, const char *filename, static inline int sf_load_config(const char *server_name,
IniContext *pIniContext, const char *section_name,
const int default_inner_port, const int default_outer_port);
int sf_load_context_from_config(SFContext *sf_context,
const char *filename, IniContext *pIniContext, const char *filename, IniContext *pIniContext,
const char *section_name, const int default_inner_port, const char *section_name, const int default_inner_port,
const int default_outer_port); const int default_outer_port)
{
SFContextIniConfig config;
SF_SET_CONTEXT_INI_CONFIG(config, filename, pIniContext,
section_name, default_inner_port, default_outer_port,
DEFAULT_WORK_THREADS);
return sf_load_config_ex(server_name, &config);
}
int sf_load_context_from_config_ex(SFContext *sf_context,
SFContextIniConfig *config);
static inline int sf_load_context_from_config(SFContext *sf_context,
const char *filename, IniContext *pIniContext,
const char *section_name, const int default_inner_port,
const int default_outer_port)
{
SFContextIniConfig config;
SF_SET_CONTEXT_INI_CONFIG(config, filename, pIniContext,
section_name, default_inner_port, default_outer_port,
DEFAULT_WORK_THREADS);
return sf_load_context_from_config_ex(sf_context, &config);
}
void sf_global_config_to_string(char *output, const int size); void sf_global_config_to_string(char *output, const int size);