Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit 2f4d60a

Browse files
authored
Iteratively encode JSON responses to avoid blocking the reactor. (#8013)
1 parent 25e55d2 commit 2f4d60a

File tree

5 files changed

+94
-13
lines changed

5 files changed

+94
-13
lines changed

changelog.d/8013.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Iteratively encode JSON to avoid blocking the reactor.

synapse/http/server.py

Lines changed: 89 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@
2222
import urllib
2323
from http import HTTPStatus
2424
from io import BytesIO
25-
from typing import Any, Callable, Dict, Tuple, Union
25+
from typing import Any, Callable, Dict, Iterator, List, Tuple, Union
2626

2727
import jinja2
28-
from canonicaljson import encode_canonical_json, encode_pretty_printed_json
28+
from canonicaljson import iterencode_canonical_json, iterencode_pretty_printed_json
29+
from zope.interface import implementer
2930

30-
from twisted.internet import defer
31+
from twisted.internet import defer, interfaces
3132
from twisted.python import failure
3233
from twisted.web import resource
3334
from twisted.web.server import NOT_DONE_YET, Request
@@ -499,6 +500,78 @@ class RootOptionsRedirectResource(OptionsResource, RootRedirect):
499500
pass
500501

501502

503+
@implementer(interfaces.IPullProducer)
504+
class _ByteProducer:
505+
"""
506+
Iteratively write bytes to the request.
507+
"""
508+
509+
# The minimum number of bytes for each chunk. Note that the last chunk will
510+
# usually be smaller than this.
511+
min_chunk_size = 1024
512+
513+
def __init__(
514+
self, request: Request, iterator: Iterator[bytes],
515+
):
516+
self._request = request
517+
self._iterator = iterator
518+
519+
def start(self) -> None:
520+
self._request.registerProducer(self, False)
521+
522+
def _send_data(self, data: List[bytes]) -> None:
523+
"""
524+
Send a list of strings as a response to the request.
525+
"""
526+
if not data:
527+
return
528+
self._request.write(b"".join(data))
529+
530+
def resumeProducing(self) -> None:
531+
# We've stopped producing in the meantime (note that this might be
532+
# re-entrant after calling write).
533+
if not self._request:
534+
return
535+
536+
# Get the next chunk and write it to the request.
537+
#
538+
# The output of the JSON encoder is coalesced until min_chunk_size is
539+
# reached. (This is because JSON encoders produce a very small output
540+
# per iteration.)
541+
#
542+
# Note that buffer stores a list of bytes (instead of appending to
543+
# bytes) to hopefully avoid many allocations.
544+
buffer = []
545+
buffered_bytes = 0
546+
while buffered_bytes < self.min_chunk_size:
547+
try:
548+
data = next(self._iterator)
549+
buffer.append(data)
550+
buffered_bytes += len(data)
551+
except StopIteration:
552+
# The entire JSON object has been serialized, write any
553+
# remaining data, finalize the producer and the request, and
554+
# clean-up any references.
555+
self._send_data(buffer)
556+
self._request.unregisterProducer()
557+
self._request.finish()
558+
self.stopProducing()
559+
return
560+
561+
self._send_data(buffer)
562+
563+
def stopProducing(self) -> None:
564+
self._request = None
565+
566+
567+
def _encode_json_bytes(json_object: Any) -> Iterator[bytes]:
568+
"""
569+
Encode an object into JSON. Returns an iterator of bytes.
570+
"""
571+
for chunk in json_encoder.iterencode(json_object):
572+
yield chunk.encode("utf-8")
573+
574+
502575
def respond_with_json(
503576
request: Request,
504577
code: int,
@@ -533,15 +606,23 @@ def respond_with_json(
533606
return None
534607

535608
if pretty_print:
536-
json_bytes = encode_pretty_printed_json(json_object) + b"\n"
609+
encoder = iterencode_pretty_printed_json
537610
else:
538611
if canonical_json or synapse.events.USE_FROZEN_DICTS:
539-
# canonicaljson already encodes to bytes
540-
json_bytes = encode_canonical_json(json_object)
612+
encoder = iterencode_canonical_json
541613
else:
542-
json_bytes = json_encoder.encode(json_object).encode("utf-8")
614+
encoder = _encode_json_bytes
615+
616+
request.setResponseCode(code)
617+
request.setHeader(b"Content-Type", b"application/json")
618+
request.setHeader(b"Cache-Control", b"no-cache, no-store, must-revalidate")
543619

544-
return respond_with_json_bytes(request, code, json_bytes, send_cors=send_cors)
620+
if send_cors:
621+
set_cors_headers(request)
622+
623+
producer = _ByteProducer(request, encoder(json_object))
624+
producer.start()
625+
return NOT_DONE_YET
545626

546627

547628
def respond_with_json_bytes(

synapse/python_dependencies.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
"jsonschema>=2.5.1",
4444
"frozendict>=1",
4545
"unpaddedbase64>=1.1.0",
46-
"canonicaljson>=1.2.0",
46+
"canonicaljson>=1.3.0",
4747
# we use the type definitions added in signedjson 1.1.
4848
"signedjson>=1.1.0",
4949
"pynacl>=1.2.1",

synapse/rest/key/v2/remote_key_resource.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515
import logging
1616
from typing import Dict, Set
1717

18-
from canonicaljson import encode_canonical_json, json
18+
from canonicaljson import json
1919
from signedjson.sign import sign_json
2020

2121
from synapse.api.errors import Codes, SynapseError
2222
from synapse.crypto.keyring import ServerKeyFetcher
23-
from synapse.http.server import DirectServeJsonResource, respond_with_json_bytes
23+
from synapse.http.server import DirectServeJsonResource, respond_with_json
2424
from synapse.http.servlet import parse_integer, parse_json_object_from_request
2525

2626
logger = logging.getLogger(__name__)
@@ -223,4 +223,4 @@ async def query_keys(self, request, query, query_remote_on_cache_miss=False):
223223

224224
results = {"server_keys": signed_keys}
225225

226-
respond_with_json_bytes(request, 200, encode_canonical_json(results))
226+
respond_with_json(request, 200, results, canonical_json=True)

tests/test_server.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,6 @@ def _callback(request, **kwargs):
178178

179179
self.assertEqual(channel.result["code"], b"200")
180180
self.assertNotIn("body", channel.result)
181-
self.assertEqual(channel.headers.getRawHeaders(b"Content-Length"), [b"15"])
182181

183182

184183
class OptionsResourceTests(unittest.TestCase):

0 commit comments

Comments
 (0)