Class: EventMachine::MySQLConnection

Inherits:
Connection
  • Object
show all
Defined in:
lib/em-mysqlplus/connection.rb

Constant Summary collapse

MAX_RETRIES_ON_DEADLOCKS =
10
DisconnectErrors =
[
  'query: not connected',
  'MySQL server has gone away',
  'Lost connection to MySQL server during query'
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mysql, opts, conn) ⇒ MySQLConnection

Returns a new instance of MySQLConnection.



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/em-mysqlplus/connection.rb', line 22

def initialize(mysql, opts, conn)
  @conn = conn
  @mysql = mysql
  @fd = mysql.socket
  @opts = opts
  @current = nil
  @queue = []
  @processing = false
  @connected = true

  self.notify_readable = true
  EM.add_timer(0){ next_query }
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &blk) ⇒ Object

mysql gem has syncronous methods such as list_dbs and others which require that we execute without callbacks



126
127
128
# File 'lib/em-mysqlplus/connection.rb', line 126

def method_missing(method, *args, &blk)
  @mysql.send(method, *args, &blk) if @mysql.respond_to? method
end

Instance Attribute Details

#connectedObject (readonly)

Returns the value of attribute connected.



11
12
13
# File 'lib/em-mysqlplus/connection.rb', line 11

def connected
  @connected
end

#optsObject (readonly) Also known as: settings

Returns the value of attribute opts.



11
12
13
# File 'lib/em-mysqlplus/connection.rb', line 11

def opts
  @opts
end

#processingObject (readonly)

Returns the value of attribute processing.



11
12
13
# File 'lib/em-mysqlplus/connection.rb', line 11

def processing
  @processing
end

Instance Method Details

#closeObject



130
131
132
133
134
135
136
# File 'lib/em-mysqlplus/connection.rb', line 130

def close
  return unless @connected

  detach
  @mysql.close
  @connected = false
end

#execute(sql, cblk = nil, eblk = nil, retries = 0) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/em-mysqlplus/connection.rb', line 102

def execute(sql, cblk = nil, eblk = nil, retries = 0)
  begin
    if not @processing or not @connected
      @processing = true
      @mysql.send_query(sql)
    else
      @queue << [sql, cblk, eblk, retries]
      return
    end

  rescue Mysql::Error => e
    if DisconnectErrors.include? e.message
      @queue << [sql, cblk, eblk, retries]
      return close
    else
      raise e
    end
  end

  @current = [sql, cblk, eblk, retries]
end

#notify_readableObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/em-mysqlplus/connection.rb', line 36

def notify_readable
  if item = @current
    sql, cblk, eblk, retries = item
    result = @mysql.get_result
    result = @mysql.affected_rows if result.nil?

    # kick off next query in the background
    # as we process the current results
    @current = nil
    @processing = false
    next_query

    cblk.call(result)
  else
    return close
  end

rescue Mysql::Error => e

  if e.message =~ /Deadlock/ and retries < MAX_RETRIES_ON_DEADLOCKS
    @queue << [sql, cblk, eblk, retries + 1]
    @processing = false
    next_query

  elsif DisconnectErrors.include? e.message
    @queue << [sql, cblk, eblk, retries + 1]
    return close

  elsif cb = (eblk || @opts[:on_error])
    cb.call(e)
    @processing = false
    next_query

  else
    raise e
  end
end

#reconnectObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/em-mysqlplus/connection.rb', line 87

def reconnect
  @processing = false
  @mysql = @conn.connect_socket(@opts)
  @fd = @mysql.socket

  @signature = EM.attach_fd(@mysql.socket, true)
  EM.set_notify_readable(@signature, true)
  EM.instance_variable_get('@conns')[@signature] = self
  @connected = true
  next_query

rescue Mysql::Error => e
  EM.add_timer(1) { reconnect }
end

#unbindObject



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/em-mysqlplus/connection.rb', line 74

def unbind
  # wait for the next tick until the current fd is removed completely from the reactor
  #
  # in certain cases the new FD# (@mysql.socket) is the same as the old, since FDs are re-used
  # without next_tick in these cases, unbind will get fired on the newly attached signature as well
  #
  # do _NOT_ use EM.next_tick here. if a bunch of sockets disconnect at the same time, we want
  # reconnects to happen after all the unbinds have been processed

  @connected = false
  EM.next_tick { reconnect }
end