Skip to content

Commit bf95fdf

Browse files
committed
Threads-Log: Use thread to write and reopen logs.
1. Remove support for reload log configs. 2. Add config for thread pool cycle interval. 3. Add config for log flush interval. 4. Create a SrsAsyncLogManager to create writers. 5. Create a SrsAsyncFileWriter to write file async.
1 parent 0cea382 commit bf95fdf

15 files changed

+419
-420
lines changed

trunk/conf/full.conf

+10
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ srs_log_level trace;
4141
# when srs_log_tank is file, specifies the log file.
4242
# default: ./objs/srs.log
4343
srs_log_file ./objs/srs.log;
44+
# The interval in ms, to flush async log.
45+
# Default: 1300
46+
srs_log_async_interval 1300;
4447
# the max connections.
4548
# if exceed the max connections, server will drop the new connection.
4649
# default: 1000
@@ -111,6 +114,13 @@ auto_reload_for_docker on;
111114
# default: 0.8
112115
tcmalloc_release_rate 0.8;
113116

117+
# For thread pool.
118+
threads {
119+
# The thread pool manager cycle interval, in seconds.
120+
# Default: 60
121+
interval 60;
122+
}
123+
114124
#############################################################################################
115125
# heartbeat/stats sections
116126
#############################################################################################

trunk/src/app/srs_app_config.cpp

+40-189
Original file line numberDiff line numberDiff line change
@@ -1492,41 +1492,13 @@ srs_error_t SrsConfig::reload_conf(SrsConfig* conf)
14921492
}
14931493
}
14941494

1495-
// merge config: srs_log_tank
1496-
if (!srs_directive_equals(root->get("srs_log_tank"), old_root->get("srs_log_tank"))) {
1497-
if ((err = do_reload_srs_log_tank()) != srs_success) {
1498-
return srs_error_wrap(err, "log tank");;
1499-
}
1500-
}
1501-
1502-
// merge config: srs_log_level
1503-
if (!srs_directive_equals(root->get("srs_log_level"), old_root->get("srs_log_level"))) {
1504-
if ((err = do_reload_srs_log_level()) != srs_success) {
1505-
return srs_error_wrap(err, "log level");;
1506-
}
1507-
}
1508-
1509-
// merge config: srs_log_file
1510-
if (!srs_directive_equals(root->get("srs_log_file"), old_root->get("srs_log_file"))) {
1511-
if ((err = do_reload_srs_log_file()) != srs_success) {
1512-
return srs_error_wrap(err, "log file");;
1513-
}
1514-
}
1515-
15161495
// merge config: max_connections
15171496
if (!srs_directive_equals(root->get("max_connections"), old_root->get("max_connections"))) {
15181497
if ((err = do_reload_max_connections()) != srs_success) {
15191498
return srs_error_wrap(err, "max connections");;
15201499
}
15211500
}
15221501

1523-
// merge config: utc_time
1524-
if (!srs_directive_equals(root->get("utc_time"), old_root->get("utc_time"))) {
1525-
if ((err = do_reload_utc_time()) != srs_success) {
1526-
return srs_error_wrap(err, "utc time");;
1527-
}
1528-
}
1529-
15301502
// merge config: pithy_print_ms
15311503
if (!srs_directive_equals(root->get("pithy_print_ms"), old_root->get("pithy_print_ms"))) {
15321504
if ((err = do_reload_pithy_print_ms()) != srs_success) {
@@ -2946,78 +2918,6 @@ srs_error_t SrsConfig::raw_set_ff_log_dir(string ff_log_dir, bool& applied)
29462918
return err;
29472919
}
29482920

2949-
srs_error_t SrsConfig::raw_set_srs_log_tank(string srs_log_tank, bool& applied)
2950-
{
2951-
srs_error_t err = srs_success;
2952-
2953-
applied = false;
2954-
2955-
SrsConfDirective* conf = root->get_or_create("srs_log_tank");
2956-
2957-
if (conf->arg0() == srs_log_tank) {
2958-
return err;
2959-
}
2960-
2961-
conf->args.clear();
2962-
conf->args.push_back(srs_log_tank);
2963-
2964-
if ((err = do_reload_srs_log_tank()) != srs_success) {
2965-
return srs_error_wrap(err, "reload log tank");
2966-
}
2967-
2968-
applied = true;
2969-
2970-
return err;
2971-
}
2972-
2973-
srs_error_t SrsConfig::raw_set_srs_log_level(string srs_log_level, bool& applied)
2974-
{
2975-
srs_error_t err = srs_success;
2976-
2977-
applied = false;
2978-
2979-
SrsConfDirective* conf = root->get_or_create("srs_log_level");
2980-
2981-
if (conf->arg0() == srs_log_level) {
2982-
return err;
2983-
}
2984-
2985-
conf->args.clear();
2986-
conf->args.push_back(srs_log_level);
2987-
2988-
if ((err = do_reload_srs_log_level()) != srs_success) {
2989-
return srs_error_wrap(err, "reload log level");
2990-
}
2991-
2992-
applied = true;
2993-
2994-
return err;
2995-
}
2996-
2997-
srs_error_t SrsConfig::raw_set_srs_log_file(string srs_log_file, bool& applied)
2998-
{
2999-
srs_error_t err = srs_success;
3000-
3001-
applied = false;
3002-
3003-
SrsConfDirective* conf = root->get_or_create("srs_log_file");
3004-
3005-
if (conf->arg0() == srs_log_file) {
3006-
return err;
3007-
}
3008-
3009-
conf->args.clear();
3010-
conf->args.push_back(srs_log_file);
3011-
3012-
if ((err = do_reload_srs_log_file()) != srs_success) {
3013-
return srs_error_wrap(err, "reload log file");
3014-
}
3015-
3016-
applied = true;
3017-
3018-
return err;
3019-
}
3020-
30212921
srs_error_t SrsConfig::raw_set_max_connections(string max_connections, bool& applied)
30222922
{
30232923
srs_error_t err = srs_success;
@@ -3042,30 +2942,6 @@ srs_error_t SrsConfig::raw_set_max_connections(string max_connections, bool& app
30422942
return err;
30432943
}
30442944

3045-
srs_error_t SrsConfig::raw_set_utc_time(string utc_time, bool& applied)
3046-
{
3047-
srs_error_t err = srs_success;
3048-
3049-
applied = false;
3050-
3051-
SrsConfDirective* conf = root->get_or_create("utc_time");
3052-
3053-
if (conf->arg0() == utc_time) {
3054-
return err;
3055-
}
3056-
3057-
conf->args.clear();
3058-
conf->args.push_back(utc_time);
3059-
3060-
if ((err = do_reload_utc_time()) != srs_success) {
3061-
return srs_error_wrap(err, "reload");
3062-
}
3063-
3064-
applied = true;
3065-
3066-
return err;
3067-
}
3068-
30692945
srs_error_t SrsConfig::raw_set_pithy_print_ms(string pithy_print_ms, bool& applied)
30702946
{
30712947
srs_error_t err = srs_success;
@@ -3265,54 +3141,6 @@ srs_error_t SrsConfig::do_reload_pid()
32653141
return err;
32663142
}
32673143

3268-
srs_error_t SrsConfig::do_reload_srs_log_tank()
3269-
{
3270-
srs_error_t err = srs_success;
3271-
3272-
vector<ISrsReloadHandler*>::iterator it;
3273-
for (it = subscribes.begin(); it != subscribes.end(); ++it) {
3274-
ISrsReloadHandler* subscribe = *it;
3275-
if ((err = subscribe->on_reload_log_tank()) != srs_success) {
3276-
return srs_error_wrap(err, "notify subscribes reload srs_log_tank failed");
3277-
}
3278-
}
3279-
srs_trace("reload srs_log_tank success.");
3280-
3281-
return err;
3282-
}
3283-
3284-
srs_error_t SrsConfig::do_reload_srs_log_level()
3285-
{
3286-
srs_error_t err = srs_success;
3287-
3288-
vector<ISrsReloadHandler*>::iterator it;
3289-
for (it = subscribes.begin(); it != subscribes.end(); ++it) {
3290-
ISrsReloadHandler* subscribe = *it;
3291-
if ((err = subscribe->on_reload_log_level()) != srs_success) {
3292-
return srs_error_wrap(err, "notify subscribes reload srs_log_level failed");
3293-
}
3294-
}
3295-
srs_trace("reload srs_log_level success.");
3296-
3297-
return err;
3298-
}
3299-
3300-
srs_error_t SrsConfig::do_reload_srs_log_file()
3301-
{
3302-
srs_error_t err = srs_success;
3303-
3304-
vector<ISrsReloadHandler*>::iterator it;
3305-
for (it = subscribes.begin(); it != subscribes.end(); ++it) {
3306-
ISrsReloadHandler* subscribe = *it;
3307-
if ((err = subscribe->on_reload_log_file()) != srs_success) {
3308-
return srs_error_wrap(err, "notify subscribes reload srs_log_file failed");
3309-
}
3310-
}
3311-
srs_trace("reload srs_log_file success.");
3312-
3313-
return err;
3314-
}
3315-
33163144
srs_error_t SrsConfig::do_reload_max_connections()
33173145
{
33183146
srs_error_t err = srs_success;
@@ -3329,22 +3157,6 @@ srs_error_t SrsConfig::do_reload_max_connections()
33293157
return err;
33303158
}
33313159

3332-
srs_error_t SrsConfig::do_reload_utc_time()
3333-
{
3334-
srs_error_t err = srs_success;
3335-
3336-
vector<ISrsReloadHandler*>::iterator it;
3337-
for (it = subscribes.begin(); it != subscribes.end(); ++it) {
3338-
ISrsReloadHandler* subscribe = *it;
3339-
if ((err = subscribe->on_reload_utc_time()) != srs_success) {
3340-
return srs_error_wrap(err, "utc_time");
3341-
}
3342-
}
3343-
srs_trace("reload utc_time success.");
3344-
3345-
return err;
3346-
}
3347-
33483160
srs_error_t SrsConfig::do_reload_pithy_print_ms()
33493161
{
33503162
srs_error_t err = srs_success;
@@ -3581,7 +3393,7 @@ srs_error_t SrsConfig::check_normal_config()
35813393
&& n != "ff_log_level" && n != "grace_final_wait" && n != "force_grace_quit"
35823394
&& n != "grace_start_wait" && n != "empty_ip_ok" && n != "disable_daemon_for_docker"
35833395
&& n != "inotify_auto_reload" && n != "auto_reload_for_docker" && n != "tcmalloc_release_rate"
3584-
) {
3396+
&& n != "srs_log_flush_interval" && n != "threads") {
35853397
return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "illegal directive %s", n.c_str());
35863398
}
35873399
}
@@ -4293,6 +4105,28 @@ double SrsConfig::tcmalloc_release_rate()
42934105
return trr;
42944106
}
42954107

4108+
srs_utime_t SrsConfig::get_threads_interval()
4109+
{
4110+
static srs_utime_t DEFAULT = 60 * SRS_UTIME_SECONDS;
4111+
4112+
SrsConfDirective* conf = root->get("threads");
4113+
if (!conf) {
4114+
return DEFAULT;
4115+
}
4116+
4117+
conf = conf->get("interval");
4118+
if (!conf || conf->arg0().empty()) {
4119+
return DEFAULT;
4120+
}
4121+
4122+
int v = ::atoi(conf->arg0().c_str());
4123+
if (v <= 0) {
4124+
return DEFAULT;
4125+
}
4126+
4127+
return v * SRS_UTIME_SECONDS;
4128+
}
4129+
42964130
vector<SrsConfDirective*> SrsConfig::get_stream_casters()
42974131
{
42984132
srs_assert(root);
@@ -6989,6 +6823,23 @@ string SrsConfig::get_log_file()
69896823
return conf->arg0();
69906824
}
69916825

6826+
srs_utime_t SrsConfig::srs_log_flush_interval()
6827+
{
6828+
srs_utime_t DEFAULT = 1300 * SRS_UTIME_MILLISECONDS;
6829+
6830+
SrsConfDirective* conf = root->get("srs_log_flush_interval");
6831+
if (!conf || conf->arg0().empty()) {
6832+
return DEFAULT;
6833+
}
6834+
6835+
srs_utime_t v = ::atoi(conf->arg0().c_str()) * SRS_UTIME_MILLISECONDS;
6836+
if (v <= 0) {
6837+
return DEFAULT;
6838+
}
6839+
6840+
return v;
6841+
}
6842+
69926843
bool SrsConfig::get_ff_log_enabled()
69936844
{
69946845
string log = get_ff_log_dir();

trunk/src/app/srs_app_config.hpp

+5-12
Original file line numberDiff line numberDiff line change
@@ -367,16 +367,8 @@ class SrsConfig
367367
virtual srs_error_t raw_set_chunk_size(std::string chunk_size, bool& applied);
368368
// RAW set the global ffmpeg log dir.
369369
virtual srs_error_t raw_set_ff_log_dir(std::string ff_log_dir, bool& applied);
370-
// RAW set the global log tank.
371-
virtual srs_error_t raw_set_srs_log_tank(std::string srs_log_tank, bool& applied);
372-
// RAW set the global log level.
373-
virtual srs_error_t raw_set_srs_log_level(std::string srs_log_level, bool& applied);
374-
// RAW set the global log file path for file tank.
375-
virtual srs_error_t raw_set_srs_log_file(std::string srs_log_file, bool& applied);
376370
// RAW set the global max connections of srs.
377371
virtual srs_error_t raw_set_max_connections(std::string max_connections, bool& applied);
378-
// RAW set the global whether use utc time.
379-
virtual srs_error_t raw_set_utc_time(std::string utc_time, bool& applied);
380372
// RAW set the global pithy print interval in ms.
381373
virtual srs_error_t raw_set_pithy_print_ms(std::string pithy_print_ms, bool& applied);
382374
// RAW create the new vhost.
@@ -396,11 +388,7 @@ class SrsConfig
396388
private:
397389
virtual srs_error_t do_reload_listen();
398390
virtual srs_error_t do_reload_pid();
399-
virtual srs_error_t do_reload_srs_log_tank();
400-
virtual srs_error_t do_reload_srs_log_level();
401-
virtual srs_error_t do_reload_srs_log_file();
402391
virtual srs_error_t do_reload_max_connections();
403-
virtual srs_error_t do_reload_utc_time();
404392
virtual srs_error_t do_reload_pithy_print_ms();
405393
virtual srs_error_t do_reload_vhost_added(std::string vhost);
406394
virtual srs_error_t do_reload_vhost_removed(std::string vhost);
@@ -487,6 +475,9 @@ class SrsConfig
487475
virtual bool auto_reload_for_docker();
488476
// For tcmalloc, get the release rate.
489477
virtual double tcmalloc_release_rate();
478+
// Thread pool section.
479+
public:
480+
virtual srs_utime_t get_threads_interval();
490481
// stream_caster section
491482
public:
492483
// Get all stream_caster in config file.
@@ -901,6 +892,8 @@ class SrsConfig
901892
virtual std::string get_log_level();
902893
// Get the log file path.
903894
virtual std::string get_log_file();
895+
// Get the interval in ms to flush asyn log.
896+
virtual srs_utime_t srs_log_flush_interval();
904897
// Whether ffmpeg log enabled
905898
virtual bool get_ff_log_enabled();
906899
// The ffmpeg log dir.

0 commit comments

Comments
 (0)