… | |
… | |
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 |
14 | by the Coro module, not to the built-in windows process emulation which |
14 | by the L<Coro> module, not to the built-in windows process emulation which |
15 | unfortunately is also called "threads") |
15 | unfortunately is also called "threads"). |
16 | |
16 | |
17 | This module replaces the I/O handlers for a database connection, with the |
17 | This module replaces the I/O handlers for a database connection, with the |
18 | effect that "patched" database handles no longer block the all threads of |
18 | effect that "patched" database handles no longer block all threads of a |
19 | a process, but only the thread that does the request. |
19 | process, but only the thread that does the request. It should work for |
|
|
20 | both L<DBD::mysql> and L<DBD::MariaDB> connections and a wide range of |
|
|
21 | mariadb/mysql client libraries. |
20 | |
22 | |
21 | This can be used to make parallel sql requests using Coro, or to do other |
23 | This can be used to make parallel sql requests using Coro, or to do other |
22 | stuff while mariadb is rumbling in the background. |
24 | stuff while mariadb is rumbling in the background. |
23 | |
25 | |
24 | =head2 CAVEAT |
26 | =head2 CAVEAT |
25 | |
27 | |
26 | Note that this module must be linked against exactly the same (shared, |
28 | Note that this module must be linked against exactly the same (shared, |
27 | possibly not working with all OSes) F<libmariadb>/F<libmysqlclient> |
29 | possibly not working with all OSes) F<libmariadb>/F<libmysqlclient> |
28 | library as L<DBD::mysql>, otherwise it will not work. |
30 | library as L<DBD::MariaDB>/L<DBD::mysql>, otherwise it will not work. |
29 | |
31 | |
30 | Also, while this module makes database handles non-blocking, you still |
32 | Also, while this module makes database handles non-blocking, you still |
31 | cannot run multiple requests in parallel on the same database handle. If |
33 | cannot run multiple requests in parallel on the same database handle. If |
32 | you want to run multiple queries in parallel, you have to create multiple |
34 | you want to run multiple queries in parallel, you have to create multiple |
33 | database connections, one for each thread that runs queries. Not doing |
35 | database connections, one for each thread that runs queries. Not doing |
… | |
… | |
68 | safely destroyed, but it's not clear how mysql itself will react to a |
70 | safely destroyed, but it's not clear how mysql itself will react to a |
69 | cancellation. |
71 | cancellation. |
70 | |
72 | |
71 | =head1 FUNCTIONS |
73 | =head1 FUNCTIONS |
72 | |
74 | |
73 | Coro::Mysql offers a single user-accessible function: |
75 | Coro::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 | |
125 | sub unblock { |
127 | sub 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 | |
|
|
154 | Returns true iff the database handle was successfully patched for |
|
|
155 | non-blocking operations. |
|
|
156 | |
|
|
157 | =cut |
|
|
158 | |
|
|
159 | sub 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 | |
|
|
175 | Returns true if this Coro::Mysql installation is compiled with special |
|
|
176 | support for L<EV> or not. |
|
|
177 | |
|
|
178 | Even if compiled in, it will only be used if L<EV> is actually the |
|
|
179 | AnyEvent event backend. |
|
|
180 | |
|
|
181 | =cut |
144 | |
182 | |
145 | 1; |
183 | 1; |
146 | |
184 | |
147 | =back |
185 | =back |
148 | |
186 | |