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



56
57
58
59
60
61
# File 'lib/pg_eventstore/connection.rb', line 56

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)


46
47
48
# File 'lib/pg_eventstore/connection.rb', line 46

def pool_size
  @pool_size
end

#pool_timeout=(value) ⇒ Integer

Returns:

  • (Integer)


49
50
51
# File 'lib/pg_eventstore/connection.rb', line 49

def pool_timeout
  @pool_timeout
end

#uri=(value) ⇒ String

Returns:

  • (String)


43
44
45
# File 'lib/pg_eventstore/connection.rb', line 43

def uri
  @uri
end

Instance Method Details

#shutdownvoid

This method returns an undefined value.



82
83
84
# File 'lib/pg_eventstore/connection.rb', line 82

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



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/pg_eventstore/connection.rb', line 66

def with(&_blk)
  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