Sha256: da55b600994315d5d9e47b67cf15c12bd4ba21e6c7177c110dd316a23c96b26f

Contents?: true

Size: 1.18 KB

Versions: 8

Compression:

Stored size: 1.18 KB

Contents

require 'rest-core/middleware'

require 'timeout'

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
    class_name = case name = run.class.to_s
                 when /Auto/
                   run.http_client.class.to_s
                 else
                   name
                 end

    timer = case class_name
            when /EmHttpRequest/
              TimerEm
            else
              TimerThread
            end.new(timeout(env), timeout_error)

    yield(env.merge(TIMER => timer))
  end

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

  autoload :TimerEm    , 'rest-core/middleware/timeout/timer_em'
  autoload :TimerThread, 'rest-core/middleware/timeout/timer_thread'
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
rest-core-2.1.2 lib/rest-core/middleware/timeout.rb
rest-core-2.1.1 lib/rest-core/middleware/timeout.rb
rest-core-2.1.0 lib/rest-core/middleware/timeout.rb
rest-core-2.0.4 lib/rest-core/middleware/timeout.rb
rest-core-2.0.3 lib/rest-core/middleware/timeout.rb
rest-core-2.0.2 lib/rest-core/middleware/timeout.rb
rest-core-2.0.1 lib/rest-core/middleware/timeout.rb
rest-core-2.0.0 lib/rest-core/middleware/timeout.rb