Grape中的代码以下:git
def market @market ||= Market.find(params[:id]) end
@market基于类层次的实例变量,属于非线程安全,若是一直使用多线程服务器,可使用 Thread.current
代替:github
def market Thread.current[:market] ||= Market.find(params[:id]) end
若是考虑到之后哦使用其余类型服务器,好比 Thin, 可使用 request_store gem 包,参考: https://github.com/steveklabnik/request_store安全
参考:
https://github.com/ruby-grape/grape-rabl/issues/37
https://ruby-china.org/topics/30188
https://stackoverflow.com/questions/9558192/thread-safety-class-variables-in-rubyruby