Sha256: 4511e89783ddcb2cb5018186fef8e8945730dfda4eb76abb0a46c5ca0fa3d422

Contents?: true

Size: 708 Bytes

Versions: 3

Compression:

Stored size: 708 Bytes

Contents

require 'faraday'


module Saddle::Middleware

  # Public: Enforces a ruby timeout on the request and throws one consistent
  # exception for all classes of timeout, internal or from faraday.
  # :timeout must be present in the request or client options
  class RubyTimeout < Faraday::Middleware

    def call(env)
      timeout = env[:request][:timeout] # nil or 0 means no timeout
      Timeout.timeout(timeout, Saddle::TimeoutError) do
        @app.call(env)
      end
    # It is possible that faraday will catch the timeout first and throw
    # this exception, rethrow as a class derived from standard error.
    rescue Faraday::Error::TimeoutError
      raise Saddle::TimeoutError
    end

  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
saddle-0.0.12 lib/saddle/middleware/ruby_timeout.rb
saddle-0.0.11 lib/saddle/middleware/ruby_timeout.rb
saddle-0.0.10 lib/saddle/middleware/ruby_timeout.rb