mirror of
https://mau.dev/maunium/synapse.git
synced 2024-08-25 03:35:28 +00:00
Make Responder a context manager
This commit is contained in:
parent
dcc8eded41
commit
85a4d78213
@ -144,15 +144,16 @@ def respond_with_responder(request, responder, media_type, file_size, upload_nam
|
|||||||
return
|
return
|
||||||
|
|
||||||
add_file_headers(request, media_type, file_size, upload_name)
|
add_file_headers(request, media_type, file_size, upload_name)
|
||||||
yield responder.write_to_consumer(request)
|
with responder:
|
||||||
|
yield responder.write_to_consumer(request)
|
||||||
finish_request(request)
|
finish_request(request)
|
||||||
|
|
||||||
|
|
||||||
class Responder(object):
|
class Responder(object):
|
||||||
"""Represents a response that can be streamed to the requester.
|
"""Represents a response that can be streamed to the requester.
|
||||||
|
|
||||||
Either `write_to_consumer` or `cancel` must be called to clean up any open
|
Responder is a context manager which *must* be used, so that any resources
|
||||||
resources.
|
held can be cleaned up.
|
||||||
"""
|
"""
|
||||||
def write_to_consumer(self, consumer):
|
def write_to_consumer(self, consumer):
|
||||||
"""Stream response into consumer
|
"""Stream response into consumer
|
||||||
@ -165,9 +166,10 @@ class Responder(object):
|
|||||||
"""
|
"""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def cancel(self):
|
def __enter__(self):
|
||||||
"""Called when the responder is not going to be used after all.
|
pass
|
||||||
"""
|
|
||||||
|
def __exit__(self, exc_type, exc_val, exc_tb):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
@ -220,8 +220,7 @@ class FileResponder(Responder):
|
|||||||
|
|
||||||
@defer.inlineCallbacks
|
@defer.inlineCallbacks
|
||||||
def write_to_consumer(self, consumer):
|
def write_to_consumer(self, consumer):
|
||||||
with self.open_file:
|
yield FileSender().beginFileTransfer(self.open_file, consumer)
|
||||||
yield FileSender().beginFileTransfer(self.open_file, consumer)
|
|
||||||
|
|
||||||
def cancel(self):
|
def __exit__(self, exc_type, exc_val, exc_tb):
|
||||||
self.open_file.close()
|
self.open_file.close()
|
||||||
|
Loading…
Reference in New Issue
Block a user