Class: ActiveCypher::Bolt::Driver
- Inherits:
-
Object
- Object
- ActiveCypher::Bolt::Driver
- Defined in:
- lib/active_cypher/bolt/driver.rb
Constant Summary collapse
- DEFAULT_POOL_SIZE =
ENV.fetch('BOLT_POOL_SIZE', 10).to_i
Instance Method Summary collapse
-
#async_with_session(**kw) {|session| ... } ⇒ Async::Task
Asynchronously yields a Session.
-
#close ⇒ Object
Closes the connection pool.
-
#initialize(uri:, adapter:, auth_token:, pool_size: DEFAULT_POOL_SIZE, secure: false, verify_cert: true) ⇒ Driver
constructor
Initializes the driver, because you can’t spell “abstraction” without “action”.
-
#verify_connectivity ⇒ Boolean
Checks if the database is alive, or just faking it for your benefit.
-
#with_session(**kw) {|session| ... } ⇒ Object
Yields a Session.
Constructor Details
#initialize(uri:, adapter:, auth_token:, pool_size: DEFAULT_POOL_SIZE, secure: false, verify_cert: true) ⇒ Driver
Initializes the driver, because you can’t spell “abstraction” without “action”.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/active_cypher/bolt/driver.rb', line 23 def initialize(uri:, adapter:, auth_token:, pool_size: DEFAULT_POOL_SIZE, secure: false, verify_cert: true) @uri = URI(uri) @adapter = adapter @auth = auth_token @secure = secure @verify_cert = verify_cert # Create a connection pool with the specified size # Because one connection is never enough for true disappointment. @pool = Async::Pool::Controller.wrap( limit: pool_size ) { build_connection } end |
Instance Method Details
#async_with_session(**kw) {|session| ... } ⇒ Async::Task
Asynchronously yields a Session. Each call acquires its own connection from the pool, making it safe for concurrent use across fibers.
58 59 60 61 62 63 64 |
# File 'lib/active_cypher/bolt/driver.rb', line 58 def async_with_session(**kw, &block) raise 'Cannot run async_with_session outside of an Async task' unless Async::Task.current? Async do _acquire_session(**kw, &block) end end |
#close ⇒ Object
Closes the connection pool. Because sometimes you just need to let go.
75 76 77 78 79 80 |
# File 'lib/active_cypher/bolt/driver.rb', line 75 def close @pool.close rescue StandardError => e # Log but don't raise to ensure we don't prevent cleanup puts "Warning: Error while closing connection pool: #{e.}" if ENV['DEBUG'] end |
#verify_connectivity ⇒ Boolean
Checks if the database is alive, or just faking it for your benefit.
69 70 71 72 |
# File 'lib/active_cypher/bolt/driver.rb', line 69 def verify_connectivity with_session { |s| s.run('RETURN 1') } true end |
#with_session(**kw) {|session| ... } ⇒ Object
Yields a Session. Works inside or outside an Async reactor. Because sometimes you want async, and sometimes you just want to feel something.
43 44 45 46 47 48 49 50 51 |
# File 'lib/active_cypher/bolt/driver.rb', line 43 def with_session(**kw, &block) Sync do _acquire_session(**kw, &block) end rescue Async::TimeoutError => e raise ActiveCypher::ConnectionError, "Connection pool timeout: #{e.}" rescue StandardError => e raise ActiveCypher::ConnectionError, "Connection error: #{e.}" end |