no_one
Very high memory usage when streaming file with Phoenix
Hello, I have discovered Elixir recently and I have decided to do some projects with it (and Phoenix).
I need to take file from one server and stream it to client through my server. I wanted to do it with streams. My HttpStream looks almost exactly as this: https://labs.civilcode.io/elixir/2018/05/03/stream-files.html.
Streaming works but when I want to access bigger file (for example ~4 GB movie) my application consumes large amounts of memory, and after ~20 seconds video stops playing.
My controller’s function looks like this:
def show(conn, _params) do
url = "http://localhost:8080/Bigfile.mp4"
%{headers: proxy_headers} = HTTPoison.head!(url)
proxy_headers = for {k, v} <- proxy_headers, do: {String.downcase(k), v}, into: %{}
chunked_conn =
conn
|> put_resp_content_type("video/mp4")
|> put_resp_header("content-length", proxy_headers["content-length"])
|> send_chunked(200)
url
|> HttpStream.stream()
|> Stream.map(fn n -> chunk(chunked_conn, n) end)
|> Stream.run()
end
It’s not beautiful but I’ll improve that later. From my observations chunk/2 is called too fast (if I insert :timer.sleep(1) before stream_next(resp) memory usage is normal but it’s much slower).
This is memory usage from :observer (images are from imgur and they appear to be cropped here):
https://imgur.com/SSOjNie
And very interesting thing is that one process has very long message queue:
https://imgur.com/leANyk4
How to fix this?
Marked As Solved
wanton7
Nice I accidentally deleted my reply and forum says “You’ve performed this action too many times. Please wait 23 hours before trying again.” when I try to undelete. I’m pretty sure I haven’t undeleted anything before and post will deleted 24 hours ![]()
Here are those links again
https://github.com/elixir-plug/plug_cowboy/issues/10
https://github.com/ninenines/cowboy/issues/1376
Also Liked
no_one
wanton7
Tried your code and downgraded {:plug_cowboy, "~> 2.0"} to {:plug_cowboy, "~> 1.0"} in deps and it seems to fix this problem and memory doesn’t increase anymore.
no_one
Still nothing, it loads whole file into memory (it uses 4.6 GB of RAM, almost exact size of my video)
EDIT: Maybe I should use something else than conn, but what and how integrate it into Phoenix app?
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex









