Class: SingleThreadedPool

Inherits:
Object show all
Defined in:
lib/assistance/connection_pool.rb

Overview

A SingleThreadedPool acts as a replacement for a ConnectionPool for use in single-threaded applications. ConnectionPool imposes a substantial performance penalty, so SingleThreadedPool is used to gain some speed.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ SingleThreadedPool

Initializes the instance with the supplied block as the connection_proc.



130
131
132
# File 'lib/assistance/connection_pool.rb', line 130

def initialize(&block)
  @connection_proc = block
end

Instance Attribute Details

#connObject (readonly)

Returns the value of attribute conn.



126
127
128
# File 'lib/assistance/connection_pool.rb', line 126

def conn
  @conn
end

#connection_proc=(value) ⇒ Object (writeonly)

Sets the attribute connection_proc

Parameters:

  • value

    the value to set the attribute connection_proc to.



127
128
129
# File 'lib/assistance/connection_pool.rb', line 127

def connection_proc=(value)
  @connection_proc = value
end

Instance Method Details

#disconnect(&block) ⇒ Object

Disconnects from the database. Once a connection is requested using #hold, the connection is reestablished.



146
147
148
149
# File 'lib/assistance/connection_pool.rb', line 146

def disconnect(&block)
  block[@conn] if block && @conn
  @conn = nil
end

#holdObject

Yields the connection to the supplied block. This method simulates the ConnectionPool#hold API.



136
137
138
139
140
141
142
# File 'lib/assistance/connection_pool.rb', line 136

def hold
  @conn ||= @connection_proc.call
  yield @conn
rescue Exception => e
  # if the error is not a StandardError it is converted into RuntimeError.
  raise e.is_a?(StandardError) ? e : e.message
end