Class: HotTub::Sessions

Inherits:
Object
  • Object
show all
Includes:
KnownClients, Reaper::Mixin
Defined in:
lib/hot_tub/sessions.rb

Constant Summary collapse

MissingSession =
Class.new(Exception)

Constants included from KnownClients

KnownClients::KNOWN_CLIENTS

Instance Attribute Summary collapse

Attributes included from Reaper::Mixin

#reap_timeout, #reaper, #shutdown

Instance Method Summary collapse

Methods included from Reaper::Mixin

#kill_reaper, #spawn_reaper

Methods included from KnownClients

#clean_client, #close_client, #reap_client?

Constructor Details

#initialize(opts = {}) ⇒ Sessions

HotTub::Sessions simplifies managing multiple Pools in a single object and using a single Reaper.

Example:

url  = "http://somewebservice.com"
url2 = "http://somewebservice2.com"

sessions = HotTub::Sessions
sessions.add(url,{:size => 12}) {
  uri = URI.parse(url)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = false
  http.start
  http
 }
sessions.add(url2,{:size => 5}) {
  Excon.new(url2)
 }

sessions.run(url) do |conn|
  p conn.head('/').code
end

sessions.run(url2) do |conn|
  p conn.head('/').code
end

OPTIONS

[:name]
  A string representing the name of your sessions used for logging.

[:reaper]
  If set to false prevents a HotTub::Reaper from initializing.

[:reap_timeout]
  Default is 600 seconds. An integer that represents the timeout for reaping the pool in seconds.


46
47
48
49
50
51
52
53
54
55
56
# File 'lib/hot_tub/sessions.rb', line 46

def initialize(opts={})
  @name             = (opts[:name] || self.class.name)
  @reaper           = opts[:reaper]
  @reap_timeout     = (opts[:reap_timeout] || 600)

  @_sessions        = {}
  @mutex            = Mutex.new
  @shutdown         = false

  at_exit {shutdown!}
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



6
7
8
# File 'lib/hot_tub/sessions.rb', line 6

def name
  @name
end

Instance Method Details

#clean!Object



102
103
104
105
106
107
108
109
110
111
# File 'lib/hot_tub/sessions.rb', line 102

def clean!
  HotTub.logger.info "[HotTub] Cleaning #{@name}!" if HotTub.logger
  @mutex.synchronize do
    @_sessions.each_value do |pool|
      break if @shutdown
      pool.clean!
    end
  end
  nil
end

#delete(key) ⇒ Object

Deletes and shutdowns the pool if its found.



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/hot_tub/sessions.rb', line 75

def delete(key)
  deleted = false
  pool = nil
  @mutex.synchronize do
    pool = @_sessions.delete(key)
  end
  if pool
    pool.reset!
    deleted = true
    HotTub.logger.info "[HotTub] #{key} was deleted from #{@name}." if HotTub.logger
  end
  deleted
end

#drain!Object



113
114
115
116
117
118
119
120
121
122
# File 'lib/hot_tub/sessions.rb', line 113

def drain!
  HotTub.logger.info "[HotTub] Draining #{@name}!" if HotTub.logger
  @mutex.synchronize do
    @_sessions.each_value do |pool|
      break if @shutdown
      pool.drain!
    end
  end
  nil
end

#fetch(key) ⇒ Object Also known as: []

Raises:



89
90
91
92
93
# File 'lib/hot_tub/sessions.rb', line 89

def fetch(key)
  pool = @_sessions[key]
  raise MissingSession, "A session could not be found for #{key.inspect} #{@name}" unless pool
  pool
end

#get_or_set(key, pool_options = {}, &client_block) ⇒ Object Also known as: add

Adds a new HotTub::Pool for the given key unless one already exists.

Raises:

  • (ArgumentError)


60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/hot_tub/sessions.rb', line 60

def get_or_set(key, pool_options={}, &client_block)
  raise ArgumentError, 'a block that initializes a new client is required.' unless block_given?
  pool = nil
  return pool if pool = @_sessions[key]
  pool_options[:sessions] = true
  pool_options[:name] = "#{@name} - #{key}"
  @mutex.synchronize do
    @reaper ||= spawn_reaper if @reaper.nil?
    pool = @_sessions[key] ||= HotTub::Pool.new(pool_options, &client_block) unless @shutdown
  end
  pool
end

#reap!Object

Remove and close extra clients



151
152
153
154
155
156
157
158
159
160
# File 'lib/hot_tub/sessions.rb', line 151

def reap!
  HotTub.logger.info "[HotTub] Reaping #{@name}!" if HotTub.log_trace?
  @mutex.synchronize do
    @_sessions.each_value do |pool|
      break if @shutdown
      pool.reap!
    end
  end
  nil
end

#reset!Object



124
125
126
127
128
129
130
131
132
133
# File 'lib/hot_tub/sessions.rb', line 124

def reset!
  HotTub.logger.info "[HotTub] Resetting #{@name}!" if HotTub.logger
  @mutex.synchronize do
    @_sessions.each_value do |pool|
      break if @shutdown
      pool.reset!
    end
  end
  nil
end

#run(key, &run_block) ⇒ Object



97
98
99
100
# File 'lib/hot_tub/sessions.rb', line 97

def run(key, &run_block)
  pool = fetch(key)
  pool.run &run_block
end

#shutdown!Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/hot_tub/sessions.rb', line 135

def shutdown!
  @shutdown = true
  HotTub.logger.info "[HotTub] Shutting down #{@name}!" if HotTub.logger
  begin
    kill_reaper
  ensure
    @mutex.synchronize do
      @_sessions.each_value do |pool|
        pool.shutdown!
      end
    end
  end
  nil
end