| 1 | package DBIO::Forked; | ||
| 2 | 2 | our $VERSION = '0.900001'; | |
| 3 | # ABSTRACT: Dependency-free, fork-based async layer for DBIO drivers | ||
| 4 | |||
| 5 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 2 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | |||
| 8 | 2 | ▶ | use base 'DBIO::Base'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | # Loading the storage backend registers the generic 'forked' async mode on the | ||
| 11 | # core base storage class (ADR 0030), so that `use DBIO::Forked` is enough to | ||
| 12 | # make `connect(..., { async => 'forked' })` available on every driver. | ||
| 13 | 2 | ▶ | use DBIO::Forked::Storage; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 14 | |||
| 15 | |||
| 16 | 2 | 1; | |
| 17 | |||
| 18 | __END__ | ||
| 19 | |||
| 20 | =pod | ||
| 21 | |||
| 22 | =encoding UTF-8 | ||
| 23 | |||
| 24 | =head1 NAME | ||
| 25 | |||
| 26 | DBIO::Forked - Dependency-free, fork-based async layer for DBIO drivers | ||
| 27 | |||
| 28 | =head1 VERSION | ||
| 29 | |||
| 30 | version 0.900001 | ||
| 31 | |||
| 32 | =head1 SYNOPSIS | ||
| 33 | |||
| 34 | See L<DBIO::Forked::Storage> for the storage backend and L<DBIO::Forked::Future> | ||
| 35 | for the loop-free Future. | ||
| 36 | |||
| 37 | =head1 DESCRIPTION | ||
| 38 | |||
| 39 | Dependency-free, fork-based async layer for L<DBIO> drivers. It makes B<any> | ||
| 40 | sync DBIO driver async without an async-capable database client and without an | ||
| 41 | event loop: each query runs in a C<fork()>ed child that speaks the ordinary | ||
| 42 | sync driver, and the result rows are streamed back to the parent over a pipe. | ||
| 43 | The parent returns a L<DBIO::Forked::Future> immediately. | ||
| 44 | |||
| 45 | C<DBIO::Forked> is a sibling of L<DBIO::Async> on the same layer -- both | ||
| 46 | satisfy the core L<DBIO::Storage::Async> contract -- but they take opposite | ||
| 47 | routes. L<DBIO::Async> drives a driver's B<own> async binding -- the non-blocking | ||
| 48 | interface a DBD such as L<DBD::Pg> (C<pg_async>) or L<DBD::mysql> (C<mysql_async>) | ||
| 49 | exposes itself -- through L<Future::IO>, so it works only for drivers that offer | ||
| 50 | one, but pulls in no extra client library. C<DBIO::Forked> uses C<fork()> + pipe | ||
| 51 | + the plain sync driver in the child, so it works for B<every> driver, including | ||
| 52 | the ones that never expose an async binding (Oracle, SQLite, DB2, Sybase, ...). | ||
| 53 | |||
| 54 | =head1 ACTIVATION | ||
| 55 | |||
| 56 | Loading C<DBIO::Forked> registers a generic C<forked> async I<mode> on the core | ||
| 57 | base storage class (ADR 0030): | ||
| 58 | |||
| 59 | DBIO::Storage::DBI->register_async_mode( forked => 'DBIO::Forked::Storage' ); | ||
| 60 | |||
| 61 | so every DBIO driver inherits it. The user then opts a connection into it | ||
| 62 | per-connection, at C<connect> time: | ||
| 63 | |||
| 64 | use DBIO::Forked; | ||
| 65 | my $schema = MyApp::Schema->connect($dsn, $user, $pass, { async => 'forked' }); | ||
| 66 | |||
| 67 | A connection opened with C<< { async => 'forked' } >> answers the six | ||
| 68 | C<*_async> methods (and the C<*_async> helpers on ResultSet/Row) through | ||
| 69 | L<DBIO::Forked::Storage>; a connection opened without it stays fully synchronous | ||
| 70 | (its C<*_async> methods croak). There is no auto-selection -- the mode is | ||
| 71 | explicit or absent. The core resolver constructs | ||
| 72 | C<< DBIO::Forked::Storage->new($schema) >> as the embedded async backend; each | ||
| 73 | async query then forks a child that reconnects the sync driver fresh and runs | ||
| 74 | the ordinary sync CRUD before streaming the result back. | ||
| 75 | |||
| 76 | =head1 DEPENDENCY POSTURE | ||
| 77 | |||
| 78 | Only core Perl -- C<fork>, C<pipe>, L<Storable> (serialization), L<IO::Select> | ||
| 79 | (waiting) -- plus L<DBIO> core. No L<Future>, no L<Future::IO>, no event loop, | ||
| 80 | no async DB client. That is the whole point: turning a sync driver async pulls | ||
| 81 | in none of the async ecosystem. | ||
| 82 | |||
| 83 | =head1 EXECUTION MODEL | ||
| 84 | |||
| 85 | Model A -- one short-lived C<fork()> per query. The child inherits the entire | ||
| 86 | parent memory (including the real driver's sync storage), throws away the | ||
| 87 | inherited DBI handle (the fork trap that L<DBIO::Storage::DBI> already guards | ||
| 88 | against), reconnects fresh from the DBI-form connect info, runs the driver's | ||
| 89 | B<ordinary> sync CRUD (no SQL is re-implemented in C<DBIO::Forked>), serializes | ||
| 90 | the result rows back over the pipe with L<Storable>, and exits. The parent | ||
| 91 | returns a L<DBIO::Forked::Future> bound to the pipe read fd. | ||
| 92 | |||
| 93 | =head1 TRANSACTION SEMANTICS | ||
| 94 | |||
| 95 | Model A forks per unit of work, so a multi-statement transaction cannot be spread | ||
| 96 | across forks. C<txn_do_async> therefore runs the B<entire> transaction block in | ||
| 97 | B<one> C<fork()>ed child: the C<BEGIN>, every statement in the block, and the | ||
| 98 | final C<COMMIT>/C<ROLLBACK> all execute synchronously in that single child against | ||
| 99 | the freshly-reconnected sync driver. Only the block's overall result is streamed | ||
| 100 | back to the parent as a L<DBIO::Forked::Future>. | ||
| 101 | |||
| 102 | So C<DBIO::Forked> makes a transaction async at the granularity of the B<whole | ||
| 103 | block>, not of individual statements. From the parent's side the block is | ||
| 104 | non-blocking -- you get a Future immediately and the event loop (or other forks) | ||
| 105 | runs while the child works -- but B<inside> the transaction there are no loop | ||
| 106 | ticks between statements: the statements do not each return asynchronously, and | ||
| 107 | you cannot issue one statement, yield to the loop, and issue the next on the same | ||
| 108 | pinned transaction. | ||
| 109 | |||
| 110 | If you need genuine per-statement async transactions over a pinned connection | ||
| 111 | (send a statement non-blocking, let the loop do other work, then send the next), | ||
| 112 | use a connection-based backend instead -- L<DBIO::Async> (L<Future::IO> over the | ||
| 113 | driver's own async binding) or a native EV add-on (L<DBIO::PostgreSQL::EV>, | ||
| 114 | L<DBIO::MySQL::EV>) -- not C<DBIO::Forked>. This is an intrinsic property of | ||
| 115 | Model A, not a defect. | ||
| 116 | |||
| 117 | =head1 AUTHOR | ||
| 118 | |||
| 119 | DBIO Authors | ||
| 120 | |||
| 121 | =head1 COPYRIGHT AND LICENSE | ||
| 122 | |||
| 123 | Copyright (C) 2026 DBIO Authors | ||
| 124 | |||
| 125 | This is free software; you can redistribute it and/or modify it under | ||
| 126 | the same terms as the Perl 5 programming language system itself. | ||
| 127 | |||
| 128 | =cut | ||