From fce089317321a67fe223da43ccb889c1e24e8e2a Mon Sep 17 00:00:00 2001 From: Andrew Bettison Date: Tue, 19 Nov 2013 15:43:51 +1030 Subject: [PATCH] Implement HTTP /restful/rhizome/newsince/.../bundlelist.json Now functions as per requirements, blocks for 60 seconds sending new bundles as they appear, sends new token with each new bundle. Tested manually. --- conf_schema.h | 1 + rhizome.h | 1 + rhizome_http.c | 17 ++++++++++++----- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/conf_schema.h b/conf_schema.h index 5f9295eb..21aee1b7 100644 --- a/conf_schema.h +++ b/conf_schema.h @@ -397,6 +397,7 @@ END_STRUCT STRUCT(rhizome_api_restful) SUB_STRUCT(userlist, users,) +ATOM(uint32_t, newsince_timeout, 60, uint32_time_interval,, "Time to block while reporting new bundles") END_STRUCT STRUCT(rhizome_api) diff --git a/rhizome.h b/rhizome.h index 8377a407..7b171a15 100644 --- a/rhizome.h +++ b/rhizome.h @@ -753,6 +753,7 @@ typedef struct rhizome_http_request enum { LIST_HEADER = 0, LIST_ROWS, LIST_DONE } phase; uint64_t rowid_highest; size_t rowcount; + time_ms_t end_time; struct rhizome_list_cursor cursor; } list; } u; diff --git a/rhizome_http.c b/rhizome_http.c index 4f898c25..b098cff4 100644 --- a/rhizome_http.c +++ b/rhizome_http.c @@ -423,6 +423,7 @@ static int restful_rhizome_newsince(rhizome_http_request *r, const char *remaind r->u.list.rowcount = 0; bzero(&r->u.list.cursor, sizeof r->u.list.cursor); r->u.list.cursor.rowid_since = rowid; + r->u.list.end_time = gettime_ms() + config.rhizome.api.restful.newsince_timeout * 1000; http_request_response_generated(&r->http, 200, "application/json", restful_rhizome_bundlelist_json_content); return 0; } @@ -463,14 +464,21 @@ static int restful_rhizome_bundlelist_json_content_chunk(sqlite_retry_state *ret int ret = rhizome_list_next(retry, &r->u.list.cursor); if (ret == -1) return -1; - rhizome_manifest *m = r->u.list.cursor.manifest; if (ret == 0) { - strbuf_puts(b, "\n]\n}\n"); - if (strbuf_overrun(b)) + time_ms_t now; + if (r->u.list.cursor.rowid_since == 0 || (now = gettime_ms()) >= r->u.list.end_time) { + strbuf_puts(b, "\n]\n}\n"); + if (!strbuf_overrun(b)) + r->u.list.phase = LIST_DONE; return 0; - r->u.list.phase = LIST_DONE; + } + time_ms_t wake_at = now + 2000; + if (wake_at > r->u.list.end_time) + wake_at = r->u.list.end_time; + http_request_pause_response(&r->http, wake_at); return 0; } + rhizome_manifest *m = r->u.list.cursor.manifest; assert(m->filesize != RHIZOME_SIZE_UNSET); rhizome_lookup_author(m); if (r->u.list.rowcount != 0) @@ -521,7 +529,6 @@ static int restful_rhizome_bundlelist_json_content_chunk(sqlite_retry_state *ret rhizome_list_commit(&r->u.list.cursor); ++r->u.list.rowcount; } - r->u.list.phase = LIST_ROWS; return 1; } case LIST_DONE: