当前位置: 首页 > 工具软件 > dianping puma > 使用案例 >

puma为canvas-lms加速

鲜于阳
2023-12-01

描述

Puma是一个简单,快速,线程式,并且支持HTTP1.1高并发的Ruby/Rack服务器。在开发环境和生产环境中都可以使用Puma。

为速度和并发而生

Puma是一个应用于Ruby Web应用,简单,快速,支持高并发的HTTP1.1服务器。它可以和任何支持Rack的应用配合使用,是Webrick或Mongrel的替代选择。它最初被设计为Rubinius的服务器,但是配合JRuby和MRI使用性能同样出色。在开发环境和生产环境中都可以使用Puma。
在面纱之下,Puma使用经过C语言优化的Ragel扩展(继承自Mongrel)来解析请求,用一种轻便的方式进行快速,精确的HTTP 1.1协议解析。然后使用内部线程池的线程来处理请求。这使得Puma可以为你的Web应用提供真正的并发。
Rubinius 2.0后,Puma将使用真正的线程来利用CPU的所有核,因此不用再使用多进程来提高性能。可以期待我们将获得和JRuby相似的性能。
对于MRI,因为全局锁(GIL)的限制,每次只能运行一个线程。但是对于大量阻塞IO的操作(例如调用Twitter API的HTTP请求),Puma还是通过允许阻塞IO操作并行运行提高了MRI的性能(像Thin这种以EventMachine为基础的服务器关闭了这个特性,需要自己使用第三方库)。根据你的使用情况,为了获得最佳性能,强烈建议使用实现了真正多线程的Ruby版本,例如 Rubinius或JRuby。

快速开始

编辑config/puma.prod.rb


# Puma can serve each request in a thread from an internal thread pool.
# The `threads` method setting takes two numbers: a minimum and maximum.
# Any libraries that use thread pools should be configured to match
# the maximum value specified for Puma. Default is set to 5 threads for minimum
# and maximum; this matches the default thread size of Active Record.

max_threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 }

threads 1, max_threads_count

# Specifies the `port` that Puma will listen on to receive requests; default is 3000.
#
port 3000


# Specifies the `environment` that Puma will run in.
#
environment ENV.fetch("RAILS_ENV") { "production" }

# Specifies the number of `workers` to boot in clustered mode.
# Workers are forked webserver processes. If using threads and workers together
# the concurrency of the application would be max `threads` * `workers`.
# Workers do not work on JRuby or Windows (both of which do not support
# processes).
#
workers ENV.fetch("WEB_CONCURRENCY") { 2 }

pidfile "tmp/server.pid"
state_path "tmp/server.state"
stdout_redirect 'log/puma_access.log', 'log/puma_error.log', true

# Use the `preload_app!` method when specifying a `workers` number.
# This directive tells Puma to first boot the application and load code
# before forking the application. This takes advantage of Copy On Write
# process behavior so workers use less memory. If you use this option
# you need to make sure to reconnect any threads in the `on_worker_boot`
# block.
#
preload_app!

# If you are preloading your application and using Active Record, it's
# recommended that you close any connections to the database before workers
# are forked to prevent connection leakage.
#
before_fork do
  if defined?(ActiveRecord)
    ActiveRecord::Base.clear_all_connections!
  end
end

# The code in the `on_worker_boot` will be called if you are using
# clustered mode by specifying a number of `workers`. After each worker
# process is booted, this block will be run. If you are using the `preload_app!`
# option, you will want to use this block to reconnect to any threads
# or connections that may have been created at application boot, as Ruby
# cannot share connections between processes.
#
on_worker_boot do
  Canvas.reconnect_redis
  if defined?(ActiveRecord)
    ActiveRecord::Base.establish_connection 
  end
end

# Allow puma to be restarted by `rails restart` command.
plugin :tmp_restart

启动

nohup bundle exec puma -C config/puma.prod.rb &

停止 & 重启

bundle exec pumactl -S ./tmp/server.state restart
bundle exec pumactl -S ./tmp/server.state stop

交流

QQ 3133818517

 类似资料: