optimize session manager config

This commit is contained in:
luwenpeng
2024-08-30 18:33:41 +08:00
parent d1d5e6e09e
commit 82cb1eaeeb
26 changed files with 632 additions and 1319 deletions

View File

@@ -5,9 +5,15 @@ extern "C"
{
#endif
#include <stdint.h>
#include <stdio.h>
#include <time.h>
#include <errno.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "toml.h"
#include "log_private.h"
#define RX_BURST_MAX 32
#define MAX_THREAD_NUM 256 // limit by snowflake
@@ -97,6 +103,136 @@ static inline void hexdump_to_fd(int fd, uint32_t idx, const char *data, uint16_
}
}
// key: "a.b.c"
static inline const char *get_toml_value_from_hierarchical_key(toml_table_t *root, const char *key)
{
toml_table_t *table = root;
char *saveptr;
char *dup_key = strdup(key);
char *token = strtok_r(dup_key, ".", &saveptr);
while (token != NULL)
{
table = toml_table_in(table, token);
if (table == NULL)
{
free(dup_key);
return NULL;
}
if (strchr(saveptr, '.') == NULL)
{
const char *val = toml_raw_in(table, saveptr);
free(dup_key);
return val;
}
token = strtok_r(NULL, ".", &saveptr);
}
free(dup_key);
return toml_raw_in(root, key);
}
static inline int load_and_validate_toml_integer_config(const char *toml_file, const char *key, uint64_t *val, uint64_t min, uint64_t max)
{
int ret = -1;
char errbuf[200];
const char *ptr = NULL;
FILE *fp = NULL;
toml_table_t *root = NULL;
fp = fopen(toml_file, "r");
if (fp == NULL)
{
STELLAR_LOG_ERROR(__thread_local_logger, "config", "config file %s open failed, %s", toml_file, strerror(errno));
return -1;
}
root = toml_parse_file(fp, errbuf, sizeof(errbuf));
if (root == NULL)
{
STELLAR_LOG_ERROR(__thread_local_logger, "config", "config file %s parse failed, %s", toml_file, errbuf);
goto error_out;
}
ptr = get_toml_value_from_hierarchical_key(root, key);
if (ptr == NULL)
{
STELLAR_LOG_ERROR(__thread_local_logger, "config", "config file missing %s", key);
goto error_out;
}
*val = atoll(ptr);
if ((*val) < min || (*val) > max)
{
STELLAR_LOG_ERROR(__thread_local_logger, "config", "invalid %s: %lu, supported range: [%lu, %lu]", key, *val, min, max);
goto error_out;
}
ret = 0;
error_out:
if (root != NULL)
{
toml_free(root);
}
if (fp)
{
fclose(fp);
}
return ret;
}
static inline int load_and_validate_toml_double_config(const char *toml_file, const char *key, double *val, double min, double max)
{
int ret = -1;
char errbuf[200];
const char *ptr = NULL;
FILE *fp = NULL;
toml_table_t *root = NULL;
fp = fopen(toml_file, "r");
if (fp == NULL)
{
STELLAR_LOG_ERROR(__thread_local_logger, "config", "config file %s open failed, %s", toml_file, strerror(errno));
return -1;
}
root = toml_parse_file(fp, errbuf, sizeof(errbuf));
if (root == NULL)
{
STELLAR_LOG_ERROR(__thread_local_logger, "config", "config file %s parse failed, %s", toml_file, errbuf);
goto error_out;
}
ptr = get_toml_value_from_hierarchical_key(root, key);
if (ptr == NULL)
{
STELLAR_LOG_ERROR(__thread_local_logger, "config", "config file missing %s", key);
goto error_out;
}
*val = atof(ptr);
if ((*val) < min || (*val) > max)
{
STELLAR_LOG_ERROR(__thread_local_logger, "config", "invalid %s: %f, supported range: [%f, %f]", key, *val, min, max);
goto error_out;
}
ret = 0;
error_out:
if (root != NULL)
{
toml_free(root);
}
if (fp)
{
fclose(fp);
}
return ret;
}
#ifdef __cplusplus
}
#endif