mirror of
https://github.com/koreader/koreader.git
synced 2025-08-10 00:52:38 +00:00
Merge pull request #932 from chrox/master
fix run out of memory when receiving large files from Calibre
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -32,4 +32,5 @@ koreader-arm-linux-gnueabi
|
||||
koreader-arm-linux-gnueabihf
|
||||
koreader-i686-w64-mingw32
|
||||
koreader-x86_64-linux-gnu
|
||||
koreader-x86_64-pc-linux-gnu
|
||||
|
||||
|
||||
@@ -57,7 +57,13 @@ end
|
||||
|
||||
function StreamMessageQueue:waitEvent()
|
||||
local data = ""
|
||||
while czmq.zpoller_wait(self.poller, 0) ~= nil do
|
||||
-- Successive zframes may be tens or hundreds in some cases
|
||||
-- if they are concatenated in a single loop it may run up memory of the
|
||||
-- machine. And it did happened when receiving file data from Calibre server.
|
||||
-- Here we receive only receive 10 packages at most in one waitEvent loop, and
|
||||
-- call receiveCallback immediately.
|
||||
local wait_packages = 10
|
||||
while czmq.zpoller_wait(self.poller, 0) ~= nil and wait_packages > 0 do
|
||||
local id_frame = czmq.zframe_recv(self.socket)
|
||||
if id_frame ~= nil then
|
||||
local id = self:handleZframe(id_frame)
|
||||
@@ -66,6 +72,7 @@ function StreamMessageQueue:waitEvent()
|
||||
if frame ~= nil then
|
||||
data = data .. (self:handleZframe(frame) or "")
|
||||
end
|
||||
wait_packages = wait_packages - 1
|
||||
end
|
||||
if self.receiveCallback and data ~= "" then
|
||||
self.receiveCallback(data)
|
||||
|
||||
@@ -322,7 +322,9 @@ function UIManager:run()
|
||||
local force_partial_refresh = false
|
||||
local force_fast_refresh = false
|
||||
for _, widget in ipairs(self._window_stack) do
|
||||
if self.repaint_all or self._dirty[widget.widget] then
|
||||
-- paint if repaint_all is request
|
||||
-- paint also if current widget or any widget underneath is dirty
|
||||
if self.repaint_all or dirty or self._dirty[widget.widget] then
|
||||
widget.widget:paintTo(Screen.bb,
|
||||
widget.x + Screen:offsetX(),
|
||||
widget.y + Screen:offsetY())
|
||||
|
||||
@@ -332,6 +332,7 @@ function CalibreCompanion:sendBook(arg)
|
||||
-- switching to raw data receiving mode
|
||||
self.calibre_socket.receiveCallback = function(data)
|
||||
--DEBUG("receive file data", #data)
|
||||
--DEBUG("Memory usage KB:", collectgarbage("count"))
|
||||
local to_write_data = data:sub(1, to_write_bytes)
|
||||
outfile:write(to_write_data)
|
||||
to_write_bytes = to_write_bytes - #to_write_data
|
||||
|
||||
Reference in New Issue
Block a user