… | |
… | |
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 extra latency, 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 | |
… | |
… | |
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 } |
… | |
… | |
118 | my $sock = $DBH->{sock}; |
130 | my $sock = $DBH->{sock}; |
119 | |
131 | |
120 | open my $fh, "+>&" . $DBH->{sockfd} |
132 | open my $fh, "+>&" . $DBH->{sockfd} |
121 | or croak "Coro::Mysql unable to clone mysql fd"; |
133 | or croak "Coro::Mysql unable to clone mysql fd"; |
122 | |
134 | |
|
|
135 | if (AnyEvent::detect ne "AnyEvent::Impl::EV" || !_use_ev) { |
|
|
136 | require Coro::Handle; |
123 | $fh = Coro::Handle::unblock $fh; |
137 | $fh = Coro::Handle::unblock ($fh); |
|
|
138 | } |
124 | |
139 | |
125 | _patch $sock, $DBH->{sockfd}, $fh, tied ${$fh}; |
140 | _patch $sock, $DBH->{sockfd}, $fh, tied ${$fh}; |
126 | } |
141 | } |
127 | |
142 | |
128 | $DBH |
143 | $DBH |
… | |
… | |
145 | use PApp::SQL; |
160 | use PApp::SQL; |
146 | |
161 | |
147 | sub with_db($$$&) { |
162 | sub with_db($$$&) { |
148 | my ($database, $user, $pass, $cb) = @_; |
163 | my ($database, $user, $pass, $cb) = @_; |
149 | |
164 | |
150 | my $dbh = Coro::Mysql::unblock DBI->connect ($database, $user, $pass) |
165 | my $dbh = DBI->connect ($database, $user, $pass)->Coro::Mysql::unblock |
151 | or die $DBI::errstr; |
166 | or die $DBI::errstr; |
152 | |
167 | |
153 | Coro::on_enter { $PApp::SQL::DBH = $dbh }; |
168 | Coro::on_enter { $PApp::SQL::DBH = $dbh }; |
154 | |
169 | |
155 | $cb->(); |
170 | $cb->(); |