class ThriftRack::Atom

Public Class Methods

new(app) click to toggle source
# File lib/thrift_rack/atom.rb, line 3
def initialize(app)
  @app = app
end
redis=(r) click to toggle source

compatibility with old version

# File lib/thrift_rack/atom.rb, line 26
def redis=(r)
  ThriftRack.redis = r
end

Public Instance Methods

call(env) click to toggle source
# File lib/thrift_rack/atom.rb, line 7
def call(env)
  req = Rack::Request.new(env)
  rpc_id = req.env["HTTP_X_RPC_ID"]
  if rpc_id
    start_time = Time.now
    valid = ThriftRack.redis.set("thrift_rack:atom:#{rpc_id}", true, nx: true, ex: 180)
    if valid
      env["ATOM_DURATION"] = ((Time.now - start_time) * 1000).round(4)
      @app.call(env)
    else
      [409, {}, ["RPC Request Processed"]]
    end
  else
    @app.call(env)
  end
end