Skip to content

Commit

Permalink
Send a large file separetedly instead of loading the whole content on…
Browse files Browse the repository at this point in the history
… memory.
  • Loading branch information
fukamachi committed Aug 12, 2024
1 parent a1b4680 commit ba0dcb3
Showing 1 changed file with 18 additions and 18 deletions.
36 changes: 18 additions & 18 deletions src/ev/socket.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -175,26 +175,26 @@
(setf (socket-write-cb socket) write-cb))
(let ((file-size (file-length stream))
(buffer (socket-buffer socket)))
;; Fill the last vector of fast-io's buffer
(let* ((start (fast-io::output-buffer-fill buffer))
(end
(read-sequence (fast-io::output-buffer-vector buffer)
stream
:start start)))
(setf (fast-io::output-buffer-fill buffer) end)
(incf (fast-io::output-buffer-len buffer)
(- end start)))
(unless (= (file-position stream) file-size)
;; Load the rest of file contents directly into the fast-io's buffer
(loop
(fast-io::extend buffer)
(let ((n
(read-sequence (fast-io::output-buffer-vector buffer)
stream)))
(setf (fast-io::output-buffer-fill buffer) n)
(incf (fast-io::output-buffer-len buffer) n))
(when (= (file-position stream) file-size)
(return)))))))
(let* ((start (fast-io::output-buffer-fill buffer))
(end
(read-sequence (fast-io::output-buffer-vector buffer)
stream
:start start)))
(setf (fast-io::output-buffer-fill buffer) end)
(incf (fast-io::output-buffer-len buffer)
(- end start)))
(cond
((= (file-position stream) file-size)
(return))
;; Prevent from loading a too large file on memory.
;; TODO: Allow to set the threshold by users.
((< 1048576 (fast-io::output-buffer-len buffer))
(and (flush-buffer socket)
(reset-buffer socket)))
(t
(fast-io::extend buffer))))))))

(declaim (inline reset-buffer))
(defun reset-buffer (socket)
Expand Down

0 comments on commit ba0dcb3

Please # to comment.