winlin

support reload http_api

@@ -567,82 +567,158 @@ int SrsConfig::reload() @@ -567,82 +567,158 @@ int SrsConfig::reload()
567 } 567 }
568 srs_trace("reload pithy_print success."); 568 srs_trace("reload pithy_print success.");
569 } 569 }
  570 +
  571 + // merge config: http_api
  572 + if ((ret = reload_http_api(old_root)) != ERROR_SUCCESS) {
  573 + return ret;
  574 + }
570 575
571 - // merge config: vhost added  
572 - for (int i = 0; i < (int)root->directives.size(); i++) {  
573 - // ingest need to start if specified.  
574 - // other features, directly supported.  
575 - SrsConfDirective* new_vhost = root->at(i);  
576 -  
577 - // only process vhost directives.  
578 - if (new_vhost->name != "vhost") {  
579 - continue;  
580 - }  
581 -  
582 - std::string vhost = new_vhost->arg0();  
583 -  
584 - // not new added vhost, ignore.  
585 - if (old_root->get("vhost", vhost)) {  
586 - continue; 576 + // merge config: vhost
  577 + if ((ret = reload_vhost(old_root)) != ERROR_SUCCESS) {
  578 + return ret;
  579 + }
  580 +
  581 + return ret;
  582 +}
  583 +
  584 +int SrsConfig::reload_http_api(SrsConfDirective* old_root)
  585 +{
  586 + int ret = ERROR_SUCCESS;
  587 +
  588 + // merge config.
  589 + std::vector<ISrsReloadHandler*>::iterator it;
  590 +
  591 + // state graph
  592 + // old_http_api new_http_api
  593 + // DISABLED => ENABLED
  594 + // ENABLED => DISABLED
  595 + // ENABLED => ENABLED (modified)
  596 +
  597 + SrsConfDirective* new_http_api = root->get("http_api");
  598 + SrsConfDirective* old_http_api = old_root->get("http_api");
  599 +
  600 + // DISABLED => ENABLED
  601 + if (!get_http_api_enabled(old_http_api) && get_http_api_enabled(new_http_api)) {
  602 + for (it = subscribes.begin(); it != subscribes.end(); ++it) {
  603 + ISrsReloadHandler* subscribe = *it;
  604 + if ((ret = subscribe->on_reload_http_api_enabled()) != ERROR_SUCCESS) {
  605 + srs_error("notify subscribes http_api disabled=>enabled failed. ret=%d", ret);
  606 + return ret;
  607 + }
587 } 608 }
  609 + srs_trace("reload disabled=>enabled http_api success.");
588 610
589 - srs_trace("vhost %s added, reload it.", vhost.c_str()); 611 + return ret;
  612 + }
  613 +
  614 + // ENABLED => DISABLED
  615 + if (get_http_api_enabled(old_http_api) && !get_http_api_enabled(new_http_api)) {
590 for (it = subscribes.begin(); it != subscribes.end(); ++it) { 616 for (it = subscribes.begin(); it != subscribes.end(); ++it) {
591 ISrsReloadHandler* subscribe = *it; 617 ISrsReloadHandler* subscribe = *it;
592 - if ((ret = subscribe->on_reload_vhost_added(vhost)) != ERROR_SUCCESS) {  
593 - srs_error("notify subscribes pithy_print remove "  
594 - "vhost %s failed. ret=%d", vhost.c_str(), ret); 618 + if ((ret = subscribe->on_reload_http_api_disabled()) != ERROR_SUCCESS) {
  619 + srs_error("notify subscribes http_api enabled=>disabled failed. ret=%d", ret);
595 return ret; 620 return ret;
596 } 621 }
597 } 622 }
598 - srs_trace("reload new vhost %s success.", vhost.c_str()); 623 + srs_trace("reload enabled=>disabled http_api success.");
  624 +
  625 + return ret;
599 } 626 }
600 627
601 - // merge config: vhost removed/disabled/modified.  
602 - for (int i = 0; i < (int)old_root->directives.size(); i++) {  
603 - SrsConfDirective* old_vhost = old_root->at(i);  
604 - // only process vhost directives.  
605 - if (old_vhost->name != "vhost") {  
606 - continue; 628 + // ENABLED => ENABLED (modified)
  629 + if (get_http_api_enabled(old_http_api) && get_http_api_enabled(new_http_api)
  630 + && !srs_directive_equals(old_http_api, new_http_api)
  631 + ) {
  632 + for (it = subscribes.begin(); it != subscribes.end(); ++it) {
  633 + ISrsReloadHandler* subscribe = *it;
  634 + if ((ret = subscribe->on_reload_http_api_enabled()) != ERROR_SUCCESS) {
  635 + srs_error("notify subscribes http_api enabled modified failed. ret=%d", ret);
  636 + return ret;
  637 + }
607 } 638 }
  639 + srs_trace("reload enabled modified http_api success.");
608 640
609 - std::string vhost = old_vhost->arg0(); 641 + return ret;
  642 + }
  643 +
  644 + srs_trace("reload http_api not changed success.");
  645 +
  646 + return ret;
  647 +}
610 648
611 - SrsConfDirective* new_vhost = root->get("vhost", vhost);  
612 - // ignore if absolutely equal  
613 - if (new_vhost && srs_directive_equals(old_vhost, new_vhost)) {  
614 - srs_trace("vhost %s absolutely equal, ignore.", vhost.c_str()); 649 +int SrsConfig::reload_vhost(SrsConfDirective* old_root)
  650 +{
  651 + int ret = ERROR_SUCCESS;
  652 +
  653 + // merge config.
  654 + std::vector<ISrsReloadHandler*>::iterator it;
  655 +
  656 + // state graph
  657 + // old_vhost new_vhost
  658 + // DISABLED => ENABLED
  659 + // ENABLED => DISABLED
  660 + // ENABLED => ENABLED (modified)
  661 +
  662 + // collect all vhost names
  663 + std::vector<std::string> vhosts;
  664 + for (int i = 0; i < (int)root->directives.size(); i++) {
  665 + SrsConfDirective* vhost = root->at(i);
  666 + if (vhost->name != "vhost") {
615 continue; 667 continue;
616 } 668 }
617 - // ignore if enable the new vhost when old vhost is disabled.  
618 - if (get_vhost_enabled(new_vhost) && !get_vhost_enabled(old_vhost)) {  
619 - srs_trace("vhost %s disabled=>enabled, ignore.", vhost.c_str()); 669 + vhosts.push_back(vhost->arg0());
  670 + }
  671 + for (int i = 0; i < (int)old_root->directives.size(); i++) {
  672 + SrsConfDirective* vhost = old_root->at(i);
  673 + if (vhost->name != "vhost") {
620 continue; 674 continue;
621 } 675 }
622 - // ignore if both old and new vhost are disabled.  
623 - if (!get_vhost_enabled(new_vhost) && !get_vhost_enabled(old_vhost)) {  
624 - srs_trace("vhost %s disabled=>disabled, ignore.", vhost.c_str()); 676 + if (root->get("vhost", vhost->arg0())) {
625 continue; 677 continue;
626 } 678 }
  679 + vhosts.push_back(vhost->arg0());
  680 + }
  681 +
  682 + // process each vhost
  683 + for (int i = 0; i < (int)vhosts.size(); i++) {
  684 + std::string vhost = vhosts.at(i);
627 685
628 - // merge config: vhost removed/disabled.  
629 - if (!get_vhost_enabled(new_vhost) && get_vhost_enabled(old_vhost)) {  
630 - srs_trace("vhost %s disabled, reload it.", vhost.c_str()); 686 + SrsConfDirective* old_vhost = old_root->get("vhost", vhost);
  687 + SrsConfDirective* new_vhost = root->get("vhost", vhost);
  688 +
  689 + // DISABLED => ENABLED
  690 + if (!get_vhost_enabled(old_vhost) && get_vhost_enabled(new_vhost)) {
  691 + srs_trace("vhost %s added, reload it.", vhost.c_str());
631 for (it = subscribes.begin(); it != subscribes.end(); ++it) { 692 for (it = subscribes.begin(); it != subscribes.end(); ++it) {
632 ISrsReloadHandler* subscribe = *it; 693 ISrsReloadHandler* subscribe = *it;
633 - if ((ret = subscribe->on_reload_vhost_removed(vhost)) != ERROR_SUCCESS) {  
634 - srs_error("notify subscribes pithy_print remove " 694 + if ((ret = subscribe->on_reload_vhost_added(vhost)) != ERROR_SUCCESS) {
  695 + srs_error("notify subscribes added "
635 "vhost %s failed. ret=%d", vhost.c_str(), ret); 696 "vhost %s failed. ret=%d", vhost.c_str(), ret);
636 return ret; 697 return ret;
637 } 698 }
638 } 699 }
639 - srs_trace("reload remove vhost %s success.", vhost.c_str()); 700 + srs_trace("reload new vhost %s success.", vhost.c_str());
640 continue; 701 continue;
641 } 702 }
642 703
643 - // merge config: vhost modified.  
644 - srs_trace("vhost %s modified, reload its detail.", vhost.c_str()); 704 + // ENABLED => DISABLED
  705 + if (get_vhost_enabled(old_vhost) && !get_vhost_enabled(new_vhost)) {
  706 + srs_trace("vhost %s removed, reload it.", vhost.c_str());
  707 + for (it = subscribes.begin(); it != subscribes.end(); ++it) {
  708 + ISrsReloadHandler* subscribe = *it;
  709 + if ((ret = subscribe->on_reload_vhost_removed(vhost)) != ERROR_SUCCESS) {
  710 + srs_error("notify subscribes removed "
  711 + "vhost %s failed. ret=%d", vhost.c_str(), ret);
  712 + return ret;
  713 + }
  714 + }
  715 + srs_trace("reload removed vhost %s success.", vhost.c_str());
  716 + continue;
  717 + }
  718 +
  719 + // ENABLED => ENABLED (modified)
645 if (get_vhost_enabled(new_vhost) && get_vhost_enabled(old_vhost)) { 720 if (get_vhost_enabled(new_vhost) && get_vhost_enabled(old_vhost)) {
  721 + srs_trace("vhost %s modified, reload its detail.", vhost.c_str());
646 // atc, only one per vhost 722 // atc, only one per vhost
647 if (!srs_directive_equals(new_vhost->get("atc"), old_vhost->get("atc"))) { 723 if (!srs_directive_equals(new_vhost->get("atc"), old_vhost->get("atc"))) {
648 for (it = subscribes.begin(); it != subscribes.end(); ++it) { 724 for (it = subscribes.begin(); it != subscribes.end(); ++it) {
@@ -706,10 +782,9 @@ int SrsConfig::reload() @@ -706,10 +782,9 @@ int SrsConfig::reload()
706 if ((ret = reload_ingest(new_vhost, old_vhost)) != ERROR_SUCCESS) { 782 if ((ret = reload_ingest(new_vhost, old_vhost)) != ERROR_SUCCESS) {
707 return ret; 783 return ret;
708 } 784 }
709 - // TODO: suppor reload hls/forward/ffmpeg/http  
710 continue; 785 continue;
711 } 786 }
712 - srs_warn("invalid reload path, enabled old: %d, new: %d", 787 + srs_trace("igreno reload vhost, enabled old: %d, new: %d",
713 get_vhost_enabled(old_vhost), get_vhost_enabled(new_vhost)); 788 get_vhost_enabled(old_vhost), get_vhost_enabled(new_vhost));
714 } 789 }
715 790
@@ -2116,7 +2191,11 @@ SrsConfDirective* SrsConfig::get_http_api() @@ -2116,7 +2191,11 @@ SrsConfDirective* SrsConfig::get_http_api()
2116 bool SrsConfig::get_http_api_enabled() 2191 bool SrsConfig::get_http_api_enabled()
2117 { 2192 {
2118 SrsConfDirective* conf = get_http_api(); 2193 SrsConfDirective* conf = get_http_api();
2119 - 2194 + return get_http_api_enabled(conf);
  2195 +}
  2196 +
  2197 +bool SrsConfig::get_http_api_enabled(SrsConfDirective* conf)
  2198 +{
2120 if (!conf) { 2199 if (!conf) {
2121 return false; 2200 return false;
2122 } 2201 }
@@ -124,6 +124,8 @@ public: @@ -124,6 +124,8 @@ public:
124 virtual void unsubscribe(ISrsReloadHandler* handler); 124 virtual void unsubscribe(ISrsReloadHandler* handler);
125 virtual int reload(); 125 virtual int reload();
126 private: 126 private:
  127 + virtual int reload_http_api(SrsConfDirective* old_root);
  128 + virtual int reload_vhost(SrsConfDirective* old_root);
127 virtual int reload_transcode(SrsConfDirective* new_vhost, SrsConfDirective* old_vhost); 129 virtual int reload_transcode(SrsConfDirective* new_vhost, SrsConfDirective* old_vhost);
128 virtual int reload_ingest(SrsConfDirective* new_vhost, SrsConfDirective* old_vhost); 130 virtual int reload_ingest(SrsConfDirective* new_vhost, SrsConfDirective* old_vhost);
129 // parse options and file 131 // parse options and file
@@ -222,6 +224,7 @@ private: @@ -222,6 +224,7 @@ private:
222 virtual SrsConfDirective* get_http_api(); 224 virtual SrsConfDirective* get_http_api();
223 public: 225 public:
224 virtual bool get_http_api_enabled(); 226 virtual bool get_http_api_enabled();
  227 + virtual bool get_http_api_enabled(SrsConfDirective* conf);
225 virtual int get_http_api_listen(); 228 virtual int get_http_api_listen();
226 // http stream section 229 // http stream section
227 private: 230 private:
@@ -65,6 +65,16 @@ int ISrsReloadHandler::on_reload_pithy_print() @@ -65,6 +65,16 @@ int ISrsReloadHandler::on_reload_pithy_print()
65 return ERROR_SUCCESS; 65 return ERROR_SUCCESS;
66 } 66 }
67 67
  68 +int ISrsReloadHandler::on_reload_http_api_enabled()
  69 +{
  70 + return ERROR_SUCCESS;
  71 +}
  72 +
  73 +int ISrsReloadHandler::on_reload_http_api_disabled()
  74 +{
  75 + return ERROR_SUCCESS;
  76 +}
  77 +
68 int ISrsReloadHandler::on_reload_vhost_added(string /*vhost*/) 78 int ISrsReloadHandler::on_reload_vhost_added(string /*vhost*/)
69 { 79 {
70 return ERROR_SUCCESS; 80 return ERROR_SUCCESS;
@@ -47,6 +47,8 @@ public: @@ -47,6 +47,8 @@ public:
47 virtual int on_reload_log_level(); 47 virtual int on_reload_log_level();
48 virtual int on_reload_log_file(); 48 virtual int on_reload_log_file();
49 virtual int on_reload_pithy_print(); 49 virtual int on_reload_pithy_print();
  50 + virtual int on_reload_http_api_enabled();
  51 + virtual int on_reload_http_api_disabled();
50 virtual int on_reload_vhost_added(std::string vhost); 52 virtual int on_reload_vhost_added(std::string vhost);
51 virtual int on_reload_vhost_removed(std::string vhost); 53 virtual int on_reload_vhost_removed(std::string vhost);
52 virtual int on_reload_atc(std::string vhost); 54 virtual int on_reload_atc(std::string vhost);
@@ -617,3 +617,17 @@ int SrsServer::on_reload_pid() @@ -617,3 +617,17 @@ int SrsServer::on_reload_pid()
617 617
618 return acquire_pid_file(); 618 return acquire_pid_file();
619 } 619 }
  620 +
  621 +int SrsServer::on_reload_http_api_enabled()
  622 +{
  623 + return listen_http_api();
  624 +}
  625 +
  626 +int SrsServer::on_reload_http_api_disabled()
  627 +{
  628 + int ret = ERROR_SUCCESS;
  629 +
  630 + close_listeners(SrsListenerHttpApi);
  631 +
  632 + return ret;
  633 +}
@@ -116,6 +116,8 @@ private: @@ -116,6 +116,8 @@ private:
116 public: 116 public:
117 virtual int on_reload_listen(); 117 virtual int on_reload_listen();
118 virtual int on_reload_pid(); 118 virtual int on_reload_pid();
  119 + virtual int on_reload_http_api_enabled();
  120 + virtual int on_reload_http_api_disabled();
119 }; 121 };
120 122
121 #endif 123 #endif