mirror of
https://github.com/ossrs/srs.git
synced 2025-03-09 15:49:59 +00:00
Fix #1635, support auto reaload config by inotify. 3.0.129
This commit is contained in:
parent
4b395f6e16
commit
9b663c070a
10 changed files with 193 additions and 14 deletions
|
@ -3489,6 +3489,7 @@ srs_error_t SrsConfig::check_normal_config()
|
|||
&& n != "utc_time" && n != "work_dir" && n != "asprocess"
|
||||
&& 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"
|
||||
) {
|
||||
return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "illegal directive %s", n.c_str());
|
||||
}
|
||||
|
@ -4111,6 +4112,30 @@ bool SrsConfig::disable_daemon_for_docker()
|
|||
return SRS_CONF_PERFER_TRUE(conf->arg0());
|
||||
}
|
||||
|
||||
bool SrsConfig::inotify_auto_reload()
|
||||
{
|
||||
static bool DEFAULT = false;
|
||||
|
||||
SrsConfDirective* conf = root->get("inotify_auto_reload");
|
||||
if (!conf || conf->arg0().empty()) {
|
||||
return DEFAULT;
|
||||
}
|
||||
|
||||
return SRS_CONF_PERFER_FALSE(conf->arg0());
|
||||
}
|
||||
|
||||
bool SrsConfig::auto_reload_for_docker()
|
||||
{
|
||||
static bool DEFAULT = true;
|
||||
|
||||
SrsConfDirective* conf = root->get("auto_reload_for_docker");
|
||||
if (!conf || conf->arg0().empty()) {
|
||||
return DEFAULT;
|
||||
}
|
||||
|
||||
return SRS_CONF_PERFER_TRUE(conf->arg0());
|
||||
}
|
||||
|
||||
vector<SrsConfDirective*> SrsConfig::get_stream_casters()
|
||||
{
|
||||
srs_assert(root);
|
||||
|
|
|
@ -478,6 +478,10 @@ public:
|
|||
virtual bool is_force_grace_quit();
|
||||
// Whether disable daemon for docker.
|
||||
virtual bool disable_daemon_for_docker();
|
||||
// Whether use inotify to auto reload by watching config file changes.
|
||||
virtual bool inotify_auto_reload();
|
||||
// Whether enable auto reload config for docker.
|
||||
virtual bool auto_reload_for_docker();
|
||||
// stream_caster section
|
||||
public:
|
||||
// Get all stream_caster in config file.
|
||||
|
|
|
@ -30,6 +30,7 @@
|
|||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <algorithm>
|
||||
#include <sys/inotify.h>
|
||||
using namespace std;
|
||||
|
||||
#include <srs_kernel_log.hpp>
|
||||
|
@ -457,6 +458,114 @@ void SrsSignalManager::sig_catcher(int signo)
|
|||
errno = err;
|
||||
}
|
||||
|
||||
// Whether we are in docker, defined in main module.
|
||||
extern bool _srs_in_docker;
|
||||
|
||||
SrsInotifyWorker::SrsInotifyWorker(SrsServer* s)
|
||||
{
|
||||
server = s;
|
||||
trd = new SrsSTCoroutine("inotify", this);
|
||||
inotify_fd = NULL;
|
||||
watch_fd = 0;
|
||||
}
|
||||
|
||||
SrsInotifyWorker::~SrsInotifyWorker()
|
||||
{
|
||||
srs_freep(trd);
|
||||
srs_close_stfd(inotify_fd);
|
||||
}
|
||||
|
||||
srs_error_t SrsInotifyWorker::start()
|
||||
{
|
||||
srs_error_t err = srs_success;
|
||||
|
||||
// Whether enable auto reload config.
|
||||
bool auto_reload = _srs_config->inotify_auto_reload();
|
||||
if (!auto_reload && _srs_in_docker && _srs_config->auto_reload_for_docker()) {
|
||||
srs_warn("enable auto reload for docker");
|
||||
auto_reload = true;
|
||||
}
|
||||
|
||||
if (!auto_reload) {
|
||||
return err;
|
||||
}
|
||||
|
||||
// Create inotify to watch config file.
|
||||
int fd = ::inotify_init1(IN_NONBLOCK);
|
||||
if (fd < 0) {
|
||||
return srs_error_new(ERROR_INOTIFY_CREATE, "create inotify");
|
||||
}
|
||||
|
||||
// Watch the config file events.
|
||||
string config_file = _srs_config->config();
|
||||
watch_fd = ::inotify_add_watch(fd, config_file.c_str(), IN_ALL_EVENTS);
|
||||
if (watch_fd < 0) {
|
||||
::close(fd);
|
||||
return srs_error_new(ERROR_INOTIFY_WATCH, "watch %s", config_file.c_str());
|
||||
}
|
||||
|
||||
// Open as stfd to read by ST.
|
||||
if ((inotify_fd = srs_netfd_open(fd)) == NULL) {
|
||||
::close(fd);
|
||||
return srs_error_new(ERROR_INOTIFY_OPENFD, "open fd=%d, file=%s, wd=%d", fd, config_file.c_str(), watch_fd);
|
||||
}
|
||||
|
||||
if (((err = srs_fd_closeexec(fd))) != srs_success) {
|
||||
return srs_error_new(ERROR_INOTIFY_OPENFD, "closeexec fd=%d, file=%s, wd=%d", fd, config_file.c_str(), watch_fd);
|
||||
}
|
||||
|
||||
srs_trace("auto reload watching %s, fd=%d, wd=%d", config_file.c_str(), fd, watch_fd);
|
||||
|
||||
if ((err = trd->start()) != srs_success) {
|
||||
return srs_error_wrap(err, "inotify");
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
srs_error_t SrsInotifyWorker::cycle()
|
||||
{
|
||||
srs_error_t err = srs_success;
|
||||
|
||||
while (true) {
|
||||
char buf[4096];
|
||||
ssize_t nn = srs_read(inotify_fd, buf, (size_t)sizeof(buf), SRS_UTIME_NO_TIMEOUT);
|
||||
if (nn < 0) {
|
||||
srs_warn("inotify ignore read failed, nn=%d", (int)nn);
|
||||
break;
|
||||
}
|
||||
|
||||
// Whether config file changed.
|
||||
bool do_reload = false;
|
||||
|
||||
// Parse all inotify events.
|
||||
for (int i = 0; i < (int)nn && !do_reload; i += (int)sizeof(inotify_event)) {
|
||||
inotify_event* ie = (inotify_event*)(buf + i);
|
||||
if (ie->wd != watch_fd) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Only care about the modify or create event.
|
||||
// @see https://github.com/ossrs/srs/issues/1635#issuecomment-598077374
|
||||
if ((ie->mask&IN_MODIFY) != IN_MODIFY && (ie->mask&IN_CREATE) != IN_CREATE) {
|
||||
continue;
|
||||
}
|
||||
|
||||
do_reload = true;
|
||||
srs_trace("inotify event wd=%d, mask=%#x, len=%d, name=%s", ie->wd, ie->mask, ie->len, ie->name);
|
||||
}
|
||||
|
||||
// Notify server to do reload.
|
||||
if (do_reload) {
|
||||
server->on_signal(SRS_SIGNAL_RELOAD);
|
||||
}
|
||||
|
||||
srs_usleep(100 * SRS_UTIME_MILLISECONDS);
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
ISrsServerCycle::ISrsServerCycle()
|
||||
{
|
||||
}
|
||||
|
@ -849,7 +958,16 @@ srs_error_t SrsServer::ingest()
|
|||
|
||||
srs_error_t SrsServer::cycle()
|
||||
{
|
||||
srs_error_t err = do_cycle();
|
||||
srs_error_t err = srs_success;
|
||||
|
||||
// Start the inotify auto reload by watching config file.
|
||||
SrsInotifyWorker inotify(this);
|
||||
if ((err = inotify.start()) != srs_success) {
|
||||
return srs_error_wrap(err, "start inotify");
|
||||
}
|
||||
|
||||
// Do server main cycle.
|
||||
err = do_cycle();
|
||||
|
||||
#ifdef SRS_AUTO_GPERF_MC
|
||||
destroy();
|
||||
|
|
|
@ -184,6 +184,25 @@ private:
|
|||
static void sig_catcher(int signo);
|
||||
};
|
||||
|
||||
// Auto reload by inotify.
|
||||
// @see https://github.com/ossrs/srs/issues/1635
|
||||
class SrsInotifyWorker : public ISrsCoroutineHandler
|
||||
{
|
||||
private:
|
||||
SrsServer* server;
|
||||
SrsCoroutine* trd;
|
||||
srs_netfd_t inotify_fd;
|
||||
int watch_fd;
|
||||
public:
|
||||
SrsInotifyWorker(SrsServer* s);
|
||||
virtual ~SrsInotifyWorker();
|
||||
public:
|
||||
virtual srs_error_t start();
|
||||
// Interface ISrsEndlessThreadHandler.
|
||||
public:
|
||||
virtual srs_error_t cycle();
|
||||
};
|
||||
|
||||
// A handler to the handle cycle in SRS RTMP server.
|
||||
class ISrsServerCycle
|
||||
{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue