Class: Beachcomber::Session
- Inherits:
-
Object
- Object
- Beachcomber::Session
- Includes:
- ResponseParsing
- Defined in:
- lib/beachcomber/client.rb
Overview
Session holds a persistent connection to the daemon, held open by the shared library, for #get/#get_with_flags/#put/#set_context.
Obtain a Session via Client#session:
client.session do |s|
s.set_context('/repo')
r = s.get('git.branch')
end
#refresh, #status, #hello and #introspect are also available for API-compatibility with the pre-ABI client, but the C ABI provides no session-scoped equivalents for them (only get/put/set_context reuse the persistent connection — see Task 3.6 of the client-ABI plan); they route through the parent Client's connection instead, one-shot per call.
The underlying handle is guarded by the library's own mutex: a concurrent caller on the same session gets BusyError rather than blocking or interleaving requests.
Instance Method Summary collapse
-
#close ⇒ Object
Closes the underlying connection.
-
#get(key, path: nil) ⇒ Result
Reads a cached value.
-
#get_with_flags(key, path: nil, force: false, wait: false) ⇒ Result
Reads a cached value with protocol flags.
-
#hello ⇒ HelloInfo
Sends a hello handshake and returns server info.
-
#initialize(handle, client_handle) ⇒ Session
constructor
A new instance of Session.
-
#introspect(subject, duration_secs: nil) ⇒ IntrospectResponse
Introspects a daemon subsystem.
-
#put(key, data = nil, ttl: nil, path: nil) ⇒ nil
Writes a value into the daemon cache on this session's connection.
-
#refresh(key, path: nil) ⇒ Object
Forces the daemon to recompute a provider/key.
-
#set_context(path) ⇒ Object
Sets the default path for subsequent #get/#get_with_flags queries on this connection.
-
#status ⇒ Array<CacheRow>
Returns cache rows from the daemon.
Constructor Details
#initialize(handle, client_handle) ⇒ Session
Returns a new instance of Session.
119 120 121 122 123 |
# File 'lib/beachcomber/client.rb', line 119 def initialize(handle, client_handle) @handle = handle @client_handle = client_handle @closed = false end |
Instance Method Details
#close ⇒ Object
Closes the underlying connection. Idempotent.
203 204 205 206 207 208 |
# File 'lib/beachcomber/client.rb', line 203 def close return if @closed @closed = true Beachcomber::FFI.close_session(@handle) end |
#get(key, path: nil) ⇒ Result
Reads a cached value.
139 140 141 |
# File 'lib/beachcomber/client.rb', line 139 def get(key, path: nil) build_result(Beachcomber::FFI.call!(:bc_session_get, @handle, key, path, 0)) end |
#get_with_flags(key, path: nil, force: false, wait: false) ⇒ Result
Reads a cached value with protocol flags.
150 151 152 153 |
# File 'lib/beachcomber/client.rb', line 150 def get_with_flags(key, path: nil, force: false, wait: false) flags = (force ? Beachcomber::FFI::GET_FORCE : 0) | (wait ? Beachcomber::FFI::GET_WAIT : 0) build_result(Beachcomber::FFI.call!(:bc_session_get, @handle, key, path, flags)) end |
#hello ⇒ HelloInfo
Sends a hello handshake and returns server info.
174 175 176 |
# File 'lib/beachcomber/client.rb', line 174 def hello parse_hello(Beachcomber::FFI.call!(:bc_hello, @client_handle)) end |
#introspect(subject, duration_secs: nil) ⇒ IntrospectResponse
Introspects a daemon subsystem.
196 197 198 199 200 |
# File 'lib/beachcomber/client.rb', line 196 def introspect(subject, duration_secs: nil) = duration_secs ? JSON.generate(duration_secs: duration_secs) : nil data = Beachcomber::FFI.call!(:bc_introspect, @client_handle, subject.to_s, ) parse_introspect(subject.to_s, data) end |
#put(key, data = nil, ttl: nil, path: nil) ⇒ nil
Writes a value into the daemon cache on this session's connection.
data = nil clears the entry.
186 187 188 189 |
# File 'lib/beachcomber/client.rb', line 186 def put(key, data = nil, ttl: nil, path: nil) Beachcomber::FFI.call!(:bc_session_put, @handle, key, JSON.generate(data), ttl&.to_s, path) nil end |
#refresh(key, path: nil) ⇒ Object
Forces the daemon to recompute a provider/key.
159 160 161 162 |
# File 'lib/beachcomber/client.rb', line 159 def refresh(key, path: nil) Beachcomber::FFI.call!(:bc_refresh, @client_handle, key, path) nil end |
#set_context(path) ⇒ Object
Sets the default path for subsequent #get/#get_with_flags queries on this connection.
129 130 131 132 |
# File 'lib/beachcomber/client.rb', line 129 def set_context(path) Beachcomber::FFI.call!(:bc_session_set_context, @handle, path) nil end |
#status ⇒ Array<CacheRow>
Returns cache rows from the daemon.
167 168 169 |
# File 'lib/beachcomber/client.rb', line 167 def status parse_cache_rows(Beachcomber::FFI.call!(:bc_status, @client_handle)) end |