Permalink
Browse files

Added support for mongo db replication set connection

  • Loading branch information...
Petteri Salmi authored and jammi committed Dec 14, 2014
1 parent a529549 commit 7f0bdd23508c4fd905544a742345ffd181a610a6
Showing with 39 additions and 42 deletions.
  1. +16 −10 conf/default_conf.yaml
  2. +5 −3 lib/rsence/default_config.rb
  3. +15 −26 lib/rsence/session/mongo_sessionstorage.rb
  4. +3 −3 lib/rsence/sessionstorage.rb
View
@@ -248,20 +248,26 @@
:title: 'Invalid Session'
:descr: 'Your session is invalid. Please reload the page to continue.'
:uri: '/'
#
# MongoDB-specific settings
:mongo:
#
# MongoDB Connection Pool Timeout:
:pool_timeout: 10 # seconds
#
# MongoDB Connection Pool Size:
:pool_size: 500 # amount of connections
#
# Database configuration
:database:
# Session database storage string. Leave empty here to enable the wizard for storage.
# SQLite
:ses_db: 'sqlite://db/ses.db'
# MongoDB
# :mongo:
# :host: localhost
# :port: 27017
# :db_name: rsence_ses
# :username: rsence
# :password: 283N1ORI758dykD
# :pool_timeout: 10 # seconds
# :pool_size: 500 # amount of connections
# :repl_enabled: false
# :repl_name: rs1
# :repl_members:
# - 'mongodb1:27017'
# - 'mongodb2:27017'
# - 'mongodb3:27017'
#
# ValueManager settings
@@ -156,9 +156,11 @@ def initialize( args, dont_expand_path=false )
end
# Check database path for sqlite databases.
if config[:database][:ses_db].start_with?('sqlite://') and not dont_expand_path
db_url = File.expand_path( config[:database][:ses_db].split('sqlite://')[1], args[:env_path] )
config[:database][:ses_db] = "sqlite://#{db_url}"
if config[:database].key?( :ses_db ) and not dont_expand_path
if config[:database][:ses_db].start_with?('sqlite://')
db_url = File.expand_path( config[:database][:ses_db].split('sqlite://')[1], args[:env_path] )
config[:database][:ses_db] = "sqlite://#{db_url}"
end
end
# Sets the various standard url prefixes:
@@ -2,36 +2,25 @@
require 'mongo'
# MongoSessionStorage is the SessionStorage backend for MongoDB
# Authentication string format is strictly like this for now:
# mongodb://username:password@host:port/database
class MongoSessionStorage
# Poor-man's connection string parser:
def parse_db_uri
db_str = @db_uri.split('mongodb://')[1]
( db_auth_str, db_conn_str ) = db_str.split('@')
( db_username, db_password ) = db_auth_str.split(':')
( db_host, db_port_name_str ) = db_conn_str.split(':')
( db_port, db_name ) = db_port_name_str.split('/')
return {
:host => db_host,
:port => db_port,
:username => db_username,
:password => db_password,
:db => db_name
}
end
# Opens database connection
def db_open
# mongodb://rsence:2N74krTMURIpSr6Y91Hy@localhost:37035/rsence_sessions
conn = parse_db_uri
@conn = Mongo::Connection.new( conn[:host], conn[:port], {
:pool_size => @config[:mongo][:pool_size],
:pool_timeout => @config[:mongo][:pool_timeout],
} )
@db = @conn.db( conn[:db] )
@db_auth = @db.authenticate( conn[:username], conn[:password] )
conf = @db_params[:mongo]
if conf[:repl_enabled]
@conn = Mongo::MongoReplicaSetClient.new( conf[:repl_members], {
:name => conf[:repl_name],
:pool_size => conf[:pool_size],
:pool_timeout => conf[:pool_timeout]
} )
else
@conn = Mongo::Connection.new( conf[:host], conf[:port], {
:pool_size => conf[:pool_size],
:pool_timeout => conf[:pool_timeout],
} )
end
@db = @conn.db( conf[:db_name] )
@db_auth = @db.authenticate( conf[:username], conf[:password] )
@db_auth = true
end
@@ -4,7 +4,7 @@ module RSence
## HValue class for session restoration
require 'rsence/value'
if RSence.config[:database][:ses_db].start_with? 'mongodb://'
if RSence.config[:database].key?( :mongo )
require 'rsence/session/mongo_sessionstorage'
SessionBackend = MongoSessionStorage
else
@@ -39,8 +39,8 @@ def initialize
## Disposable keys (new ses_key each request)
@config = RSence.config[:session_conf]
@db_uri = RSence.config[:database][:ses_db]
@db_params = RSence.config[:database]
@db_uri = @db_params[:ses_db]
if db_test
@db_avail = true

0 comments on commit 7f0bdd2

Please sign in to comment.