Class: Wee::RequestHandler

Inherits:
Object show all
Defined in:
lib/wee/requesthandler.rb

Direct Known Subclasses

AbstractSession

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRequestHandler

Returns a new instance of RequestHandler.



74
75
76
77
78
79
# File 'lib/wee/requesthandler.rb', line 74

def initialize
  @last_access = @creation_time = Time.now 
  @expire_after = 30*60                  # The default is 30 minutes of inactivity
  @request_count = 0
  @running = true
end

Instance Attribute Details

#applicationObject

Points to the Wee::Application object for which this handler is registered.



5
6
7
# File 'lib/wee/requesthandler.rb', line 5

def application
  @application
end

#expire_afterObject

Expire after this number of seconds of inactivity. If this value is nil, the RequestHandler will never expire due to inactivity (but may still due to max_lifetime).



19
20
21
# File 'lib/wee/requesthandler.rb', line 19

def expire_after
  @expire_after
end

#idObject

Each request handler of an application has a unique id, which should be non-guessable, that means it has to be cryptographically secure.

This id is used to uniquely identify a RequestHandler from each other. This is the same id used as a session id in class Wee::Session.



13
14
15
# File 'lib/wee/requesthandler.rb', line 13

def id
  @id
end

#max_lifetimeObject

The lifetime of this handler is limited to this number of seconds. A value of nil means infinite lifetime.



24
25
26
# File 'lib/wee/requesthandler.rb', line 24

def max_lifetime
  @max_lifetime
end

#max_requestsObject

The maximum number of requests this handler should serve. A value of nil means infinity.



29
30
31
# File 'lib/wee/requesthandler.rb', line 29

def max_requests
  @max_requests
end

Instance Method Details

#alive?Boolean

Query whether this handler is still alive.

Returns:

  • (Boolean)


43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/wee/requesthandler.rb', line 43

def alive?
  return false if not @running
  return @running = false if @max_requests and @request_count >= @max_requests

  now = Time.now
  inactivity = now - @last_access 
  lifetime = now - @creation_time

  return @running = false if @expire_after and inactivity > @expire_after 
  return @running = false if @max_lifetime and lifetime > @max_lifetime 
  return true
end

#handle_request(context) ⇒ Object

Extend #handle_request in your own subclass.



58
59
60
61
# File 'lib/wee/requesthandler.rb', line 58

def handle_request(context)
  @request_count += 1
  @last_access = Time.now
end

#statisticsObject



63
64
65
66
67
68
69
70
71
72
# File 'lib/wee/requesthandler.rb', line 63

def statistics
  now = Time.now
  {
    :last_access => @last_access,        # The time when this handler was last accessed
    :inactivity => now - @last_access,   # The number of seconds of inactivity
    :creation_time => @creation_time,    # The time when this handler was created 
    :lifetime =>  now - @creation_time,  # The uptime or lifetime of this handler in seconds
    :request_count => @request_count     # The number of requests served by this handler
  }
end

#teminateObject

Terminates the handler.

This will usually not immediatly terminate the handler from running, but further requests will not be answered.



37
38
39
# File 'lib/wee/requesthandler.rb', line 37

def teminate
  @running = false
end