Permalink
Browse files

some fixes.. tbf

  • Loading branch information...
nofxx committed Jul 7, 2009
1 parent 1d2d03e commit 6dbacdbb4d3e73d8d652bb83283d286a6d04fcb6
Showing with 24 additions and 33 deletions.
  1. +13 −31 lib/rack/session/tokyo.rb
  2. +11 −2 spec/rack/session/tokyo_spec.rb
View
@@ -7,18 +7,22 @@ class Tokyo < Abstract::ID
def initialize(app, options = {})
super
@mutex = Mutex.new
- host, port = *options[:tyrant_server] || @default_options[:tyrant_server].split(":") # @default_options) #options[:cache] ||
+ @host, @port = *(options[:tyrant_server] || @default_options[:tyrant_server]).split(":") # @default_options) #options[:cache] ||
+ tokyo_connect
+ end
+
+ private
+ def tokyo_connect
begin
- @pool = Rufus::Tokyo::Tyrant.new(host, port.to_i)
- rescue => e
- "No server avaiable or #{e}"
+ @pool = Rufus::Tokyo::Tyrant.new(@host, @port.to_i)
+ rescue Rufus::Tokyo::TokyoError => e
+ warn "Can't connect to Tyrant #{e}"
end
end
- private
def get_session(env, sid)
@mutex.lock if env['rack.multithread']
- session = Marshal.load(@pool[sid]) rescue session if sid
+ session = Marshal.load(@pool[sid]) rescue session if sid && session = @pool[sid]
unless sid && session
env['rack.errors'].puts("Session '#{sid.inspect}' not found, initializing...") if $VERBOSE and not sid.nil?
session = {}
@@ -28,7 +32,7 @@ def get_session(env, sid)
end
session.instance_variable_set('@old', {}.merge(session))
return [sid, session]
- rescue => e
+ rescue Rufus::Tokyo::TokyoError => e
session = {}
ensure
@mutex.unlock if env['rack.multithread']
@@ -44,38 +48,16 @@ def set_session(env, sid, new_session, options)
@pool[sid] = ""
end
old_session = new_session.instance_variable_get('@old') || {}
- session = merge_sessions sid, old_session, new_session, session
+ session = new_session
@pool[sid] = options && options[:raw] ? session : Marshal.dump(session)
return sid
- rescue => e
- if e =~ /refused/
+ rescue Rufus::Tokyo::TokyoError => e
warn "#{self} is unable to find server, error: #{e}"
warn $!.inspect
- return false
- else
- raise e
- end
ensure
@mutex.unlock if env['rack.multithread']
end
- def merge_sessions(sid, old, new, cur={})
- unless Hash === old and Hash === new
- warn 'Bad old or new sessions provided.'
- return cur
- end
-
- delete = old.keys - new.keys
- warn "//@#{sid}: dropping #{delete*','}" if $DEBUG and not delete.empty?
- delete.each{|k| cur.delete k }
-
- update = new.keys.select{|k| new[k] != old[k] }
- warn "//@#{sid}: updating #{update*','}" if $DEBUG and not update.empty?
- update.each{|k| cur[k] = new[k] }
-
- cur
- end
-
def generate_sid
loop do
sid = super
@@ -3,6 +3,9 @@
module Rack
module Session
+ # class Tokyo
+ # attr_reader :
+ # end
describe "Rack::Session::Tokyo" do
before(:each) do
@session_key = Rack::Session::Tokyo::DEFAULT_OPTIONS[:key]
@@ -27,15 +30,21 @@ module Session
end
it "should specify connection params" do
- pool = Rack::Session::Tokyo.new(@incrementor, :tokyo_server => "localhost:6380/1").pool
+ pool = Rack::Session::Tokyo.new(@incrementor, :tyrant_server => "127.0.0.1:1978").pool
pool.should be_kind_of(Rufus::Tokyo::Tyrant)
- pool.host.should eql("localhost")
+ pool.host.should eql("127.0.0.1")
pool.port.should eql(1978)
# pool = Rack::Session::Tokyo.new(@incrementor, :tokyo_server => ["localhost:6379", "localhost:6380"]).pool
# pool.should be_kind_of(DistributedMarshaledTokyo)
end
+ it "should raise tokyo error on connect" do
+ lambda{ Rack::Session::Tokyo.new(@incrementor, :tyrant_server => "localhost:6380").pool }.
+ should_not raise_error(Rufus::Tokyo::TokyoError)
+ end
+
+
it "creates a new cookie" do
pool = Rack::Session::Tokyo.new(@incrementor)
res = Rack::MockRequest.new(pool).get("/")

0 comments on commit 6dbacdb

Please sign in to comment.