Sha256: b9eb78289011fc0cec536278764dfb96f909a704ff1c930d5835450ad7f32e6c

Contents?: true

Size: 813 Bytes

Versions: 10

Compression:

Stored size: 813 Bytes

Contents

require 'rest-core/middleware'
require 'rest-core/timer'

class RestCore::Timeout
  def self.members; [:timeout]; end
  include RestCore::Middleware

  def call env, &k
    return app.call(env, &k) if env[DRY] || timeout(env) == 0
    monitor(env){ |e|
      app.call(e){ |r|
        if r[ASYNC] ||
           !(exp = (r[FAIL]||[]).find{ |f| f.kind_of?(::Timeout::Error) })
          # we do nothing special for callback and rest-client
          k.call(r)
        else
          # it would go to this branch only under response future
          raise exp
        end}}
  end

  def monitor env
    timer = Timer.new(timeout(env), timeout_error)
    yield(env.merge(TIMER => timer))
  rescue Exception
    timer.cancel
    raise
  end

  def timeout_error
    ::Timeout::Error.new('execution expired')
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
rest-core-3.4.1 lib/rest-core/middleware/timeout.rb
rest-core-3.4.0 lib/rest-core/middleware/timeout.rb
rest-core-3.3.3 lib/rest-core/middleware/timeout.rb
rest-core-3.3.2 lib/rest-core/middleware/timeout.rb
rest-core-3.3.1 lib/rest-core/middleware/timeout.rb
rest-core-3.3.0 lib/rest-core/middleware/timeout.rb
rest-core-3.2.0 lib/rest-core/middleware/timeout.rb
rest-core-3.1.1 lib/rest-core/middleware/timeout.rb
rest-core-3.1.0 lib/rest-core/middleware/timeout.rb
rest-core-3.0.0 lib/rest-core/middleware/timeout.rb