From 4f2e1a4acd3ba9e090ea0fa438a52a062998ffa1 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Tue, 4 Sep 2018 22:48:23 -0700 Subject: [PATCH] modules: Reduce various chunked json buffer mgmt boilerplates. --- modules/client/rooms/context.cc | 14 +------------- modules/client/rooms/messages.cc | 13 +++---------- modules/federation/backfill.cc | 14 +------------- modules/federation/backfill_ids.cc | 13 +++---------- modules/federation/event.cc | 11 +---------- modules/federation/event_auth.cc | 14 +------------- modules/federation/get_missing_events.cc | 14 +------------- modules/federation/state.cc | 14 +------------- modules/federation/state_ids.cc | 13 +++---------- 9 files changed, 15 insertions(+), 105 deletions(-) diff --git a/modules/client/rooms/context.cc b/modules/client/rooms/context.cc index 9694893bd..88ce85a9e 100644 --- a/modules/client/rooms/context.cc +++ b/modules/client/rooms/context.cc @@ -83,26 +83,14 @@ get__context(client &client, event_id }; - const unique_buffer buf - { - 96_KiB - }; - resource::response::chunked response { client, http::OK }; - const auto flush{[&response] - (const const_buffer &buf) - { - response.write(buf); - return buf; - }}; - json::stack out { - buf, flush, size_t(flush_hiwat) + response.buf, response.flusher(), size_t(flush_hiwat) }; json::stack::object ret diff --git a/modules/client/rooms/messages.cc b/modules/client/rooms/messages.cc index a4e5bfde9..4789f080e 100644 --- a/modules/client/rooms/messages.cc +++ b/modules/client/rooms/messages.cc @@ -96,22 +96,15 @@ get__messages(client &client, room, page.from, &default_fetch_opts }; - const unique_buffer buf - { - 96_KiB - }; - resource::response::chunked response { client, http::OK }; - json::stack out{buf, [&response] - (const const_buffer &buf) + json::stack out { - response.write(buf); - return buf; - }}; + response.buf, response.flusher() + }; json::stack::object ret { diff --git a/modules/federation/backfill.cc b/modules/federation/backfill.cc index 740189ab6..8e1fb6fd9 100644 --- a/modules/federation/backfill.cc +++ b/modules/federation/backfill.cc @@ -95,26 +95,14 @@ get__backfill(client &client, room_id, event_id }; - const unique_buffer buf - { - 96_KiB - }; - resource::response::chunked response { client, http::OK }; - const auto flush{[&response] - (const const_buffer &buf) - { - response.write(buf); - return buf; - }}; - json::stack out { - buf, flush, size_t(backfill_flush_hiwat) + response.buf, response.flusher(), size_t(backfill_flush_hiwat) }; json::stack::object top{out}; diff --git a/modules/federation/backfill_ids.cc b/modules/federation/backfill_ids.cc index d3407b146..6fa351e22 100644 --- a/modules/federation/backfill_ids.cc +++ b/modules/federation/backfill_ids.cc @@ -96,22 +96,15 @@ get__backfill_ids(client &client, room }; - const unique_buffer buf - { - 4_KiB - }; - resource::response::chunked response { client, http::OK }; - json::stack out{buf, [&response] - (const const_buffer &buf) + json::stack out { - response.write(buf); - return buf; - }}; + response.buf, response.flusher() + }; json::stack::object top{out}; json::stack::member pdus_m diff --git a/modules/federation/event.cc b/modules/federation/event.cc index 7c227d2a1..1848aba6b 100644 --- a/modules/federation/event.cc +++ b/modules/federation/event.cc @@ -49,11 +49,6 @@ handle_get(client &client, event_id }; - const unique_buffer buf - { - 96_KiB - }; - resource::response::chunked response { client, http::OK @@ -61,11 +56,7 @@ handle_get(client &client, json::stack out { - buf, [&response](const const_buffer &buf) - { - response.write(buf); - return buf; - } + response.buf, response.flusher() }; json::stack::object top{out}; diff --git a/modules/federation/event_auth.cc b/modules/federation/event_auth.cc index 198472223..733bec4d7 100644 --- a/modules/federation/event_auth.cc +++ b/modules/federation/event_auth.cc @@ -80,26 +80,14 @@ get__event_auth(client &client, room }; - const unique_buffer buf - { - 96_KiB - }; - resource::response::chunked response { client, http::OK }; - const auto flush{[&response] - (const const_buffer &buf) - { - response.write(buf); - return buf; - }}; - json::stack out { - buf, flush, size_t(event_auth_flush_hiwat) + response.buf, response.flusher(), size_t(event_auth_flush_hiwat) }; json::stack::object top{out}; diff --git a/modules/federation/get_missing_events.cc b/modules/federation/get_missing_events.cc index d17a53a21..6abf66418 100644 --- a/modules/federation/get_missing_events.cc +++ b/modules/federation/get_missing_events.cc @@ -108,26 +108,14 @@ get__missing_events(client &client, }); }}; - const unique_buffer buf - { - 96_KiB - }; - resource::response::chunked response { client, http::OK }; - const auto flush{[&response] - (const const_buffer &buf) - { - response.write(buf); - return buf; - }}; - json::stack out { - buf, flush, size_t(flush_hiwat) + response.buf, response.flusher(), size_t(flush_hiwat) }; json::stack::object top{out}; diff --git a/modules/federation/state.cc b/modules/federation/state.cc index 745ac205f..83a79f66c 100644 --- a/modules/federation/state.cc +++ b/modules/federation/state.cc @@ -62,26 +62,14 @@ get__state(client &client, room }; - const unique_buffer buf - { - 96_KiB - }; - resource::response::chunked response { client, http::OK }; - const auto flush{[&response] - (const const_buffer &buf) - { - response.write(buf); - return buf; - }}; - json::stack out { - buf, flush, size_t(state_flush_hiwat) + response.buf, response.flusher(), size_t(state_flush_hiwat) }; json::stack::object top{out}; diff --git a/modules/federation/state_ids.cc b/modules/federation/state_ids.cc index 3e6ce4e2f..c69505bae 100644 --- a/modules/federation/state_ids.cc +++ b/modules/federation/state_ids.cc @@ -55,22 +55,15 @@ get__state_ids(client &client, room }; - const unique_buffer buf - { - 8_KiB - }; - resource::response::chunked response { client, http::OK }; - json::stack out{buf, [&response] - (const const_buffer &buf) + json::stack out { - response.write(buf); - return buf; - }}; + response.buf, response.flusher() + }; json::stack::object top{out}; json::stack::member pdus_m