Class: PgEventstore::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/pg_eventstore/connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri:, pool_size: 5, pool_timeout: 5) ⇒ Connection

Returns a new instance of Connection.

Parameters:

  • uri (String)

    PostgreSQL connection URI. Example: “postgresql://postgres:postgres@localhost:5432/eventstore”

  • pool_size (Integer) (defaults to: 5)

    Connection pool size

  • pool_timeout (Integer) (defaults to: 5)

    Connection pool timeout in seconds



26
27
28
29
30
31
# File 'lib/pg_eventstore/connection.rb', line 26

def initialize(uri:, pool_size: 5, pool_timeout: 5)
  @uri = uri
  @pool_size = pool_size
  @pool_timeout = pool_timeout
  init_pool
end

Instance Attribute Details

#pool_size=(value) ⇒ Integer

Returns:

  • (Integer)


16
17
18
# File 'lib/pg_eventstore/connection.rb', line 16

def pool_size
  @pool_size
end

#pool_timeout=(value) ⇒ Integer

Returns:

  • (Integer)


19
20
21
# File 'lib/pg_eventstore/connection.rb', line 19

def pool_timeout
  @pool_timeout
end

#uri=(value) ⇒ String

Returns:

  • (String)


13
14
15
# File 'lib/pg_eventstore/connection.rb', line 13

def uri
  @uri
end

Instance Method Details

#shutdownvoid

This method returns an undefined value.



52
53
54
# File 'lib/pg_eventstore/connection.rb', line 52

def shutdown
  @pool.shutdown(&:close)
end

#with {|connection| ... } ⇒ Object

A shorthand from ConnectionPool#with.

Yield Parameters:

  • connection (PG::Connection)

    PostgreSQL connection instance

Returns:

  • (Object)

    a value of a given block



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/pg_eventstore/connection.rb', line 36

def with(&)
  should_retry = true
  @pool.with do |conn|
    yield conn
  rescue PG::ConnectionBad, PG::UnableToSend
    # Recover a connection after fork or when we lost a connection to PostgreSQL. We retry only once and without any
    # delay.
    conn.sync_reset
    raise unless should_retry

    should_retry = false
    retry
  end
end