… | |
… | |
34 | |
34 | |
35 | If you make sure that you never run two or more requests in parallel, you |
35 | If you make sure that you never run two or more requests in parallel, you |
36 | can freely share the database handles between threads, of course. |
36 | can freely share the database handles between threads, of course. |
37 | |
37 | |
38 | Also, this module uses a number of "unclean" techniques (patching an |
38 | Also, this module uses a number of "unclean" techniques (patching an |
39 | internal libmysql structure for one thing) and was hacked within a few |
39 | internal libmysql structure for one thing) and was initially hacked within |
40 | hours on a long flight to Malaysia. |
40 | a few hours on a long flight to Malaysia. |
41 | |
41 | |
42 | It does, however, check whether it indeed got the structure layout |
42 | It does, however, check whether it indeed got the structure layout |
43 | correct, so you should expect perl exceptions or early crashes as opposed |
43 | correct, so you should expect perl exceptions or early crashes as opposed |
44 | to data corruption when something goes wrong during patching. |
44 | to data corruption when something goes wrong during patching. |
45 | |
45 | |
46 | =head2 SPEED |
46 | =head2 SPEED |
47 | |
47 | |
48 | This module is implemented in XS, and as long as mysqld replies quickly |
48 | This module is implemented in XS, and as long as mysqld replies quickly |
49 | enough, it adds no overhead to the standard libmysql communication |
49 | enough, it adds no overhead to the standard libmysql communication |
50 | routines (which are very badly written, btw.). |
50 | routines (which are very badly written, btw.). In fact, since it has a |
|
|
51 | more efficient buffering and allows requests to run in parallel, it often |
|
|
52 | decreases the actual time to run many queries considerably. |
51 | |
53 | |
52 | For very fast queries ("select 0"), this module can add noticable overhead |
54 | For very fast queries ("select 0"), this module can add noticable overhead |
53 | (around 15%) as it tries to switch to other coroutines when mysqld doesn't |
55 | (around 15%, 7% when EV can be used) as it tries to switch to other |
54 | deliver the data instantly. |
56 | coroutines when mysqld doesn't deliver the data immediately, although, |
|
|
57 | again, when running queries in parallel, they will usually execute faster. |
55 | |
58 | |
56 | For most types of queries, there will be no overhead, especially on |
59 | For most types of queries, there will be no extra latency, especially on |
57 | multicore systems where your perl process can do other things while mysqld |
60 | multicore systems where your perl process can do other things while mysqld |
58 | does its stuff. |
61 | does its stuff. |
59 | |
62 | |
60 | =head2 LIMITATIONS |
63 | =head2 LIMITATIONS |
61 | |
64 | |
62 | This module only supports "standard" mysql connection handles - this |
65 | This module only supports "standard" mysql connection handles - this |
63 | means unix domain or TCP sockets, and excludes SSL/TLS connections, named |
66 | means unix domain or TCP sockets, and excludes SSL/TLS connections, named |
64 | pipes (windows) and shared memory (also windows). No support for these |
67 | pipes (windows) and shared memory (also windows). No support for these |
65 | connection types is planned, either. |
68 | connection types is planned, either. |
66 | |
69 | |
|
|
70 | =head1 CANCELLATION |
|
|
71 | |
|
|
72 | Cancelling a thread that is within a mysql query will likely make the |
|
|
73 | handle unusable. As far as Coro::Mysql is concerned, the handle can be |
|
|
74 | safely destroyed, but it's not clear how mysql itself will react to a |
|
|
75 | cancellation. |
|
|
76 | |
67 | =head1 FUNCTIONS |
77 | =head1 FUNCTIONS |
68 | |
78 | |
69 | Coro::Mysql offers a single user-accessible function: |
79 | Coro::Mysql offers a single user-accessible function: |
70 | |
80 | |
71 | =over 4 |
81 | =over 4 |
… | |
… | |
79 | |
89 | |
80 | use Scalar::Util (); |
90 | use Scalar::Util (); |
81 | use Carp qw(croak); |
91 | use Carp qw(croak); |
82 | |
92 | |
83 | use Guard; |
93 | use Guard; |
|
|
94 | use AnyEvent (); |
84 | use Coro::Handle (); |
95 | use Coro (); |
|
|
96 | use Coro::AnyEvent (); # not necessary with newer Coro versions |
85 | |
97 | |
86 | # we need this extra indirection, as Coro doesn't support |
98 | # we need this extra indirection, as Coro doesn't support |
87 | # calling SLF-like functions via call_sv. |
99 | # calling SLF-like functions via call_sv. |
88 | |
100 | |
89 | sub readable { &Coro::Handle::FH::readable } |
101 | sub readable { &Coro::Handle::FH::readable } |
90 | sub writable { &Coro::Handle::FH::writable } |
102 | sub writable { &Coro::Handle::FH::writable } |
91 | |
103 | |
92 | BEGIN { |
104 | BEGIN { |
93 | our $VERSION = '1.02'; |
105 | our $VERSION = '1.2'; |
94 | |
106 | |
95 | require XSLoader; |
107 | require XSLoader; |
96 | XSLoader::load Coro::Mysql::, $VERSION; |
108 | XSLoader::load Coro::Mysql::, $VERSION; |
97 | } |
109 | } |
98 | |
110 | |
… | |
… | |
107 | It is safe to call this function on any database handle (or just about any |
119 | It is safe to call this function on any database handle (or just about any |
108 | value), but it will only do anything to L<DBD::mysql> handles, others are |
120 | value), but it will only do anything to L<DBD::mysql> handles, others are |
109 | returned unchanged. That means it is harmless when applied to database |
121 | returned unchanged. That means it is harmless when applied to database |
110 | handles of other databases. |
122 | handles of other databases. |
111 | |
123 | |
|
|
124 | It is also safe to pass C<undef>, so code like this is works as expected: |
|
|
125 | |
|
|
126 | my $dbh = DBI->connect ($database, $user, $pass)->Coro::Mysql::unblock |
|
|
127 | or die $DBI::errstr; |
|
|
128 | |
112 | =cut |
129 | =cut |
113 | |
130 | |
114 | sub unblock { |
131 | sub unblock { |
115 | my ($DBH) = @_; |
132 | my ($DBH) = @_; |
116 | |
133 | |
117 | if ($DBH->{Driver}{Name} eq "mysql") { |
134 | if ($DBH && $DBH->{Driver}{Name} eq "mysql") { |
118 | my $sock = $DBH->{sock}; |
135 | my $sock = $DBH->{sock}; |
119 | |
136 | |
120 | open my $fh, "+>&" . $DBH->{sockfd} |
137 | open my $fh, "+>&" . $DBH->{sockfd} |
121 | or croak "Coro::Mysql unable to clone mysql fd"; |
138 | or croak "Coro::Mysql unable to clone mysql fd"; |
122 | |
139 | |
|
|
140 | if (AnyEvent::detect ne "AnyEvent::Impl::EV" || !_use_ev) { |
|
|
141 | require Coro::Handle; |
123 | $fh = Coro::Handle::unblock $fh; |
142 | $fh = Coro::Handle::unblock ($fh); |
|
|
143 | } |
124 | |
144 | |
125 | _patch $sock, $DBH->{sockfd}, $fh, tied ${$fh}; |
145 | _patch $sock, $DBH->{sockfd}, $DBH->{mysql_clientversion}, $fh, tied ${$fh}; |
126 | } |
146 | } |
127 | |
147 | |
128 | $DBH |
148 | $DBH |
129 | } |
149 | } |
130 | |
150 | |
… | |
… | |
145 | use PApp::SQL; |
165 | use PApp::SQL; |
146 | |
166 | |
147 | sub with_db($$$&) { |
167 | sub with_db($$$&) { |
148 | my ($database, $user, $pass, $cb) = @_; |
168 | my ($database, $user, $pass, $cb) = @_; |
149 | |
169 | |
150 | my $dbh = Coro::Mysql::unblock DBI->connect ($database, $user, $pass) |
170 | my $dbh = DBI->connect ($database, $user, $pass)->Coro::Mysql::unblock |
151 | or die $DBI::errstr; |
171 | or die $DBI::errstr; |
152 | |
172 | |
153 | Coro::on_enter { $PApp::SQL::DBH = $dbh }; |
173 | Coro::on_enter { $PApp::SQL::DBH = $dbh }; |
154 | |
174 | |
155 | $cb->(); |
175 | $cb->(); |