Skip to content

Commit

Permalink
add .set for lazy loading Sessions, travis does not auto detect laste…
Browse files Browse the repository at this point in the history
…st ruby
  • Loading branch information
JoshMcKin committed Dec 22, 2015
1 parent 4237347 commit ce09b0e
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 18 deletions.
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ sudo: false
cache: bundler
language: ruby
rvm:
- 2.2
- 2.1
- 2.0.0
- 2.2.4
- 2.1.8
- 2.0.0.p648
- ruby-head
- rbx-2
- jruby-head
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,12 @@ A global Sessions object is available from the HotTub module and has several hel

# We can add more HotTub::Pools with unique settings.
# Lets add another HotTub::Pool of Excon clients with a pool size of 12.
# We are not setting :max_size so our connections will grow to match our currency.
# Once load dies down our pool will be reaped back down to 12 connections
# HotTub.set sets the options passed to a settings cache, the pool is
# created the first time we call HotTub.run. We are not setting :max_size
# so our connections will grow to match our currency. Once load dies down
# our pool will be reaped back down to 12 connections

HotTub.add('excon_yahoo', { :size => 12} ) do
HotTub.set('excon_yahoo', { :size => 12} ) do
Excon.new("https://yahoo.com", :thread_safe_sockets => false )
end

Expand Down
54 changes: 42 additions & 12 deletions lib/hot_tub/sessions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,29 +68,59 @@ def initialize(opts={}, &default_client)
@default_client = default_client
@pool_options = (opts[:pool_options] || {})

@_settings = {}
@_settings.taint
@_sessions = {}
@_sessions.taint

@mutex = Mutex.new
@shutdown = false

at_exit {shutdown!}
end

# Adds a new HotTub::Pool for the given key unless
# one already exists.
def get_or_set(key, pool_options={}, &client_block)
pool = nil
return pool if pool = @_sessions[key]
clnt_blk = (client_block || @default_client)
op = @pool_options.merge(pool_options)
op[:sessions_key] = key
op[:name] = "#{@name} - #{key}"
# Sets arguments / settings for a session that will be
# lazy loaded, returns nil because pool is not created
def set(key, pool_options={}, &client_block)
@mutex.synchronize do
@reaper ||= spawn_reaper if @reaper.nil?
pool = @_sessions[key] ||= HotTub::Pool.new(op, &clnt_blk) unless @shutdown
@_settings[key] = [pool_options,client_block]
end
nil
end

# Returns a HotTub::Pool for the given key. If a session
# is not found and the is a default_client set, a session will
# be created for the key using the default_client.
def get(key)
pool = @_sessions[key]
unless pool
@mutex.synchronize do
unless @shutdown
@reaper = spawn_reaper if @reaper.nil?
unless pool = @_sessions[key]
settings = @_settings[key]
clnt_blk = (settings[1] || @default_client)
op = @pool_options.merge(settings[0])
op[:sessions_key] = key
op[:name] = "#{@name} - #{key}"
pool = @_sessions[key] = HotTub::Pool.new(op, &clnt_blk)
end
end
end
end
pool
end

# Adds session unless it already exists and returns
# the session
def get_or_set(key, pool_options={}, &client_block)
unless @_settings[key]
@mutex.synchronize do
@_settings[key] ||= [pool_options,client_block]
end
end
get(key)
end
alias :add :get_or_set

# Deletes and shutdowns the pool if its found.
Expand All @@ -109,7 +139,7 @@ def delete(key)
end

def fetch(key)
unless pool = get_or_set(key, &@default_client)
unless pool = get(key, &@default_client)
raise MissingSession, "A session could not be found for #{key.inspect} #{@name}"
end
pool
Expand Down

0 comments on commit ce09b0e

Please sign in to comment.