More refactoring in LBRYStreamProducer
This commit is contained in:
parent
f245822814
commit
a6fcd5d1f2
1 changed files with 10 additions and 8 deletions
|
@ -246,9 +246,9 @@ class EncryptedFileStreamer(object):
|
||||||
|
|
||||||
# How long to wait between sending blocks (needed because some
|
# How long to wait between sending blocks (needed because some
|
||||||
# video players freeze up if you try to send data too fast)
|
# video players freeze up if you try to send data too fast)
|
||||||
stream_interval = 0.02
|
stream_interval = 0.01
|
||||||
|
|
||||||
# How long to wait before checking again
|
# How long to wait before checking if new data has been appended to the file
|
||||||
new_data_check_interval = 0.25
|
new_data_check_interval = 0.25
|
||||||
|
|
||||||
|
|
||||||
|
@ -287,13 +287,15 @@ class EncryptedFileStreamer(object):
|
||||||
if not self._running:
|
if not self._running:
|
||||||
return
|
return
|
||||||
|
|
||||||
|
# Clear the file's EOF indicator by seeking to current position
|
||||||
|
self._file.seek(self._file.tell())
|
||||||
|
|
||||||
data = self._file.read(self.bufferSize)
|
data = self._file.read(self.bufferSize)
|
||||||
if data:
|
if data:
|
||||||
self._request.write(data)
|
self._request.write(data)
|
||||||
|
if self._running: # .write() can trigger a pause
|
||||||
self._deferred.addCallback(lambda _: task.deferLater(reactor, self.stream_interval, self._check_for_new_data))
|
self._deferred.addCallback(lambda _: task.deferLater(reactor, self.stream_interval, self._check_for_new_data))
|
||||||
else:
|
else:
|
||||||
# We've written all the bytes currently in the file, but we may
|
|
||||||
# still be downloading, so check file status to see if we're done.
|
|
||||||
self._deferred.addCallback(lambda _: self._file_manager.get_lbry_file_status(self._stream))
|
self._deferred.addCallback(lambda _: self._file_manager.get_lbry_file_status(self._stream))
|
||||||
self._deferred.addCallback(_recurse_or_stop)
|
self._deferred.addCallback(_recurse_or_stop)
|
||||||
|
|
||||||
|
@ -310,8 +312,8 @@ class EncryptedFileStreamer(object):
|
||||||
self._deferred.addErrback(lambda err: err.trap(defer.CancelledError))
|
self._deferred.addErrback(lambda err: err.trap(defer.CancelledError))
|
||||||
self._deferred.addErrback(lambda err: err.trap(error.ConnectionDone))
|
self._deferred.addErrback(lambda err: err.trap(error.ConnectionDone))
|
||||||
self._deferred.cancel()
|
self._deferred.cancel()
|
||||||
self._request.finish()
|
|
||||||
self._request.unregisterProducer()
|
self._request.unregisterProducer()
|
||||||
|
self._request.finish()
|
||||||
|
|
||||||
|
|
||||||
class HostedEncryptedFile(resource.Resource):
|
class HostedEncryptedFile(resource.Resource):
|
||||||
|
@ -325,8 +327,8 @@ class HostedEncryptedFile(resource.Resource):
|
||||||
producer = EncryptedFileStreamer(request, path, stream, self._api.lbry_file_manager)
|
producer = EncryptedFileStreamer(request, path, stream, self._api.lbry_file_manager)
|
||||||
request.registerProducer(producer, streaming=True)
|
request.registerProducer(producer, streaming=True)
|
||||||
|
|
||||||
request.notifyFinish().addCallback(lambda _: producer.stopProducing())
|
d = request.notifyFinish()
|
||||||
request.notifyFinish().addErrback(self._responseFailed, d)
|
d.addErrback(self._responseFailed, d)
|
||||||
return d
|
return d
|
||||||
|
|
||||||
def render_GET(self, request):
|
def render_GET(self, request):
|
||||||
|
|
Loading…
Reference in a new issue