Class: ActiveCypher::Bolt::Driver

Inherits:
Object
  • Object
show all
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

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”.

Parameters:

  • uri (String)

    The Bolt URI

  • adapter (Object)

    The adapter instance

  • auth_token (Hash)

    Authentication token

  • pool_size (Integer) (defaults to: DEFAULT_POOL_SIZE)

    Connection pool size

  • secure (Boolean) (defaults to: false)

    Use SSL (default: false)

  • verify_cert (Boolean) (defaults to: true)

    Verify SSL certificate (default: true)



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.

Yield Parameters:

Returns:

  • (Async::Task)

    A task that resolves to the block’s result



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

#closeObject

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.message}" if ENV['DEBUG']
end

#verify_connectivityBoolean

Checks if the database is alive, or just faking it for your benefit.

Returns:

  • (Boolean)


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.

Yield Parameters:

Returns:

  • (Object)

    The result of the block



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.message}"
rescue StandardError => e
  raise ActiveCypher::ConnectionError, "Connection error: #{e.message}"
end