Skip to content

Make socket classes configurable #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ MySQL connector for Ruby.
stmt = my.prepare('insert into tblname (col1,col2) values (?,?)')
stmt.execute 123, 'abc'

== Use with {Celluloid::IO}[https://github.com/celluloid/celluloid-io]

You just need to override Mysql default socket classes with
Celluloid's ones. Then use it as you normally do.

Mysql.unixsocket_class = Celluloid::IO::UNIXSocket
Mysql.tcpsocket_class = Celluloid::IO::TCPSocket

== Incompatible with MySQL/Ruby 2.8.x

* Ruby 1.8.x ではシフトJISのような安全でないマルチバイト文字セットに対して Mysql#escape_string を使用すると例外が発生します。
Expand Down
14 changes: 14 additions & 0 deletions lib/mysql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,20 @@ class Mysql
rescue LoadError
end

##
# Make socket classes configurable, so we can be compatible
# with amazing Celluloid::IO (https://github.com/celluloid/celluloid-io)
#
@@tcpsocket_class = TCPSocket
def self.tcpsocket_class; @@tcpsocket_class end
def self.tcpsocket_class=(value); @@tcpsocket_class = value end

@@unixsocket_class = UNIXSocket
def self.unixsocket_class; @@unixsocket_class end
def self.unixsocket_class=(value); @@unixsocket_class = value end
#
##

VERSION = 20911 # Version number of this library
MYSQL_UNIX_PORT = "/tmp/mysql.sock" # UNIX domain socket filename
MYSQL_TCP_PORT = 3306 # TCP socket port number
Expand Down
4 changes: 2 additions & 2 deletions lib/mysql/protocol.rb
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,10 @@ def initialize(host, port, socket, conn_timeout, read_timeout, write_timeout)
Timeout.timeout conn_timeout do
if host.nil? or host.empty? or host == "localhost"
socket ||= ENV["MYSQL_UNIX_PORT"] || MYSQL_UNIX_PORT
@sock = UNIXSocket.new socket
@sock = Mysql.unixsocket_class.new socket
else
port ||= ENV["MYSQL_TCP_PORT"] || (Socket.getservbyname("mysql","tcp") rescue MYSQL_TCP_PORT)
@sock = TCPSocket.new host, port
@sock = Mysql.tcpsocket_class.new host, port
end
end
rescue Timeout::Error
Expand Down