Rails 4 - How to serve video?

前端 未结 1 1218
遥遥无期
遥遥无期 2021-01-03 17:10

I\'m using Rails 4 for my web application and I\'m using VideoJS to display it, like:

相关标签:
1条回答
  • 2021-01-03 17:17

    Don't use rails to serve video files. Although servers such as unicorn support streaming video files there are a few reasons why it's a bad idea. There are vastly different connections at play here. A rails app generally should take no longer than ~200ms to return a response. When your streaming video it would not be uncommon for the connection to stay open for much longer.

    Why does that matter? Consider for a moment that a rails web sever designed to run ruby code and produce text based output. To do this as quickly as possible very smart people make a number of decisions when writing servers such as puma and raptor. In the case of raptor I know they never allocate a new object during runtime as it's too expensive. They instead maintain a pool of objects for handling incoming requests. The request will be handed and then the objects in the pool released. That's fine for short lived requests but what if you only have 5 objects in your pool? Your sever will very quickly run out of connections as soon as you have 5 people watching a video on your site. While this is happening your site is down to the rest of the world. For more details on how raptor works see http://www.rubyraptor.org/how-we-made-raptor-up-to-4x-faster-than-unicorn-and-up-to-2x-faster-than-puma-torquebox/

    Another reason is memory use. A rails server is doing far more complex and high level processing than say nginx. It has smarts in it like connecting to databases and handing sessions. These things don't come for free and cost memory on your server. A front end server such as nginx does not have any of this overhead and can serve video files in the exact same way.

    Ideally rails should not even serve static assets such as JS and CSS. Your better configuring your forward facing web server to do this directly to the client. You will have less overhead. Video is a whole other issue on top of that. I would suggest looking at an external hosting service built for hosting video. Amazon has S3 and I am sure there are a few others.

    The same argument would go for WEBRick. It's a development server that does not even have the benefit of being compiled. It's pure ruby so it's going to be even worse than say raptor or puma.

    0 讨论(0)
提交回复
热议问题