Skip to content

Commit 661eb8b

Browse files
committed
For #913, use complex error for reload utc time
1 parent 5c9a12e commit 661eb8b

9 files changed

+86
-48
lines changed

trunk/src/app/srs_app_config.cpp

+28-29
Original file line numberDiff line numberDiff line change
@@ -1195,7 +1195,6 @@ void SrsConfig::unsubscribe(ISrsReloadHandler* handler)
11951195

11961196
srs_error_t SrsConfig::reload()
11971197
{
1198-
int ret = ERROR_SUCCESS;
11991198
srs_error_t err = srs_success;
12001199

12011200
SrsConfig conf;
@@ -1214,8 +1213,8 @@ srs_error_t SrsConfig::reload()
12141213
return srs_error_wrap(err, "check config");
12151214
}
12161215

1217-
if ((ret = reload_conf(&conf)) != ERROR_SUCCESS) {
1218-
return srs_error_new(ret, "reload config");
1216+
if ((err = reload_conf(&conf)) != srs_success) {
1217+
return srs_error_wrap(err, "reload config");
12191218
}
12201219

12211220
return err;
@@ -1485,9 +1484,10 @@ int SrsConfig::reload_vhost(SrsConfDirective* old_root)
14851484
return ret;
14861485
}
14871486

1488-
int SrsConfig::reload_conf(SrsConfig* conf)
1487+
srs_error_t SrsConfig::reload_conf(SrsConfig* conf)
14891488
{
14901489
int ret = ERROR_SUCCESS;
1490+
srs_error_t err = srs_success;
14911491

14921492
SrsConfDirective* old_root = root;
14931493
SrsAutoFree(SrsConfDirective, old_root);
@@ -1509,78 +1509,78 @@ int SrsConfig::reload_conf(SrsConfig* conf)
15091509
// merge config: listen
15101510
if (!srs_directive_equals(root->get("listen"), old_root->get("listen"))) {
15111511
if ((ret = do_reload_listen()) != ERROR_SUCCESS) {
1512-
return ret;
1512+
return srs_error_new(ret, "listen");
15131513
}
15141514
}
15151515

15161516
// merge config: pid
15171517
if (!srs_directive_equals(root->get("pid"), old_root->get("pid"))) {
15181518
if ((ret = do_reload_pid()) != ERROR_SUCCESS) {
1519-
return ret;
1519+
return srs_error_new(ret, "pid");;
15201520
}
15211521
}
15221522

15231523
// merge config: srs_log_tank
15241524
if (!srs_directive_equals(root->get("srs_log_tank"), old_root->get("srs_log_tank"))) {
15251525
if ((ret = do_reload_srs_log_tank()) != ERROR_SUCCESS) {
1526-
return ret;
1526+
return srs_error_new(ret, "log tank");;
15271527
}
15281528
}
15291529

15301530
// merge config: srs_log_level
15311531
if (!srs_directive_equals(root->get("srs_log_level"), old_root->get("srs_log_level"))) {
15321532
if ((ret = do_reload_srs_log_level()) != ERROR_SUCCESS) {
1533-
return ret;
1533+
return srs_error_new(ret, "log level");;
15341534
}
15351535
}
15361536

15371537
// merge config: srs_log_file
15381538
if (!srs_directive_equals(root->get("srs_log_file"), old_root->get("srs_log_file"))) {
15391539
if ((ret = do_reload_srs_log_file()) != ERROR_SUCCESS) {
1540-
return ret;
1540+
return srs_error_new(ret, "log file");;
15411541
}
15421542
}
15431543

15441544
// merge config: max_connections
15451545
if (!srs_directive_equals(root->get("max_connections"), old_root->get("max_connections"))) {
15461546
if ((ret = do_reload_max_connections()) != ERROR_SUCCESS) {
1547-
return ret;
1547+
return srs_error_new(ret, "max connections");;
15481548
}
15491549
}
15501550

15511551
// merge config: utc_time
15521552
if (!srs_directive_equals(root->get("utc_time"), old_root->get("utc_time"))) {
1553-
if ((ret = do_reload_utc_time()) != ERROR_SUCCESS) {
1554-
return ret;
1553+
if ((err = do_reload_utc_time()) != srs_success) {
1554+
return srs_error_wrap(err, "utc time");;
15551555
}
15561556
}
15571557

15581558
// merge config: pithy_print_ms
15591559
if (!srs_directive_equals(root->get("pithy_print_ms"), old_root->get("pithy_print_ms"))) {
15601560
if ((ret = do_reload_pithy_print_ms()) != ERROR_SUCCESS) {
1561-
return ret;
1561+
return srs_error_new(ret, "pithy print ms");;
15621562
}
15631563
}
15641564

15651565
// merge config: http_api
15661566
if ((ret = reload_http_api(old_root)) != ERROR_SUCCESS) {
1567-
return ret;
1567+
return srs_error_new(ret, "http api");;
15681568
}
15691569

15701570
// merge config: http_stream
15711571
if ((ret = reload_http_stream(old_root)) != ERROR_SUCCESS) {
1572-
return ret;
1572+
return srs_error_new(ret, "http steram");;
15731573
}
15741574

15751575
// TODO: FIXME: support reload stream_caster.
15761576
// TODO: FIXME: support reload kafka.
15771577

15781578
// merge config: vhost
15791579
if ((ret = reload_vhost(old_root)) != ERROR_SUCCESS) {
1580-
return ret;
1580+
return srs_error_new(ret, "vhost");;
15811581
}
15821582

1583-
return ret;
1583+
return err;
15841584
}
15851585

15861586
int SrsConfig::reload_http_api(SrsConfDirective* old_root)
@@ -3015,28 +3015,28 @@ int SrsConfig::raw_set_max_connections(string max_connections, bool& applied)
30153015
return ret;
30163016
}
30173017

3018-
int SrsConfig::raw_set_utc_time(string utc_time, bool& applied)
3018+
srs_error_t SrsConfig::raw_set_utc_time(string utc_time, bool& applied)
30193019
{
3020-
int ret = ERROR_SUCCESS;
3020+
srs_error_t err = srs_success;
30213021

30223022
applied = false;
30233023

30243024
SrsConfDirective* conf = root->get_or_create("utc_time");
30253025

30263026
if (conf->arg0() == utc_time) {
3027-
return ret;
3027+
return err;
30283028
}
30293029

30303030
conf->args.clear();
30313031
conf->args.push_back(utc_time);
30323032

3033-
if ((ret = do_reload_utc_time()) != ERROR_SUCCESS) {
3034-
return ret;
3033+
if ((err = do_reload_utc_time()) != srs_success) {
3034+
return srs_error_wrap(err, "reload");
30353035
}
30363036

30373037
applied = true;
30383038

3039-
return ret;
3039+
return err;
30403040
}
30413041

30423042
int SrsConfig::raw_set_pithy_print_ms(string pithy_print_ms, bool& applied)
@@ -3307,21 +3307,20 @@ int SrsConfig::do_reload_max_connections()
33073307
return ret;
33083308
}
33093309

3310-
int SrsConfig::do_reload_utc_time()
3310+
srs_error_t SrsConfig::do_reload_utc_time()
33113311
{
3312-
int ret = ERROR_SUCCESS;
3312+
srs_error_t err = srs_success;
33133313

33143314
vector<ISrsReloadHandler*>::iterator it;
33153315
for (it = subscribes.begin(); it != subscribes.end(); ++it) {
33163316
ISrsReloadHandler* subscribe = *it;
3317-
if ((ret = subscribe->on_reload_utc_time()) != ERROR_SUCCESS) {
3318-
srs_error("notify subscribes utc_time failed. ret=%d", ret);
3319-
return ret;
3317+
if ((err = subscribe->on_reload_utc_time()) != srs_success) {
3318+
return srs_error_wrap(err, "utc_time");
33203319
}
33213320
}
33223321
srs_trace("reload utc_time success.");
33233322

3324-
return ret;
3323+
return err;
33253324
}
33263325

33273326
int SrsConfig::do_reload_pithy_print_ms()

trunk/src/app/srs_app_config.hpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ class SrsConfig
388388
* reload from the config.
389389
* @remark, use protected for the utest to override with mock.
390390
*/
391-
virtual int reload_conf(SrsConfig* conf);
391+
virtual srs_error_t reload_conf(SrsConfig* conf);
392392
private:
393393
/**
394394
* reload the http_api section of config.
@@ -476,7 +476,7 @@ class SrsConfig
476476
/**
477477
* raw set the global whether use utc time.
478478
*/
479-
virtual int raw_set_utc_time(std::string utc_time, bool& applied);
479+
virtual srs_error_t raw_set_utc_time(std::string utc_time, bool& applied);
480480
/**
481481
* raw set the global pithy print interval in ms.
482482
*/
@@ -516,7 +516,7 @@ class SrsConfig
516516
virtual int do_reload_srs_log_level();
517517
virtual int do_reload_srs_log_file();
518518
virtual int do_reload_max_connections();
519-
virtual int do_reload_utc_time();
519+
virtual srs_error_t do_reload_utc_time();
520520
virtual int do_reload_pithy_print_ms();
521521
virtual int do_reload_vhost_added(std::string vhost);
522522
virtual int do_reload_vhost_removed(std::string vhost);

trunk/src/app/srs_app_http_api.cpp

+43-3
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,16 @@ int srs_api_response_jsonp_code(ISrsHttpResponseWriter* w, string callback, int
8585
return srs_api_response_jsonp(w, callback, obj->dumps());
8686
}
8787

88+
int srs_api_response_jsonp_code(ISrsHttpResponseWriter* w, string callback, srs_error_t err)
89+
{
90+
SrsJsonObject* obj = SrsJsonAny::object();
91+
SrsAutoFree(SrsJsonObject, obj);
92+
93+
obj->set("code", SrsJsonAny::integer(srs_error_code(err)));
94+
95+
return srs_api_response_jsonp(w, callback, obj->dumps());
96+
}
97+
8898
int srs_api_response_json(ISrsHttpResponseWriter* w, string data)
8999
{
90100
SrsHttpHeader* h = w->header();
@@ -105,6 +115,16 @@ int srs_api_response_json_code(ISrsHttpResponseWriter* w, int code)
105115
return srs_api_response_json(w, obj->dumps());
106116
}
107117

118+
int srs_api_response_json_code(ISrsHttpResponseWriter* w, srs_error_t err)
119+
{
120+
SrsJsonObject* obj = SrsJsonAny::object();
121+
SrsAutoFree(SrsJsonObject, obj);
122+
123+
obj->set("code", SrsJsonAny::integer(srs_error_code(err)));
124+
125+
return srs_api_response_json(w, obj->dumps());
126+
}
127+
108128
int srs_api_response(ISrsHttpResponseWriter* w, ISrsHttpMessage* r, std::string json)
109129
{
110130
// no jsonp, directly response.
@@ -129,6 +149,26 @@ int srs_api_response_code(ISrsHttpResponseWriter* w, ISrsHttpMessage* r, int cod
129149
return srs_api_response_jsonp_code(w, callback, code);
130150
}
131151

152+
int srs_api_response_code(ISrsHttpResponseWriter* w, ISrsHttpMessage* r, srs_error_t err)
153+
{
154+
int ret = ERROR_SUCCESS;
155+
156+
// no jsonp, directly response.
157+
if (!r->is_jsonp()) {
158+
ret = srs_api_response_json_code(w, err);
159+
} else {
160+
// jsonp, get function name from query("callback")
161+
string callback = r->query_get("callback");
162+
ret = srs_api_response_jsonp_code(w, callback, err);
163+
}
164+
165+
if (err != srs_success) {
166+
srs_warn("error %s", srs_error_desc(err).c_str());
167+
srs_freep(err);
168+
}
169+
return ret;
170+
}
171+
132172
SrsGoApiRoot::SrsGoApiRoot()
133173
{
134174
}
@@ -852,6 +892,7 @@ SrsGoApiRaw::~SrsGoApiRaw()
852892
int SrsGoApiRaw::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
853893
{
854894
int ret = ERROR_SUCCESS;
895+
srs_error_t err = srs_success;
855896

856897
std::string rpc = r->query_get("rpc");
857898

@@ -1126,9 +1167,8 @@ int SrsGoApiRaw::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
11261167
return srs_api_response_code(w, r, ret);
11271168
}
11281169

1129-
if ((ret = _srs_config->raw_set_utc_time(srs_config_bool2switch(value), applied)) != ERROR_SUCCESS) {
1130-
srs_error("raw api update utc_time=%s failed. ret=%d", value.c_str(), ret);
1131-
return srs_api_response_code(w, r, ret);
1170+
if ((err = _srs_config->raw_set_utc_time(srs_config_bool2switch(value), applied)) != srs_success) {
1171+
return srs_api_response_code(w, r, srs_error_wrap(err, "raw api update utc_time=%s", value.c_str()));
11321172
}
11331173
} else if (scope == "pithy_print_ms") {
11341174
int ppmv = ::atoi(value.c_str());

trunk/src/app/srs_app_log.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -199,11 +199,11 @@ void SrsFastLog::error(const char* tag, int context_id, const char* fmt, ...)
199199
write_log(fd, log_data, size, SrsLogLevelError);
200200
}
201201

202-
int SrsFastLog::on_reload_utc_time()
202+
srs_error_t SrsFastLog::on_reload_utc_time()
203203
{
204204
utc = _srs_config->get_utc_time();
205205

206-
return ERROR_SUCCESS;
206+
return srs_success;
207207
}
208208

209209
int SrsFastLog::on_reload_log_tank()

trunk/src/app/srs_app_log.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class SrsFastLog : public ISrsLog, public ISrsReloadHandler
6565
virtual void error(const char* tag, int context_id, const char* fmt, ...);
6666
// interface ISrsReloadHandler.
6767
public:
68-
virtual int on_reload_utc_time();
68+
virtual srs_error_t on_reload_utc_time();
6969
virtual int on_reload_log_tank();
7070
virtual int on_reload_log_level();
7171
virtual int on_reload_log_file();

trunk/src/app/srs_app_reload.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ int ISrsReloadHandler::on_reload_listen()
4040
return ERROR_SUCCESS;
4141
}
4242

43-
int ISrsReloadHandler::on_reload_utc_time()
43+
srs_error_t ISrsReloadHandler::on_reload_utc_time()
4444
{
45-
return ERROR_SUCCESS;
45+
return srs_success;
4646
}
4747

4848
int ISrsReloadHandler::on_reload_max_conns()

trunk/src/app/srs_app_reload.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class ISrsReloadHandler
4141
ISrsReloadHandler();
4242
virtual ~ISrsReloadHandler();
4343
public:
44-
virtual int on_reload_utc_time();
44+
virtual srs_error_t on_reload_utc_time();
4545
virtual int on_reload_max_conns();
4646
virtual int on_reload_listen();
4747
virtual int on_reload_pid();

trunk/src/app/srs_app_server.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ srs_error_t SrsBufferListener::listen(string i, int p)
150150
listener = new SrsTcpListener(this, ip, port);
151151

152152
if ((err = listener->listen()) != srs_success) {
153-
return srs_error_wrap(err, "buffer tcp listen %s:%d", ip.c_str(), port);
153+
return srs_error_wrap(err, "buffered tcp listen");
154154
}
155155

156156
string v = srs_listener_type2string(type);
@@ -1227,7 +1227,8 @@ srs_error_t SrsServer::fd2conn(SrsListenerType type, srs_netfd_t stfd, SrsConnec
12271227
fd, max_connections, (int)conns.size(), srs_error_desc(err).c_str());
12281228
}
12291229
if ((int)conns.size() >= max_connections) {
1230-
return srs_error_new(ERROR_EXCEED_CONNECTIONS, "drop fd=%d, max=%d, cur=%d for exceed connection limits",
1230+
return srs_error_new(ERROR_EXCEED_CONNECTIONS,
1231+
"drop fd=%d, max=%d, cur=%d for exceed connection limits",
12311232
fd, max_connections, (int)conns.size());
12321233
}
12331234

@@ -1278,8 +1279,7 @@ void SrsServer::remove(ISrsConnection* c)
12781279
stat->kbps_add_delta(conn);
12791280
stat->on_disconnect(conn->srs_id());
12801281

1281-
// all connections are created by server,
1282-
// so we free it here.
1282+
// use manager to free it async.
12831283
conn_manager->remove(c);
12841284
}
12851285

trunk/src/utest/srs_utest_reload.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -285,16 +285,15 @@ MockSrsReloadConfig::~MockSrsReloadConfig()
285285

286286
srs_error_t MockSrsReloadConfig::do_reload(string buf)
287287
{
288-
int ret = ERROR_SUCCESS;
289288
srs_error_t err = srs_success;
290289

291290
MockSrsReloadConfig conf;
292291
if ((err = conf.parse(buf)) != srs_success) {
293292
return srs_error_wrap(err, "parse");
294293
}
295294

296-
if ((ret = MockSrsConfig::reload_conf(&conf)) != ERROR_SUCCESS) {
297-
return srs_error_new(ret, "reload conf");
295+
if ((err = MockSrsConfig::reload_conf(&conf)) != srs_success) {
296+
return srs_error_wrap(err, "reload conf");
298297
}
299298

300299
return err;

0 commit comments

Comments
 (0)