Class: ThriftRack::Atom

Inherits:
Object
  • Object
show all
Defined in:
lib/thrift_rack/atom.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Atom

Returns a new instance of Atom.



3
4
5
# File 'lib/thrift_rack/atom.rb', line 3

def initialize(app)
  @app = app
end

Class Method Details

.redis=(r) ⇒ Object

compatibility with old version



26
27
28
# File 'lib/thrift_rack/atom.rb', line 26

def redis=(r)
  ThriftRack.redis = r
end

Instance Method Details

#call(env) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 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