ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro-Mysql/Mysql.pm
(Generate patch)

Comparing Coro-Mysql/Mysql.pm (file contents):
Revision 1.13 by root, Thu Oct 11 03:18:31 2012 UTC vs.
Revision 1.25 by root, Mon Mar 4 11:40:52 2019 UTC

1=head1 NAME 1=head1 NAME
2 2
3Coro::Mysql - let other threads run while doing mysql requests 3Coro::Mysql - let other threads run while doing mysql/mariadb requests
4 4
5=head1 SYNOPSIS 5=head1 SYNOPSIS
6 6
7 use Coro::Mysql; 7 use Coro::Mysql;
8 8
9 my $DBH = Coro::Mysql::unblock DBI->connect (...); 9 my $DBH = Coro::Mysql::unblock DBI->connect (...);
10 10
11=head1 DESCRIPTION 11=head1 DESCRIPTION
12 12
13(Note that in this manual, "thread" refers to real threads as implemented 13(Note that in this manual, "thread" refers to real threads as implemented
14by the Coro module, not to the built-in windows process emulation which 14by the L<Coro> module, not to the built-in windows process emulation which
15unfortunately is also called "threads") 15unfortunately is also called "threads").
16 16
17This module replaces the I/O handlers for a database connection, with the 17This module replaces the I/O handlers for a database connection, with the
18effect that "patched" database handles no longer block the all threads of 18effect that "patched" database handles no longer block all threads of a
19a process, but only the thread that does the request. 19process, but only the thread that does the request. It should work for
20both L<DBD::mysql> and L<DBD::MariaDB> connections and a wide range of
21mariadb/mysql client libraries.
20 22
21This can be used to make parallel sql requests using Coro, or to do other 23This can be used to make parallel sql requests using Coro, or to do other
22stuff while mysql is rumbling in the background. 24stuff while mariadb is rumbling in the background.
23 25
24=head2 CAVEAT 26=head2 CAVEAT
25 27
26Note that this module must be linked against exactly the same (shared, 28Note that this module must be linked against exactly the same (shared,
27possibly not working with all OSes) F<libmysqlclient> library as 29possibly not working with all OSes) F<libmariadb>/F<libmysqlclient>
28DBD::mysql, otherwise it will not work. 30library as L<DBD::MariaDB>/L<DBD::mysql>, otherwise it will not work.
29
30Also, this module requires a header file that apparently isn't installed
31everywhere (F<violite.h>), and therefore comes with it's own copy, which
32might or might not be compatible to the F<violite.h> of your library -
33when in doubt, make sure all the libmysqlclient header files are installed
34and delete the F<violite.h> header that comes with this module.
35
36On the good side, this module does a multitude of checks to ensure that
37the libray versions match on the binary level, so on incompatibilities you
38should expect an exception when trying to unblock a handle, rather than
39data corruption.
40 31
41Also, while this module makes database handles non-blocking, you still 32Also, while this module makes database handles non-blocking, you still
42cannot run multiple requests in parallel on the same database handle. If 33cannot run multiple requests in parallel on the same database handle. If
43you want to run multiple queries in parallel, you have to create multiple 34you want to run multiple queries in parallel, you have to create multiple
44database connections, one for each thread that runs queries. Not doing 35database connections, one for each thread that runs queries. Not doing
79safely destroyed, but it's not clear how mysql itself will react to a 70safely destroyed, but it's not clear how mysql itself will react to a
80cancellation. 71cancellation.
81 72
82=head1 FUNCTIONS 73=head1 FUNCTIONS
83 74
84Coro::Mysql offers a single user-accessible function: 75Coro::Mysql offers these functions, the only one that oyu usually need is C<unblock>:
85 76
86=over 4 77=over 4
87 78
88=cut 79=cut
89 80
105 96
106sub readable { &Coro::Handle::FH::readable } 97sub readable { &Coro::Handle::FH::readable }
107sub writable { &Coro::Handle::FH::writable } 98sub writable { &Coro::Handle::FH::writable }
108 99
109BEGIN { 100BEGIN {
110 our $VERSION = '1.2'; 101 our $VERSION = '2.1';
111 102
112 require XSLoader; 103 require XSLoader;
113 XSLoader::load Coro::Mysql::, $VERSION; 104 XSLoader::load Coro::Mysql::, $VERSION;
114} 105}
115 106
134=cut 125=cut
135 126
136sub unblock { 127sub unblock {
137 my ($DBH) = @_; 128 my ($DBH) = @_;
138 129
130 if ($DBH) {
131 my $mariadb = $DBH->{Driver}{Name} eq "MariaDB";
139 if ($DBH && $DBH->{Driver}{Name} eq "mysql") { 132 if ($mariadb or $DBH->{Driver}{Name} eq "mysql") {
140 my $sock = $DBH->{sock}; 133 my $sock = $mariadb ? $DBH->{mariadb_sock} : $DBH->{sock};
134 my $sockfd = $mariadb ? $DBH->mariadb_sockfd : $DBH->{sockfd};
135 my $cvers = $mariadb ? $DBH->{mariadb_clientversion} : $DBH->{mysql_clientversion};
141 136
142 open my $fh, "+>&" . $DBH->{sockfd} 137 open my $fh, "+>&$sockfd"
143 or croak "Coro::Mysql unable to clone mysql fd"; 138 or croak "Coro::Mysql unable to dup mariadb/mysql fd";
144 139
145 if (AnyEvent::detect ne "AnyEvent::Impl::EV" || !_use_ev) { 140 if (AnyEvent::detect ne "AnyEvent::Impl::EV" || !_use_ev) {
146 require Coro::Handle; 141 require Coro::Handle;
147 $fh = Coro::Handle::unblock ($fh); 142 $fh = Coro::Handle::unblock ($fh);
143 }
144
145 _patch $sock, $sockfd, $cvers, $fh, tied *$$fh;
148 } 146 }
149
150 _patch $sock, $DBH->{sockfd}, $DBH->{mysql_clientversion}, $fh, tied ${$fh};
151 } 147 }
152 148
153 $DBH 149 $DBH
154} 150}
151
152=item $bool = Coro::Mysql::is_unblocked $DBH
153
154Returns true iff the database handle was successfully patched for
155non-blocking operations.
156
157=cut
158
159sub is_unblocked {
160 my ($DBH) = @_;
161
162 if ($DBH) {
163 my $mariadb = $DBH->{Driver}{Name} eq "MariaDB";
164 if ($mariadb or $DBH->{Driver}{Name} eq "mysql") {
165 my $sock = $mariadb ? $DBH->{mariadb_sock} : $DBH->{sock};
166 return _is_patched $sock
167 }
168 }
169
170 0
171}
172
173=item $bool = Coro::Mysql::have_ev
174
175Returns true if this Coro::Mysql installation is compiled with special
176support for L<EV> or not.
177
178Even if compiled in, it will only be used if L<EV> is actually the
179AnyEvent event backend.
180
181=cut
155 182
1561; 1831;
157 184
158=back 185=back
159 186
176 or die $DBI::errstr; 203 or die $DBI::errstr;
177 204
178 Coro::on_enter { $PApp::SQL::DBH = $dbh }; 205 Coro::on_enter { $PApp::SQL::DBH = $dbh };
179 206
180 $cb->(); 207 $cb->();
181 } 208 }
182 209
183This function makes it possible to easily use L<PApp::SQL> with 210This function makes it possible to easily use L<PApp::SQL> with
184L<Coro::Mysql>, without worrying about database handles. 211L<Coro::Mysql>, without worrying about database handles.
185 212
186 # now start 10 threads doing stuff 213 # now start 10 threads doing stuff
211 238
212This module was initially hacked together within a few hours on a long 239This module was initially hacked together within a few hours on a long
213flight to Malaysia, and seems to have worked ever since, with minor 240flight to Malaysia, and seems to have worked ever since, with minor
214adjustments for newer libmysqlclient libraries. 241adjustments for newer libmysqlclient libraries.
215 242
243Well, at least until mariadb introduced the new Pluggable Virtual IO API
244in mariadb 10.3, which changed and broke everything. On the positive
245side, the old system was horrible to use, as many GNU/Linux distributions
246forgot to include the required heaqder files and there were frequent small
247changes, while the new PVIO system seems to be "official" and hopefully
248better supported.
249
216=head1 AUTHOR 250=head1 AUTHOR
217 251
218 Marc Lehmann <schmorp@schmorp.de> 252 Marc Lehmann <schmorp@schmorp.de>
219 http://home.schmorp.de/ 253 http://home.schmorp.de/
220 254

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines