Skip to content

Commit aa5ec87

Browse files
Support HTTP-API for fetching reload result. v5.0.176 v6.0.71 (#3779)
## Reload Error Ignore During a Reload, several stages will be passed through: 1. Parsing new configurations: Parse. 2. Transforming configurations: Transform. 3. Applying configurations: Apply. Previously, any error at any stage would result in a direct exit, making the system completely dependent on configuration checks: ```bash ./objs/srs -c conf/srs.conf -t echo $? #0 ``` Optimized to: If an error occurs before applying the configuration, it can be ignored. If an error occurs during the application of the configuration, some of the configuration may have already taken effect, leading to unpredictable behavior, so SRS will exit directly. ## Reload Fetch API Added a new HTTP API to query the result of the reload. ```nginx http_api { enabled on; raw_api { enabled on; allow_reload on; } } ``` ```bash curl http://localhost:1985/api/v1/raw?rpc=reload-fetch ``` ```json { "code": 0, "data": { "err": 0, "msg": "Success", "state": 0, "rid": "0s6y0n9" } } { "code": 0, "data": { "err": 1023, "msg": "code=1023(ConfigInvalid) : parse file : parse buffer containers/conf/srs.release-local.conf : root parse : parse dir : parse include buffer containers/data/config/srs.vhost.conf : read token, line=0, state=0 : line 3: unexpected end of file, expecting ; or \"}\"", "state": 1, "rid": "0g4z471" } } ``` This way, you can know if the last reload of the system was successful. --------- Co-authored-by: Haibo Chen <[email protected]>
1 parent bb93311 commit aa5ec87

File tree

10 files changed

+85
-21
lines changed

10 files changed

+85
-21
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ ARG TARGETPLATFORM
4141
RUN echo "BUILDPLATFORM: $BUILDPLATFORM, TARGETPLATFORM: $TARGETPLATFORM"
4242

4343
# Expose ports for streaming @see https://github.com/ossrs/srs#ports
44-
EXPOSE 1935 1985 8080 8000/udp 10080/udp
44+
EXPOSE 1935 1985 8080 5060 9000 8000/udp 10080/udp
4545

4646
# FFMPEG 4.1
4747
COPY --from=build /usr/local/bin/ffmpeg /usr/local/srs/objs/ffmpeg/bin/ffmpeg

trunk/conf/full.conf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -651,8 +651,8 @@ stream_caster {
651651
# The available variables:
652652
# [stream] The video channel codec ID.
653653
output rtmp://127.0.0.1/live/[stream];
654-
# The listen TCP/UDP port for stream converter.
655-
# For gb28181 converter, listen at TCP/UDP port. for example, 9000.
654+
# The listen TCP port for stream converter.
655+
# For gb28181 converter, listen at TCP port. for example, 9000.
656656
# @remark We always enable bundle for media streams at this port.
657657
listen 9000;
658658
# SIP server for GB28181. Please note that this is only a demonstrated SIP server, please never use it in your
@@ -662,7 +662,7 @@ stream_caster {
662662
# Whether enable embedded SIP server.
663663
# Default: on
664664
enabled on;
665-
# The SIP listen port, for both TCP and UDP protocol.
665+
# The SIP listen port, for TCP protocol.
666666
# Default: 5060
667667
listen 5060;
668668
# The SIP or media transport timeout in seconds.

trunk/doc/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ The changelog for SRS.
77
<a name="v6-changes"></a>
88

99
## SRS 6.0 Changelog
10+
* v6.0, 2023-08-30, Merge [#3779](https://github.com/ossrs/srs/pull/3779): Support HTTP-API for fetching reload result. v6.0.71 (#3779)
1011
* v6.0, 2023-08-28, Merge [#3503](https://github.com/ossrs/srs/pull/3503): SrsContextId assignment can be improved without create a duplicated one. v6.0.70 (#3503)
1112
* v6.0, 2023-08-28, Merge [#3781](https://github.com/ossrs/srs/pull/3781): HLS: Fix on_hls and hls_dispose critical zone issue. v6.0.69 (#3781)
1213
* v6.0, 2023-08-28, Merge [#3768](https://github.com/ossrs/srs/pull/3768): Support include empty config file. v6.0.68 (#3768)
@@ -82,6 +83,7 @@ The changelog for SRS.
8283
<a name="v5-changes"></a>
8384

8485
## SRS 5.0 Changelog
86+
* v5.0, 2023-08-30, Merge [#3779](https://github.com/ossrs/srs/pull/3779): Support HTTP-API for fetching reload result. v5.0.176 (#3779)
8587
* v5.0, 2023-08-28, Merge [#3503](https://github.com/ossrs/srs/pull/3503): SrsContextId assignment can be improved without create a duplicated one. v5.0.175 (#3503)
8688
* v5.0, 2023-08-28, Merge [#3781](https://github.com/ossrs/srs/pull/3781): HLS: Fix on_hls and hls_dispose critical zone issue. v5.0.174 (#3781)
8789
* v5.0, 2023-08-28, Merge [#3768](https://github.com/ossrs/srs/pull/3768): Support include empty config file. v5.0.173 (#3768)

trunk/src/app/srs_app_config.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1333,30 +1333,36 @@ void SrsConfig::unsubscribe(ISrsReloadHandler* handler)
13331333
}
13341334

13351335
// LCOV_EXCL_START
1336-
srs_error_t SrsConfig::reload()
1336+
srs_error_t SrsConfig::reload(SrsReloadState *pstate)
13371337
{
1338+
*pstate = SrsReloadStateInit;
1339+
13381340
srs_error_t err = srs_success;
1339-
1341+
13401342
SrsConfig conf;
1341-
1343+
1344+
*pstate = SrsReloadStateParsing;
13421345
if ((err = conf.parse_file(config_file.c_str())) != srs_success) {
13431346
return srs_error_wrap(err, "parse file");
13441347
}
13451348
srs_info("config reloader parse file success.");
13461349

13471350
// transform config to compatible with previous style of config.
1351+
*pstate = SrsReloadStateTransforming;
13481352
if ((err = srs_config_transform_vhost(conf.root)) != srs_success) {
13491353
return srs_error_wrap(err, "transform config");
13501354
}
13511355

13521356
if ((err = conf.check_config()) != srs_success) {
13531357
return srs_error_wrap(err, "check config");
13541358
}
1355-
1359+
1360+
*pstate = SrsReloadStateApplying;
13561361
if ((err = reload_conf(&conf)) != srs_success) {
13571362
return srs_error_wrap(err, "reload config");
13581363
}
1359-
1364+
1365+
*pstate = SrsReloadStateFinished;
13601366
return err;
13611367
}
13621368
// LCOV_EXCL_STOP

trunk/src/app/srs_app_config.hpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,19 @@ class SrsConfDirective
256256
virtual srs_error_t read_token(srs_internal::SrsConfigBuffer* buffer, std::vector<std::string>& args, int& line_start, SrsDirectiveState& state);
257257
};
258258

259+
// The state for reloading config.
260+
enum SrsReloadState {
261+
SrsReloadStateInit = 0,
262+
// Start to parse the new config file.
263+
SrsReloadStateParsing = 10,
264+
// Start to transform the new config file to new version.
265+
SrsReloadStateTransforming = 20,
266+
// Start to apply the new config file.
267+
SrsReloadStateApplying = 30,
268+
// The reload is finished.
269+
SrsReloadStateFinished = 90,
270+
};
271+
259272
// The config service provider.
260273
// For the config supports reload, so never keep the reference cross st-thread,
261274
// that is, never save the SrsConfDirective* get by any api of config,
@@ -308,7 +321,7 @@ class SrsConfig
308321
virtual void unsubscribe(ISrsReloadHandler* handler);
309322
// Reload the config file.
310323
// @remark, user can test the config before reload it.
311-
virtual srs_error_t reload();
324+
virtual srs_error_t reload(SrsReloadState *pstate);
312325
private:
313326
// Reload the vhost section of config.
314327
virtual srs_error_t reload_vhost(SrsConfDirective* old_root);

trunk/src/app/srs_app_http_api.cpp

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -863,7 +863,7 @@ srs_error_t SrsGoApiClients::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessa
863863
}
864864
} else {
865865
SrsJsonObject* data = SrsJsonAny::object();
866-
obj->set("client", data);;
866+
obj->set("client", data);
867867

868868
if ((err = client->dumps(data)) != srs_success) {
869869
int code = srs_error_code(err);
@@ -907,6 +907,10 @@ SrsGoApiRaw::~SrsGoApiRaw()
907907
_srs_config->unsubscribe(this);
908908
}
909909

910+
extern srs_error_t _srs_reload_err;
911+
extern SrsReloadState _srs_reload_state;
912+
extern std::string _srs_reload_id;
913+
910914
srs_error_t SrsGoApiRaw::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
911915
{
912916
srs_error_t err = srs_success;
@@ -937,8 +941,8 @@ srs_error_t SrsGoApiRaw::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage*
937941

938942
//////////////////////////////////////////////////////////////////////////
939943
// the rpc is required.
940-
// the allowd rpc method check.
941-
if (rpc.empty() || rpc != "reload") {
944+
// the allowed rpc method check.
945+
if (rpc.empty() || (rpc != "reload" && rpc != "reload-fetch")) {
942946
return srs_api_response_code(w, r, ERROR_SYSTEM_CONFIG_RAW);
943947
}
944948

@@ -950,6 +954,16 @@ srs_error_t SrsGoApiRaw::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage*
950954

951955
server->on_signal(SRS_SIGNAL_RELOAD);
952956
return srs_api_response_code(w, r, ERROR_SUCCESS);
957+
} else if (rpc == "reload-fetch") {
958+
SrsJsonObject* data = SrsJsonAny::object();
959+
obj->set("data", data);
960+
961+
data->set("err", SrsJsonAny::integer(srs_error_code(_srs_reload_err)));
962+
data->set("msg", SrsJsonAny::str(srs_error_summary(_srs_reload_err).c_str()));
963+
data->set("state", SrsJsonAny::integer(_srs_reload_state));
964+
data->set("rid", SrsJsonAny::str(_srs_reload_id.c_str()));
965+
966+
return srs_api_response(w, r, obj->dumps());
953967
}
954968

955969
return err;

trunk/src/app/srs_app_server.cpp

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -870,7 +870,9 @@ srs_error_t SrsServer::cycle()
870870
}
871871

872872
// Do server main cycle.
873-
err = do_cycle();
873+
if ((err = do_cycle()) != srs_success) {
874+
srs_error("server err %s", srs_error_desc(err).c_str());
875+
}
874876

875877
// OK, SRS server is done.
876878
wg_->done();
@@ -942,6 +944,10 @@ void SrsServer::on_signal(int signo)
942944
}
943945
}
944946

947+
srs_error_t _srs_reload_err;
948+
SrsReloadState _srs_reload_state;
949+
std::string _srs_reload_id;
950+
945951
srs_error_t SrsServer::do_cycle()
946952
{
947953
srs_error_t err = srs_success;
@@ -991,12 +997,27 @@ srs_error_t SrsServer::do_cycle()
991997
// do reload the config.
992998
if (signal_reload) {
993999
signal_reload = false;
994-
srs_info("get signal to reload the config.");
995-
996-
if ((err = _srs_config->reload()) != srs_success) {
997-
return srs_error_wrap(err, "config reload");
1000+
srs_trace("starting reload config.");
1001+
1002+
SrsReloadState state = SrsReloadStateInit;
1003+
_srs_reload_state = SrsReloadStateInit; srs_freep(_srs_reload_err); _srs_reload_id = srs_random_str(7);
1004+
err = _srs_config->reload(&state);
1005+
_srs_reload_state = state; _srs_reload_err = srs_error_copy(err);
1006+
if (err != srs_success) {
1007+
// If the parsing and transformation of the configuration fail, we can tolerate it by simply
1008+
// ignoring the new configuration and continuing to use the current one. However, if the
1009+
// application of the new configuration fails, some configurations may be applied while
1010+
// others may not. For instance, the listening port may be closed when the configuration
1011+
// is set to listen on an unavailable port. In such cases, we should terminate the service.
1012+
if (state == SrsReloadStateApplying) {
1013+
return srs_error_wrap(err, "reload fatal error state=%d", state);
1014+
}
1015+
1016+
srs_warn("reload failed, state=%d, err %s", state, srs_error_desc(err).c_str());
1017+
srs_freep(err);
1018+
} else {
1019+
srs_trace("reload config success, state=%d.", state);
9981020
}
999-
srs_trace("reload config success.");
10001021
}
10011022

10021023
srs_usleep(1 * SRS_UTIME_SECONDS);

trunk/src/app/srs_app_threads.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,10 @@ srs_error_t SrsCircuitBreaker::on_timer(srs_utime_t interval)
295295
SrsCircuitBreaker* _srs_circuit_breaker = NULL;
296296
SrsAsyncCallWorker* _srs_dvr_async = NULL;
297297

298+
extern srs_error_t _srs_reload_err;
299+
extern SrsReloadState _srs_reload_state;
300+
extern std::string _srs_reload_id;
301+
298302
srs_error_t srs_global_initialize()
299303
{
300304
srs_error_t err = srs_success;
@@ -446,6 +450,10 @@ srs_error_t srs_global_initialize()
446450
_srs_apm = new SrsApmClient();
447451
#endif
448452

453+
_srs_reload_err = srs_success;
454+
_srs_reload_state = SrsReloadStateInit;
455+
_srs_reload_id = srs_random_str(7);
456+
449457
return err;
450458
}
451459

trunk/src/core/srs_core_version5.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99

1010
#define VERSION_MAJOR 5
1111
#define VERSION_MINOR 0
12-
#define VERSION_REVISION 175
12+
#define VERSION_REVISION 176
1313

1414
#endif

trunk/src/core/srs_core_version6.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99

1010
#define VERSION_MAJOR 6
1111
#define VERSION_MINOR 0
12-
#define VERSION_REVISION 70
12+
#define VERSION_REVISION 71
1313

1414
#endif

0 commit comments

Comments
 (0)