Class: XRuntime::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/x_runtime/middleware.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, redis, opts = {}) ⇒ Middleware

threshold => ms



5
6
7
8
9
10
11
12
13
# File 'lib/x_runtime/middleware.rb', line 5

def initialize(app, redis, opts = {})
  @app = app
  @redis = redis
  opts = {:threshold => 100.0, :cache => 50, :expire => 120}.update opts
  @cache = opts[:cache].to_i
  @expire = opts[:expire].to_i
  @threshold = opts[:threshold].to_f
  XRuntime.middleware = self
end

Instance Attribute Details

#authObject

Returns the value of attribute auth.



3
4
5
# File 'lib/x_runtime/middleware.rb', line 3

def auth
  @auth
end

#redisObject

Returns the value of attribute redis.



3
4
5
# File 'lib/x_runtime/middleware.rb', line 3

def redis
  @redis
end

Instance Method Details

#call(env) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/x_runtime/middleware.rb', line 27

def call(env)
  start_time = Time.now
  status, headers, body = @app.call(env)
  request_time = (Time.now - start_time).to_f*1000
  if request_time >= @threshold
    logredis(request_time, env['REQUEST_PATH'])
  end

  [status, headers, body]
end

#dsObject



15
16
17
# File 'lib/x_runtime/middleware.rb', line 15

def ds
  @ds ||= DataSet.new(redis_key, script, @cache, @expire)
end

#logredis(cost, uri) ⇒ Object



38
39
40
# File 'lib/x_runtime/middleware.rb', line 38

def logredis(cost,uri)
  ds.add(uri, cost) rescue nil
end

#redis_keyObject



23
24
25
# File 'lib/x_runtime/middleware.rb', line 23

def redis_key
  @key ||= "#{XRuntime::NameSpace}::#{@threshold}"
end

#scriptObject



19
20
21
# File 'lib/x_runtime/middleware.rb', line 19

def script
  @script ||= Script.new(@redis)
end