streaming rate limiting return error 503
Costa Tsaousis (ktsaou) committed
Jul 6, 2018 at 20:14 UTC
f5181aeb47350e996f951b5b6ae4b52a94d88dd5
1 file changed
+14
-2
src/rrdpush.c
+14
-2
@@ -1006,6 +1006,14 @@ int rrdpush_receiver_permission_denied(struct web_client *w) {
1006
return 401;
1007
}
1008
1009
+int rrdpush_receiver_too_busy_now(struct web_client *w) {
1010
+ // we always respond with the same message and error code
1011
+ // to prevent an attacker from gaining info about the error
1012
+ buffer_flush(w->response.data);
1013
+ buffer_sprintf(w->response.data, "The server is too busy now to accept this request. Try later.");
1014
+ return 503;
1015
+}
1016
+
1017
int rrdpush_receiver_thread_spawn(RRDHOST *host, struct web_client *w, char *url) {
1018
(void)host;
1019
@@ -1117,11 +1125,15 @@ int rrdpush_receiver_thread_spawn(RRDHOST *host, struct web_client *w, char *url
1125
time_t now = now_realtime_sec();
1126
1127
netdata_mutex_lock(&stream_rate_mutex);
1128
+ if(unlikely(last_stream_accepted_t == 0))
1129
+ last_stream_accepted_t = now;
1130
+
1131
if(last_stream_accepted_t + 30 < now) {
1132
netdata_mutex_unlock(&stream_rate_mutex);
1122
- error("STREAM [receive from [%s]:%s]: too busy to accept new streaming request. Try again in a while.", w->client_ip, w->client_port);
1123
- return rrdpush_receiver_permission_denied(w);
1133
+ error("STREAM [receive from [%s]:%s]: too busy to accept new streaming request. Try again in %ld secs.", w->client_ip, w->client_port, (long)(last_stream_accepted_t + 30 - now));
1134
+ return rrdpush_receiver_too_busy_now(w);
1135
}
1136
+
1137
last_stream_accepted_t = now;
1138
netdata_mutex_unlock(&stream_rate_mutex);
1139
}