0
0
Fork 0
mirror of https://github.com/netdata/netdata.git synced 2025-05-06 02:00:18 +00:00

fix expiration dates for API responses ()

This commit is contained in:
Costa Tsaousis 2023-07-26 16:10:31 +03:00 committed by GitHub
parent 33cdac2f68
commit 6daedef25b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 26 additions and 9 deletions

View file

@ -1052,7 +1052,7 @@ QUERY_TARGET *query_target_create(QUERY_TARGET_REQUEST *qtr) {
if(query_target_has_percentage_of_group(qt)) if(query_target_has_percentage_of_group(qt))
qt->window.options &= ~RRDR_OPTION_PERCENTAGE; qt->window.options &= ~RRDR_OPTION_PERCENTAGE;
rrdr_relative_window_to_absolute(&qt->window.after, &qt->window.before, &qt->window.now); qt->internal.relative = rrdr_relative_window_to_absolute(&qt->window.after, &qt->window.before, &qt->window.now);
// prepare our local variables - we need these across all these functions // prepare our local variables - we need these across all these functions
QUERY_TARGET_LOCALS qtl = { QUERY_TARGET_LOCALS qtl = {

View file

@ -409,6 +409,7 @@ typedef struct query_target {
struct { struct {
SPINLOCK spinlock; SPINLOCK spinlock;
bool used; // when true, this query is currently being used bool used; // when true, this query is currently being used
bool relative; // when true, this query uses relative timestamps
size_t queries; // how many query we have done so far with this QUERY_TARGET - not related to database queries size_t queries; // how many query we have done so far with this QUERY_TARGET - not related to database queries
struct query_target *prev; struct query_target *prev;
struct query_target *next; struct query_target *next;

View file

@ -15,6 +15,7 @@ void buffer_reset(BUFFER *wb) {
wb->options = 0; wb->options = 0;
wb->date = 0; wb->date = 0;
wb->expires = 0; wb->expires = 0;
buffer_no_cacheable(wb);
buffer_overflow_check(wb); buffer_overflow_check(wb);
} }
@ -254,6 +255,7 @@ BUFFER *buffer_create(size_t size, size_t *statistics)
b->size = size; b->size = size;
b->content_type = CT_TEXT_PLAIN; b->content_type = CT_TEXT_PLAIN;
b->statistics = statistics; b->statistics = statistics;
buffer_no_cacheable(b);
buffer_overflow_init(b); buffer_overflow_init(b);
buffer_overflow_check(b); buffer_overflow_check(b);
@ -317,6 +319,7 @@ void buffer_json_initialize(BUFFER *wb, const char *key_quote, const char *value
buffer_fast_strcat(wb, "{", 1); buffer_fast_strcat(wb, "{", 1);
wb->content_type = CT_APPLICATION_JSON; wb->content_type = CT_APPLICATION_JSON;
buffer_no_cacheable(wb);
} }
void buffer_json_finalize(BUFFER *wb) { void buffer_json_finalize(BUFFER *wb) {

View file

@ -1057,8 +1057,10 @@ int web_client_api_request_v1_badge(RRDHOST *host, struct web_client *w, char *u
buffer_sprintf(w->response.header, "Refresh: %d\r\n", refresh); buffer_sprintf(w->response.header, "Refresh: %d\r\n", refresh);
w->response.data->date = now_realtime_sec(); w->response.data->date = now_realtime_sec();
w->response.data->expires = w->response.data->date + refresh; w->response.data->expires = w->response.data->date + refresh;
buffer_cacheable(w->response.data);
} }
else buffer_no_cacheable(w->response.data); else
buffer_no_cacheable(w->response.data);
if(!value_color) { if(!value_color) {
switch(rc->status) { switch(rc->status) {

View file

@ -1808,6 +1808,8 @@ int web_api_v12_weights(BUFFER *wb, QUERY_WEIGHTS_REQUEST *qwr) {
if(!rrdr_relative_window_to_absolute(&qwr->after, &qwr->before, NULL)) if(!rrdr_relative_window_to_absolute(&qwr->after, &qwr->before, NULL))
buffer_no_cacheable(wb); buffer_no_cacheable(wb);
else
buffer_cacheable(wb);
if (qwr->before <= qwr->after) { if (qwr->before <= qwr->after) {
resp = HTTP_RESP_BAD_REQUEST; resp = HTTP_RESP_BAD_REQUEST;

View file

@ -32,6 +32,8 @@ static bool web_client_check_acl_and_bearer(struct web_client *w, WEB_CLIENT_ACL
} }
int web_client_api_request_vX(RRDHOST *host, struct web_client *w, char *url_path_endpoint, struct web_api_command *api_commands) { int web_client_api_request_vX(RRDHOST *host, struct web_client *w, char *url_path_endpoint, struct web_api_command *api_commands) {
buffer_no_cacheable(w->response.data);
if(unlikely(!url_path_endpoint || !*url_path_endpoint)) { if(unlikely(!url_path_endpoint || !*url_path_endpoint)) {
buffer_flush(w->response.data); buffer_flush(w->response.data);
buffer_sprintf(w->response.data, "Which API command?"); buffer_sprintf(w->response.data, "Which API command?");

View file

@ -883,6 +883,11 @@ static inline int web_client_api_request_v1_data(RRDHOST *host, struct web_clien
else if(format == DATASOURCE_JSONP) else if(format == DATASOURCE_JSONP)
buffer_strcat(w->response.data, ");"); buffer_strcat(w->response.data, ");");
if(qt->internal.relative)
buffer_no_cacheable(w->response.data);
else
buffer_cacheable(w->response.data);
cleanup: cleanup:
query_target_release(qt); query_target_release(qt);
onewayalloc_destroy(owa); onewayalloc_destroy(owa);

View file

@ -629,6 +629,11 @@ static int web_client_api_request_v2_data(RRDHOST *host __maybe_unused, struct w
else if(format == DATASOURCE_JSONP) else if(format == DATASOURCE_JSONP)
buffer_strcat(w->response.data, ");"); buffer_strcat(w->response.data, ");");
if(qt->internal.relative)
buffer_no_cacheable(w->response.data);
else
buffer_cacheable(w->response.data);
cleanup: cleanup:
query_target_release(qt); query_target_release(qt);
onewayalloc_destroy(owa); onewayalloc_destroy(owa);

View file

@ -304,7 +304,7 @@ static void webrtc_execute_api_request(WEBRTC_DC *chan, const char *request, siz
web_client_timeout_checkpoint_set(w, 0); web_client_timeout_checkpoint_set(w, 0);
web_client_decode_path_and_query_string(w, path); web_client_decode_path_and_query_string(w, path);
path = (char *)buffer_tostring(w->url_path_decoded); path = (char *)buffer_tostring(w->url_path_decoded);
w->response.code = (short)web_client_api_request_with_node_selection(localhost, w, path); w->response.code = (short)web_client_api_request_with_node_selection(rrdb.localhost, w, path);
web_client_timeout_checkpoint_response_ready(w, NULL); web_client_timeout_checkpoint_response_ready(w, NULL);
size_t sent_bytes = 0; size_t sent_bytes = 0;

View file

@ -1274,12 +1274,9 @@ void web_client_build_http_header(struct web_client *w) {
w->response.data->date = now_realtime_sec(); w->response.data->date = now_realtime_sec();
// set a proper expiration date, if not already set // set a proper expiration date, if not already set
if(unlikely(!w->response.data->expires)) { if(unlikely(!w->response.data->expires))
if(w->response.data->options & WB_CONTENT_NO_CACHEABLE) w->response.data->expires = w->response.data->date +
w->response.data->expires = w->response.data->date + rrdb.localhost->update_every; ((w->response.data->options & WB_CONTENT_NO_CACHEABLE) ? 0 : 86400);
else
w->response.data->expires = w->response.data->date + 86400;
}
// prepare the HTTP response header // prepare the HTTP response header
netdata_log_debug(D_WEB_CLIENT, "%llu: Generating HTTP header with response %d.", w->id, w->response.code); netdata_log_debug(D_WEB_CLIENT, "%llu: Generating HTTP header with response %d.", w->id, w->response.code);