1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-03-09 15:49:59 +00:00

STAT: Support config server_id and generate one if empty. v4.0.257

This commit is contained in:
winlin 2022-08-24 15:06:43 +08:00
parent 8e6d207e56
commit 9923c749d4
9 changed files with 158 additions and 33 deletions

View file

@ -58,6 +58,9 @@ const char* _srs_version = "XCORE-" RTMP_SIG_SRS_SERVER;
// '\r'
#define SRS_CR (char)SRS_CONSTS_CR
// Overwrite the config by env.
#define SRS_OVERWRITE_BY_ENV_SECONDS(key) if (getenv(key)) return ::atoi(getenv(key)) * SRS_UTIME_SECONDS
/**
* dumps the ingest/transcode-engine in @param dir to amf0 object @param engine.
* @param dir the transcode or ingest config directive.
@ -2466,11 +2469,11 @@ srs_error_t SrsConfig::check_normal_config()
&& n != "max_connections" && n != "daemon" && n != "heartbeat"
&& n != "http_api" && n != "stats" && n != "vhost" && n != "pithy_print_ms"
&& n != "http_server" && n != "stream_caster" && n != "rtc_server" && n != "srt_server"
&& n != "utc_time" && n != "work_dir" && n != "asprocess"
&& n != "utc_time" && n != "work_dir" && n != "asprocess" && n != "server_id"
&& n != "ff_log_level" && n != "grace_final_wait" && n != "force_grace_quit"
&& n != "grace_start_wait" && n != "empty_ip_ok" && n != "disable_daemon_for_docker"
&& n != "inotify_auto_reload" && n != "auto_reload_for_docker" && n != "tcmalloc_release_rate"
&& n != "query_latest_version"
&& n != "query_latest_version" && n != "first_wait_for_qlv"
&& n != "circuit_breaker" && n != "is_full" && n != "in_docker"
) {
return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "illegal directive %s", n.c_str());
@ -3028,6 +3031,90 @@ SrsConfDirective* SrsConfig::get_root()
return root;
}
string srs_server_id_path(string pid_file)
{
string path = srs_string_replace(pid_file, ".pid", ".id");
if (!srs_string_ends_with(path, ".id")) {
path += ".id";
}
return path;
}
string srs_try_read_file(string path) {
srs_error_t err = srs_success;
SrsFileReader r;
if ((err = r.open(path)) != srs_success) {
srs_freep(err);
return "";
}
static char buf[1024];
ssize_t nn = 0;
if ((err = r.read(buf, sizeof(buf), &nn)) != srs_success) {
srs_freep(err);
return "";
}
if (nn > 0) {
return string(buf, nn);
}
return "";
}
void srs_try_write_file(string path, string content) {
srs_error_t err = srs_success;
SrsFileWriter w;
if ((err = w.open(path)) != srs_success) {
srs_freep(err);
return;
}
if ((err = w.write((void*)content.data(), content.length(), NULL)) != srs_success) {
srs_freep(err);
return;
}
}
string SrsConfig::get_server_id()
{
static string DEFAULT = "";
// Try to read DEFAULT from server id file.
if (DEFAULT.empty()) {
DEFAULT = srs_try_read_file(srs_server_id_path(get_pid_file()));
}
// Generate a random one if empty.
if (DEFAULT.empty()) {
DEFAULT = srs_generate_stat_vid();
}
// Get the server id from env, config or DEFAULT.
string server_id;
if (getenv("SRS_SERVER_ID")) {
server_id = getenv("SRS_SERVER_ID");
}
SrsConfDirective* conf = root->get("server_id");
if (conf) {
server_id = conf->arg0();
}
if (server_id.empty()) {
server_id = DEFAULT;
}
// Write server id to tmp file.
if (!server_id.empty()) {
srs_try_write_file(srs_server_id_path(get_pid_file()), server_id);
}
return server_id;
}
int SrsConfig::get_max_connections()
{
static int DEFAULT = 1000;
@ -3128,6 +3215,20 @@ bool SrsConfig::whether_query_latest_version()
return SRS_CONF_PERFER_TRUE(conf->arg0());
}
srs_utime_t SrsConfig::first_wait_for_qlv()
{
SRS_OVERWRITE_BY_ENV_SECONDS("SRS_FIRST_WAIT_FOR_QLV");
static srs_utime_t DEFAULT = 5 * 60 * SRS_UTIME_SECONDS;
SrsConfDirective* conf = root->get("first_wait_for_qlv");
if (!conf) {
return DEFAULT;
}
return ::atoi(conf->arg0().c_str()) * SRS_UTIME_SECONDS;
}
bool SrsConfig::empty_ip_ok()
{
static bool DEFAULT = true;