Module: Atech::ObjectStore::Connection

Defined in:
lib/atech/object_store/connection.rb

Overview

A very simple connection pool. Allows as many connections as MySQL is happy to give us. Probably not compatible with a truly concurrent Ruby due to the use of Array#shift, but good enough for MRI

Class Method Summary collapse

Class Method Details

.client {|client| ... } ⇒ Object

Get an available connection (or create a new one) and yield it to the passed block. Places the client into

Yields:

  • (client)

    Block that wishes to access database

Yield Parameters:

  • client (Mysql2::Client)

    A mysql2 client just for this block

Returns:

  • (Object)

    Result of the block



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/atech/object_store/connection.rb', line 15

def self.client
  client = @free_clients.shift || new_client
  return_value = nil
  tries = 2
  begin
    return_value = yield client
  rescue Mysql2::Error => e
    if e.message =~ /(lost connection|gone away)/i && (tries -= 1) > 0
      retry
    else
      raise
    end
  ensure
    @free_clients << client
  end
  return_value
end