Module: Semian::Mysql2

Includes:
Adapter
Defined in:
lib/semian/mysql2.rb

Constant Summary collapse

CONNECTION_ERROR =
Regexp.union(
  /Can't connect to (?:MySQL )?server on/i,
  /Lost connection to (?:MySQL )?server/i,
  /MySQL server has gone away/i,
  /Too many connections/i,
  /closed MySQL connection/i,
  /Timeout waiting for a response/i,
  /No matching servers with free connections/i,
  /Max connect timeout reached while reaching hostgroup/i,
)
ResourceBusyError =
::Mysql2::ResourceBusyError
CircuitOpenError =
::Mysql2::CircuitOpenError
PingFailure =
Class.new(::Mysql2::Error)
DEFAULT_HOST =
"localhost"
DEFAULT_PORT =
3306
QUERY_ALLOWLIST =
%r{\A(?:/\*.*?\*/)?\s*(ROLLBACK|COMMIT|RELEASE\s+SAVEPOINT)}i

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Adapter

#clear_semian_resource, #semian_resource

Class Method Details

.included(base) ⇒ Object

The naked methods are exposed as ‘raw_query` and `raw_connect` for instrumentation purpose



46
47
48
49
50
51
52
53
54
55
# File 'lib/semian/mysql2.rb', line 46

def included(base)
  base.send(:alias_method, :raw_query, :query)
  base.send(:remove_method, :query)

  base.send(:alias_method, :raw_connect, :connect)
  base.send(:remove_method, :connect)

  base.send(:alias_method, :raw_ping, :ping)
  base.send(:remove_method, :ping)
end

Instance Method Details

#pingObject



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/semian/mysql2.rb', line 70

def ping
  return false if closed?

  result = nil
  acquire_semian_resource(adapter: :mysql, scope: :ping) do
    result = raw_ping
    raise PingFailure, result.to_s unless result
  end
  result
rescue ResourceBusyError, CircuitOpenError, PingFailure
  false
end

#query(*args) ⇒ Object



83
84
85
86
87
88
89
# File 'lib/semian/mysql2.rb', line 83

def query(*args)
  if query_whitelisted?(*args)
    raw_query(*args)
  else
    acquire_semian_resource(adapter: :mysql, scope: :query) { raw_query(*args) }
  end
end

#semian_identifierObject



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/semian/mysql2.rb', line 58

def semian_identifier
  @semian_identifier ||= begin
    name = semian_options && semian_options[:name]
    unless name
      host = query_options[:host] || DEFAULT_HOST
      port = query_options[:port] || DEFAULT_PORT
      name = "#{host}:#{port}"
    end
    :"mysql_#{name}"
  end
end

#with_resource_timeout(temp_timeout) ⇒ Object

TODO: write_timeout and connect_timeout can’t be configured currently dynamically, await github.com/brianmario/mysql2/pull/955



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/semian/mysql2.rb', line 93

def with_resource_timeout(temp_timeout)
  prev_read_timeout = @read_timeout

  begin
    # C-ext reads this directly, writer method will configure
    # properly on the client but based on my read--this is good enough
    # until we get https://github.com/brianmario/mysql2/pull/955 in
    @read_timeout = temp_timeout
    yield
  ensure
    @read_timeout = prev_read_timeout
  end
end