【问题标题】:How to configure Thin web server from Sinatra?如何从 Sinatra 配置 Thin Web 服务器?
【发布时间】:2019-04-03 15:54:18
【问题描述】:

这是我的网络应用:

class Front < Sinatra::Base
  configure do
    set :server, :thin
  end
  get '/' do
    'Hello, world!'
  end 
end

我是这样开始的:

Front.start!

效果很好,但我想将 Thin 配置为“线程化”。根据their documentation,我知道这是可能的。但我不知道如何将 threaded: true 参数传递给 Thin。我试过了,它不起作用:

configure do
  set :server_settings, threaded: true
end

【问题讨论】:

  • 为什么不使用thin.yml?
  • @SergioTulentsev 究竟如何?我已经准备好使用它了,但是我如何告诉 Thin 它的位置?
  • 通过命令行:thin start -C config/thin.yml
  • @SergioTulentsev 我没有从命令行启动我的 Sinatra 应用程序,正如您在上面的问题中看到的那样。
  • “我不会从命令行启动我的 Sinatra 应用程序” - 当然你可以通过 rackup 或类似的方式启动。当 Thin 启动您的应用程序时,更改其架构可能已经太晚了。实际上,我对 sinatra 让您首先选择处理程序感到有点惊讶。我会找出它是如何完成的。

标签: ruby sinatra thin


【解决方案1】:

当以您描述的方式启动时,默认情况下瘦 Web 服务器是线程化的。

# thin_test.rb
require 'sinatra/base'

class Front < Sinatra::Base
  configure do
    set :server, :thin
  end

  get '/' do
    'Hello, world!'
  end 

  get '/foo' do
    sleep 30
    'bar'
  end
end

Front.start!

开始于:

ruby thin_test.rb

确认:

# will hang for 30 seconds while sleeping
curl localhost:4567/foo

# will complete successfully while the other request is pending
curl localhost:4567
Hello, world!

this answer 中有更多关于 Sinatra 如何使用其他 Web 服务器的详细信息。

如果由于某种原因这不起作用,则可以使用 server_settings 选项来破解某些东西,这通常仅对 WEBrick 有用,除非您使用一些未记录的方法来强制它:

require 'sinatra/base'
require 'thin'

class ThreadedThinBackend < ::Thin::Backends::TcpServer
  def initialize(host, port, options)
    super(host, port)
    @threaded = true
  end
end

class Front < Sinatra::Base
  configure do
    set :server, :thin

    class << settings
      def server_settings
        { :backend => ThreadedThinBackend }
      end
    end
  end

  get '/' do
    'Hello, world!'
  end 

  get '/foo' do
    sleep 30
    'foobar'
  end
end

Front.start!

我很难判断这个示例是否是导致它被线程化的原因,因为它默认以线程模式启动。也就是说,它不会引发异常并且会在线程模式下运行:

# will hang for 30 seconds while sleeping
curl localhost:4567/foo

# will complete successfully while the other request is pending
curl localhost:4567
Hello, world!

有关server_settings 的更多信息,请访问here、here 和here。

【讨论】:

  • 这个例子真的不行。如果您将@threaded 更改为@maximum_connections = 10 并再次运行,您将在日志中看到该值仍然是1024,这是默认值。
猜你喜欢
  • 1970-01-01
  • 2012-05-24
  • 1970-01-01
  • 2012-04-24
  • 2012-03-31
  • 2012-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多