Skip to content

HTTP API: support streaming JSON responses #10407

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
julianbrost opened this issue Apr 10, 2025 · 0 comments
Open

HTTP API: support streaming JSON responses #10407

julianbrost opened this issue Apr 10, 2025 · 0 comments
Labels
area/api REST API

Comments

@julianbrost
Copy link
Contributor

Currently, responses to HTTP requests are fully assembled in memory before they are sent to the client. Given that these responses can get quite big, for example when fetching an unfiltered /v1/objects/services list in a large installation, this can result in (to put it mildly) suboptimal memory usage.

Status quo

  1. After an object is determined to be included in the response, it is serialized (this creates a copy of all its attributes):
    result1.emplace_back("attrs", SerializeObjectAttrs(obj, String(), uattrs, false, false));
  2. It is then stored in a results vector (which will contain O(response size) memory in total after all serialized objects were appended):
    results.push_back(new Dictionary(std::move(result1)));
  3. This is then passed to HttpUtility::SendJsonBody():
    Dictionary::Ptr result = new Dictionary({
    { "results", new Array(std::move(results)) }
    });
    response.result(http::status::ok);
    HttpUtility::SendJsonBody(response, params, result);
  4. Which in turn serializes the whole vector containing all the objects into a big JSON string that's then added to the response object:
    response.body() = JsonEncode(val, params && GetLastParameter(params, "pretty"));
    response.content_length(response.body().size());

Desired behavior

Stream the JSON to the client's HTTP connection on the fly while going through the objects. This should allow answering these requests using far less memory (probably something like what it takes to represent a single object).

I'll create some sub-issues to break this into smaller tasks to start with.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/api REST API
Projects
None yet
Development

No branches or pull requests

1 participant