Class: Bones::RPC::Session
- Inherits:
-
Object
- Object
- Bones::RPC::Session
- Includes:
- Optionable
- Defined in:
- lib/bones/rpc/session.rb
Overview
A session in bones_rpc is root for all interactions with a Bones::RPC server or replica set.
It can talk to a single default database, or dynamically speak to multiple databases.
Instance Attribute Summary collapse
-
#cluster ⇒ Cluster
The cluster of nodes.
- #options ⇒ Object
Class Method Summary collapse
-
.connect(uri, &block) ⇒ Session
Create a new session from a URI.
Instance Method Summary collapse
- #backend ⇒ Object
-
#command(op) ⇒ Object
Run
commandon the current database. - #context ⇒ Object
-
#disconnect ⇒ true
Disconnects all nodes in the session’s cluster.
- #handle_refresh(node) ⇒ Object
-
#initialize(seeds, options = {}, &callback) ⇒ Session
constructor
Initialize a new database session.
-
#inspect ⇒ String
Provide a string inspection for the session.
- #notify(method, *params) ⇒ Object
-
#read_preference ⇒ Object
Get the read preference for the session.
- #request(method, *params) ⇒ Object
- #synchronize ⇒ Object
Constructor Details
#initialize(seeds, options = {}, &callback) ⇒ Session
Initialize a new database session.
145 146 147 148 149 150 |
# File 'lib/bones/rpc/session.rb', line 145 def initialize(seeds, = {}, &callback) validate_strict() = @callback = callback @cluster = Cluster.new(self, seeds) end |
Instance Attribute Details
#cluster ⇒ Cluster
39 40 41 |
# File 'lib/bones/rpc/session.rb', line 39 def cluster @cluster end |
#options ⇒ Object
39 |
# File 'lib/bones/rpc/session.rb', line 39 attr_reader :cluster, :options |
Class Method Details
.connect(uri, &block) ⇒ Session
Create a new session from a URI.
189 190 191 192 193 |
# File 'lib/bones/rpc/session.rb', line 189 def connect(uri, &block) uri = Uri.new(uri) session = new(*uri.bones_rpc_arguments, &block) session end |
Instance Method Details
#backend ⇒ Object
41 42 43 |
# File 'lib/bones/rpc/session.rb', line 41 def backend @backend ||= Backend.get([:backend] || :synchronous).tap(&:setup) end |
#command(op) ⇒ Object
Run command on the current database.
52 53 54 |
# File 'lib/bones/rpc/session.rb', line 52 def command(op) current_database.command(op) end |
#context ⇒ Object
56 57 58 |
# File 'lib/bones/rpc/session.rb', line 56 def context @context ||= Context.new(self) end |
#disconnect ⇒ true
Disconnects all nodes in the session’s cluster. This should only be used in cases # where you know you’re not going to use the cluster on the thread anymore and need to force the connections to close.
67 68 69 |
# File 'lib/bones/rpc/session.rb', line 67 def disconnect cluster.disconnect end |
#handle_refresh(node) ⇒ Object
71 72 73 |
# File 'lib/bones/rpc/session.rb', line 71 def handle_refresh(node) @callback.call(node) if @callback end |
#inspect ⇒ String
Provide a string inspection for the session.
83 84 85 |
# File 'lib/bones/rpc/session.rb', line 83 def inspect "<#{self.class.name} seeds=#{cluster.seeds}>" end |
#notify(method, *params) ⇒ Object
152 153 154 |
# File 'lib/bones/rpc/session.rb', line 152 def notify(method, *params) context.notify(method, params) end |
#read_preference ⇒ Object
Get the read preference for the session. Will default to primary if none was provided.
165 166 167 |
# File 'lib/bones/rpc/session.rb', line 165 def read_preference @read_preference ||= ReadPreference.get([:read] || :nearest) end |
#request(method, *params) ⇒ Object
169 170 171 |
# File 'lib/bones/rpc/session.rb', line 169 def request(method, *params) context.request(method, params) end |
#synchronize ⇒ Object
173 174 175 |
# File 'lib/bones/rpc/session.rb', line 173 def synchronize context.synchronize end |