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.23 by root, Mon Mar 4 05:44:43 2019 UTC vs.
Revision 1.24 by root, Mon Mar 4 06:19:16 2019 UTC

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 mariadb 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<libmariadb>/F<libmysqlclient> 29possibly not working with all OSes) F<libmariadb>/F<libmysqlclient>
28library as L<DBD::mysql>, otherwise it will not work. 30library as L<DBD::MariaDB>/L<DBD::mysql>, otherwise it will not work.
29 31
30Also, while this module makes database handles non-blocking, you still 32Also, while this module makes database handles non-blocking, you still
31cannot run multiple requests in parallel on the same database handle. If 33cannot run multiple requests in parallel on the same database handle. If
32you 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
33database connections, one for each thread that runs queries. Not doing 35database connections, one for each thread that runs queries. Not doing
68safely 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
69cancellation. 71cancellation.
70 72
71=head1 FUNCTIONS 73=head1 FUNCTIONS
72 74
73Coro::Mysql offers a single user-accessible function: 75Coro::Mysql offers these functions, the only one that oyu usually need is C<unblock>:
74 76
75=over 4 77=over 4
76 78
77=cut 79=cut
78 80
123=cut 125=cut
124 126
125sub unblock { 127sub unblock {
126 my ($DBH) = @_; 128 my ($DBH) = @_;
127 129
130 if ($DBH) {
131 my $mariadb = $DBH->{Driver}{Name} eq "MariaDB";
128 if ($DBH && $DBH->{Driver}{Name} eq "mysql") { 132 if ($mariadb or $DBH->{Driver}{Name} eq "mysql") {
129 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};
130 136
131 open my $fh, "+>&" . $DBH->{sockfd} 137 open my $fh, "+>&$sockfd"
132 or croak "Coro::Mysql unable to clone mysql fd"; 138 or croak "Coro::Mysql unable to dup mariadb/mysql fd";
133 139
134 if (AnyEvent::detect ne "AnyEvent::Impl::EV" || !_use_ev) { 140 if (AnyEvent::detect ne "AnyEvent::Impl::EV" || !_use_ev) {
135 require Coro::Handle; 141 require Coro::Handle;
136 $fh = Coro::Handle::unblock ($fh); 142 $fh = Coro::Handle::unblock ($fh);
143 }
144
145 _patch $sock, $sockfd, $cvers, $fh, tied *$$fh;
137 } 146 }
138
139 _patch $sock, $DBH->{sockfd}, $DBH->{mysql_clientversion}, $fh, tied *$$fh;
140 } 147 }
141 148
142 $DBH 149 $DBH
143} 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
144 182
1451; 1831;
146 184
147=back 185=back
148 186

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines