Permalink
Browse files

add tyrant pure ruby and cabinet C

  • Loading branch information...
nofxx committed Jul 19, 2009
1 parent 09b7bc1 commit 42077ef0af95f568c86d5e80cb7cd456cf7454a6
View
@@ -26,6 +26,13 @@
}
bar = "_" * 45
+puts "\n#{bar} SET"
+Benchmark.bmbm do |b|
+ TEST.each_pair do |n,s|
+ b.report(n) { T.times { Rack::MockRequest.new(s).get("/") }}
+ end
+end
+
puts "\n#{bar} GET"
Benchmark.bmbm do |b|
TEST.each_pair do |n,s|
@@ -37,13 +44,6 @@
end
end
-puts "\n#{bar} SET"
-Benchmark.bmbm do |b|
- TEST.each_pair do |n,s|
- b.report(n) { T.times { Rack::MockRequest.new(s).get("/") }}
- end
-end
-
puts "\n#{bar} EXIST"
Benchmark.bmbm do |b|
TEST.each_pair do |n,s|
@@ -0,0 +1,77 @@
+require 'tokyocabinet'
+module Rack
+ module Session
+ class Cabinet < Abstract::ID
+ include TokyoCabinet
+ attr_reader :mutex, :pool
+ DEFAULT_OPTIONS = Abstract::ID::DEFAULT_OPTIONS.merge :cabinet_file => "session_cabinet.tch"
+
+ def initialize(app, options = {})
+ super
+ @mutex = Mutex.new
+ @db = options[:cabinet_file] || @default_options[:cabinet_file]
+ tokyo_connect
+ end
+
+ private
+ def tokyo_connect
+ @pool = HDB.new
+ unless @pool.open(@db, HDB::OWRITER | HDB::OCREAT)
+ warn "Can't open db file #{@db}"
+ end
+ end
+
+ def get_session(env, sid)
+ # tokyo_connect
+ session = Marshal.load(@pool.get(sid)) rescue session if sid && session = @pool.get(sid)
+ @mutex.lock if env['rack.multithread']
+ unless sid && session
+ env['rack.errors'].puts("Session '#{sid.inspect}' not found, initializing...") if $VERBOSE and not sid.nil?
+ session = {}
+ sid = generate_sid
+ ret = @pool.put(sid, Marshal.dump(session))
+ raise "Session collision on '#{sid.inspect}'" unless ret
+ end
+ #session.instance_variable_put('@old', {}.merge(session))
+ return [sid, session]
+ rescue Rufus::Tokyo::TokyoError => e
+ return [nil, {}]
+ ensure
+ @mutex.unlock if env['rack.multithread']
+ # @pool.close
+ end
+
+ def set_session(env, sid, new_session, options)
+ # tokyo_connect
+ @mutex.lock if env['rack.multithread']
+ session = Marshal.load(session) rescue session if session = @pool.get(sid)
+ if options[:renew] || options[:drop]
+ @pool.out(sid)
+ return false if options[:drop]
+ sid = generate_sid
+ @pool.put(sid, "")
+ end
+ #old_session = new_session.instance_variable_get('@old') || {}
+ session = new_session
+ @pool.put(sid, options && options[:raw] ? session : Marshal.dump(session))
+ return sid
+ rescue Rufus::Tokyo::TokyoError => e
+ warn "#{self} is unable to find server, error: #{e}"
+ warn $!.inspect
+ ensure
+ @mutex.unlock if env['rack.multithread']
+ # @pool.close
+ end
+
+ def generate_sid
+ loop do
+ sid = super
+ break sid unless @pool.get(sid)
+ end
+ end
+
+ end
+
+ end
+
+end
View
@@ -12,21 +12,24 @@ def initialize(app, options = {})
# not sure if this is the best option, but otherwise it'll keep
# opening connections until tyrant freezes... =/
# tokyo_connect
+ # tokyo_connect
+ p @pool
+ @pool ||= Rufus::Tokyo::Tyrant.new(@host, @port.to_i)
end
private
- def tokyo_connect
- begin
- @pool = Rufus::Tokyo::Tyrant.new(@host, @port.to_i)
- rescue Rufus::Tokyo::TokyoError => e
- warn "Can't connect to Tyrant #{e}"
- end
- end
+ # def tokyo_connect
+ # begin
+
+ # rescue Rufus::Tokyo::TokyoError => e
+ # warn "Can't connect to Tyrant #{e}"
+ # end
+ # end
def get_session(env, sid)
- tokyo_connect
- @mutex.lock if env['rack.multithread']
+ # tokyo_connect
session = Marshal.load(@pool[sid]) rescue session if sid && session = @pool[sid]
+ @mutex.lock if env['rack.multithread']
unless sid && session
env['rack.errors'].puts("Session '#{sid.inspect}' not found, initializing...") if $VERBOSE and not sid.nil?
session = {}
@@ -37,14 +40,14 @@ def get_session(env, sid)
session.instance_variable_set('@old', {}.merge(session))
return [sid, session]
rescue Rufus::Tokyo::TokyoError => e
- session = {}
+ return [nil, {}]
ensure
@mutex.unlock if env['rack.multithread']
- @pool.close
+ # @pool.close
end
def set_session(env, sid, new_session, options)
- tokyo_connect
+ # tokyo_connect
@mutex.lock if env['rack.multithread']
session = Marshal.load(session) rescue session if session = @pool[sid]
if options[:renew] || options[:drop]
@@ -62,7 +65,7 @@ def set_session(env, sid, new_session, options)
warn $!.inspect
ensure
@mutex.unlock if env['rack.multithread']
- @pool.close
+ # @pool.close
end
def generate_sid
View
@@ -0,0 +1,77 @@
+require 'tokyotyrant'
+module Rack
+ module Session
+ class Tyrant < Abstract::ID
+ include TokyoTyrant
+ attr_reader :mutex, :pool
+ DEFAULT_OPTIONS = Abstract::ID::DEFAULT_OPTIONS.merge :tyrant_server => "localhost:1978"
+
+ def initialize(app, options = {})
+ super
+ @mutex = Mutex.new
+ @host, @port = *(options[:tyrant_server] || @default_options[:tyrant_server]).split(":") # @default_options) #options[:cache] ||
+ tokyo_connect
+ end
+
+ private
+ def tokyo_connect
+ @pool = RDB.new
+ unless @pool.open(@host, @port.to_i)
+ warn "Can't connect to Tyrant #{e}"
+ end
+ end
+
+ def get_session(env, sid)
+ # tokyo_connect
+ session = Marshal.load(@pool.get(sid)) rescue session if sid && session = @pool.get(sid)
+ @mutex.lock if env['rack.multithread']
+ unless sid && session
+ env['rack.errors'].puts("Session '#{sid.inspect}' not found, initializing...") if $VERBOSE and not sid.nil?
+ session = {}
+ sid = generate_sid
+ ret = @pool.put(sid, Marshal.dump(session))
+ raise "Session collision on '#{sid.inspect}'" unless ret
+ end
+ #session.instance_variable_put('@old', {}.merge(session))
+ return [sid, session]
+ rescue Rufus::Tokyo::TokyoError => e
+ return [nil, {}]
+ ensure
+ @mutex.unlock if env['rack.multithread']
+ # @pool.close
+ end
+
+ def set_session(env, sid, new_session, options)
+ # tokyo_connect
+ @mutex.lock if env['rack.multithread']
+ session = Marshal.load(session) rescue session if session = @pool.get(sid)
+ if options[:renew] || options[:drop]
+ @pool.delete sid
+ return false if options[:drop]
+ sid = generate_sid
+ @pool.put(sid, "")
+ end
+ #old_session = new_session.instance_variable_get('@old') || {}
+ session = new_session
+ @pool.put(sid, options && options[:raw] ? session : Marshal.dump(session))
+ return sid
+ rescue Rufus::Tokyo::TokyoError => e
+ warn "#{self} is unable to find server, error: #{e}"
+ warn $!.inspect
+ ensure
+ @mutex.unlock if env['rack.multithread']
+ # @pool.close
+ end
+
+ def generate_sid
+ loop do
+ sid = super
+ break sid unless @pool.get(sid)
+ end
+ end
+
+ end
+
+ end
+
+end
Oops, something went wrong.

0 comments on commit 42077ef

Please sign in to comment.