Class: XRuntime::Middleware
- Inherits:
-
Object
- Object
- XRuntime::Middleware
- Defined in:
- lib/x_runtime/middleware.rb
Instance Attribute Summary collapse
-
#auth ⇒ Object
Returns the value of attribute auth.
-
#redis ⇒ Object
Returns the value of attribute redis.
Instance Method Summary collapse
- #call(env) ⇒ Object
- #ds ⇒ Object
-
#initialize(app, redis, opts = {}) ⇒ Middleware
constructor
threshold => ms.
- #logredis(cost, uri) ⇒ Object
- #redis_key ⇒ Object
- #script ⇒ Object
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
#auth ⇒ Object
Returns the value of attribute auth.
3 4 5 |
# File 'lib/x_runtime/middleware.rb', line 3 def auth @auth end |
#redis ⇒ Object
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 |
#ds ⇒ Object
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 |