… | |
… | |
26 | F<libmysqlclient> library as DBD::mysql, otherwise it will not work. |
26 | F<libmysqlclient> library as DBD::mysql, otherwise it will not work. |
27 | |
27 | |
28 | Also, while this module makes database handles non-blocking, you still |
28 | Also, while this module makes database handles non-blocking, you still |
29 | cannot run multiple requests in parallel on the same database handle. If |
29 | cannot run multiple requests in parallel on the same database handle. If |
30 | you want to run multiple queries in parallel, you have to create multiple |
30 | you want to run multiple queries in parallel, you have to create multiple |
31 | database connections, one for each thread that runs queries. |
31 | database connections, one for each thread that runs queries. Not doing so |
|
|
32 | can corrupt your data - use a Coro::Semaphore when in doubt. |
32 | |
33 | |
33 | If you make sure that you never run two or more requests in parallel, you |
34 | If you make sure that you never run two or more requests in parallel, you |
34 | cna freely share the database handles between threads, of course. |
35 | can freely share the database handles between threads, of course. |
35 | |
36 | |
36 | Also, this module uses a number of "unclean" techniques (patching an |
37 | Also, this module uses a number of "unclean" techniques (patching an |
37 | internal libmysql structure for one thing) and was hacked within a few |
38 | internal libmysql structure for one thing) and was hacked within a few |
38 | hours on a long flight to Malaysia. |
39 | hours on a long flight to Malaysia. |
39 | |
40 | |
40 | It does, however, check whether it indeed got the structure layout |
41 | It does, however, check whether it indeed got the structure layout |
41 | correct, so you should expect perl exceptions or early crashes as opposed |
42 | correct, so you should expect perl exceptions or early crashes as opposed |
42 | to data corruption when something goes wrong. |
43 | to data corruption when something goes wrong during patching. |
43 | |
44 | |
44 | =head2 SPEED |
45 | =head2 SPEED |
45 | |
46 | |
46 | This module is implemented in XS, and as long as mysqld replies quickly |
47 | This module is implemented in XS, and as long as mysqld replies quickly |
47 | enough, it adds no overhead to the standard libmysql communication |
48 | enough, it adds no overhead to the standard libmysql communication |
48 | routines (which are very badly written). |
49 | routines (which are very badly written, btw.). |
49 | |
50 | |
50 | For very fast queries ("select 0"), this module can add noticable overhead |
51 | For very fast queries ("select 0"), this module can add noticable overhead |
51 | (around 15%) as it tries to switch to other coroutines when mysqld doesn't |
52 | (around 15%) as it tries to switch to other coroutines when mysqld doesn't |
52 | deliver the data instantly. |
53 | deliver the data instantly. |
53 | |
54 | |
54 | For most types of queries, there will be no overhead, especially on |
55 | For most types of queries, there will be no overhead, especially on |
55 | multicore systems where your perl process can do other things while mysqld |
56 | multicore systems where your perl process can do other things while mysqld |
56 | does its stuff. |
57 | does its stuff. |
|
|
58 | |
|
|
59 | =head2 LIMITATIONS |
|
|
60 | |
|
|
61 | This module only supports "standard" mysql connection handles - this |
|
|
62 | means unix domain or TCP sockets, and excludes SSL/TLS connections, named |
|
|
63 | pipes (windows) and shared memory (also windows). No support for these |
|
|
64 | connection types is planned, either. |
|
|
65 | |
|
|
66 | =head1 FUNCTIONS |
|
|
67 | |
|
|
68 | Coro::Mysql offers a single user-accessible function: |
57 | |
69 | |
58 | =over 4 |
70 | =over 4 |
59 | |
71 | |
60 | =cut |
72 | =cut |
61 | |
73 | |
… | |
… | |
75 | |
87 | |
76 | sub readable { &Coro::Handle::FH::readable } |
88 | sub readable { &Coro::Handle::FH::readable } |
77 | sub writable { &Coro::Handle::FH::writable } |
89 | sub writable { &Coro::Handle::FH::writable } |
78 | |
90 | |
79 | BEGIN { |
91 | BEGIN { |
80 | our $VERSION = '0.1'; |
92 | our $VERSION = '1.0'; |
81 | |
93 | |
82 | require XSLoader; |
94 | require XSLoader; |
83 | XSLoader::load Coro::Mysql::, $VERSION; |
95 | XSLoader::load Coro::Mysql::, $VERSION; |
84 | } |
96 | } |
85 | |
97 | |
… | |
… | |
89 | so it becomes compatible to Coro threads. |
101 | so it becomes compatible to Coro threads. |
90 | |
102 | |
91 | After that, it returns the patched handle - you should always use the |
103 | After that, it returns the patched handle - you should always use the |
92 | newly returned database handle. |
104 | newly returned database handle. |
93 | |
105 | |
|
|
106 | It is safe to call this function on any database handle (or just about any |
|
|
107 | value), but it will only do anything to L<DBD::mysql> handles, others are |
|
|
108 | returned unchanged. That means it is harmless when applied to database |
|
|
109 | handles of other databases. |
|
|
110 | |
94 | =cut |
111 | =cut |
95 | |
112 | |
96 | sub unblock { |
113 | sub unblock { |
97 | my ($DBH) = @_; |
114 | my ($DBH) = @_; |
98 | my $sock = $DBH->{sock}; |
|
|
99 | |
115 | |
100 | open my $fh, "+>&" . $DBH->{sockfd} |
116 | if ($DBH->{Driver}{Name} eq "mysql") { |
101 | or croak "Coro::Mysql unable to clone mysql fd"; |
117 | my $sock = $DBH->{sock}; |
102 | |
118 | |
103 | $fh = Coro::Handle::unblock $fh; |
119 | open my $fh, "+>&" . $DBH->{sockfd} |
|
|
120 | or croak "Coro::Mysql unable to clone mysql fd"; |
104 | |
121 | |
|
|
122 | $fh = Coro::Handle::unblock $fh; |
|
|
123 | |
105 | _patch $sock, $DBH->{sockfd}, tied ${$fh}; |
124 | _patch $sock, $DBH->{sockfd}, $fh, tied ${$fh}; |
106 | $DBH->{private_Coro_Mysql} = guard { |
|
|
107 | _unpatch $sock; |
|
|
108 | undef $fh; |
|
|
109 | }; |
125 | } |
110 | |
126 | |
111 | $DBH |
127 | $DBH |
112 | } |
128 | } |
113 | |
129 | |
114 | 1; |
130 | 1; |
115 | |
131 | |
116 | =back |
132 | =back |
117 | |
133 | |
|
|
134 | =head1 USAGE EXAMPLE |
|
|
135 | |
|
|
136 | This example uses L<PApp::SQL> and L<Coro::on_enter> to implement a |
|
|
137 | function C<with_db>, that connects to a database, uses C<unblock> on the |
|
|
138 | resulting handle and then makes sure that C<$PApp::SQL::DBH> is set to the |
|
|
139 | (per-thread) database handle when the given thread is running (it does not |
|
|
140 | restore any previous value of $PApp::SQL::DBH, however): |
|
|
141 | |
|
|
142 | use Coro; |
|
|
143 | use Coro::Mysql; |
|
|
144 | use PApp::SQL; |
|
|
145 | |
|
|
146 | sub with_db($$$&) { |
|
|
147 | my ($database, $user, $pass, $cb) = @_; |
|
|
148 | |
|
|
149 | my $dbh = Coro::Mysql::unblock DBI->connect ($database, $user, $pass) |
|
|
150 | or die $DBI::errstr; |
|
|
151 | |
|
|
152 | Coro::on_enter { $PApp::SQL::DBH = $dbh }; |
|
|
153 | |
|
|
154 | $cb->(); |
|
|
155 | } |
|
|
156 | |
|
|
157 | This function makes it possible to easily use L<PApp::SQL> with |
|
|
158 | L<Coro::Mysql>, without worrying about database handles. |
|
|
159 | |
|
|
160 | # now start 10 threads doing stuff |
|
|
161 | async { |
|
|
162 | |
|
|
163 | with_db "DBI:mysql:test", "", "", sub { |
|
|
164 | sql_exec "update table set col = 5 where id = 7"; |
|
|
165 | |
|
|
166 | my $st = sql_exec \my ($id, $name), |
|
|
167 | "select id, name from table where name like ?", |
|
|
168 | "a%"; |
|
|
169 | |
|
|
170 | while ($st->fetch) { |
|
|
171 | ... |
|
|
172 | } |
|
|
173 | |
|
|
174 | my $id = sql_insertid sql_exec "insert into table values (1,2,3)"; |
|
|
175 | # etc. |
|
|
176 | }; |
|
|
177 | |
|
|
178 | } for 1..10; |
|
|
179 | |
|
|
180 | =head1 SEE ALSO |
|
|
181 | |
|
|
182 | L<Coro>, L<PApp::SQL> (a user friendly but efficient wrapper around DBI). |
|
|
183 | |
118 | =head1 AUTHOR |
184 | =head1 AUTHOR |
119 | |
185 | |
120 | Marc Lehmann <schmorp@schmorp.de> |
186 | Marc Lehmann <schmorp@schmorp.de> |
121 | http://home.schmorp.de/ |
187 | http://home.schmorp.de/ |
122 | |
188 | |