Class: RestCore::Timeout

Inherits:
Object
  • Object
show all
Includes:
Middleware
Defined in:
lib/rest-core/middleware/timeout.rb

Constant Summary

Constants included from Middleware

Middleware::UNRESERVED

Constants included from RestCore

ASYNC, CLIENT, DRY, FAIL, HIJACK, LOG, PROMISE, REQUEST_HEADERS, REQUEST_METHOD, REQUEST_PATH, REQUEST_PAYLOAD, REQUEST_QUERY, REQUEST_URI, RESPONSE_BODY, RESPONSE_HEADERS, RESPONSE_KEY, RESPONSE_SOCKET, RESPONSE_STATUS, Simple, TIMER, Universal, VERSION

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Middleware

contain_binary?, #contain_binary?, escape, #escape, #fail, #id, included, #log, merge_hash, #merge_hash, #percent_encode, percent_encode, request_uri, #request_uri, #run, #string_keys, string_keys

Methods included from RestCore

eagerload, id

Class Method Details

.membersObject



6
# File 'lib/rest-core/middleware/timeout.rb', line 6

def self.members; [:timeout]; end

Instance Method Details

#call(env, &k) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/rest-core/middleware/timeout.rb', line 9

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

#monitor(env) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/rest-core/middleware/timeout.rb', line 23

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

#timeout_errorObject



31
32
33
# File 'lib/rest-core/middleware/timeout.rb', line 31

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