| 1 | package Mojo::Pg::PubSub; | ||
| 2 | use Mojo::Base 'Mojo::EventEmitter'; | ||
| 3 | |||
| 4 | use Mojo::JSON qw(from_json to_json); | ||
| 5 | use Scalar::Util qw(weaken); | ||
| 6 | |||
| 7 | has pg => undef, weak => 1; | ||
| 8 | has reconnect_interval => 1; | ||
| 9 | |||
| 10 | sub db { | ||
| 11 | my $self = shift; | ||
| 12 | |||
| 13 | return $self->{db} if $self->{db}; | ||
| 14 | |||
| 15 | my $db = $self->{db} = $self->pg->db; | ||
| 16 | weaken $self; | ||
| 17 | $db->on( | ||
| 18 | notification => sub { | ||
| 19 | my ($db, $name, $pid, $payload) = @_; | ||
| 20 | return unless my $cbs = $self->{chans}{$name}; | ||
| 21 | $payload = eval { from_json $payload } if $self->{json}{$name}; | ||
| 22 | for my $cb (@$cbs) { $self->$cb($payload) } | ||
| 23 | } | ||
| 24 | ); | ||
| 25 | |||
| 26 | $db->once(close => sub { $self->emit(disconnect => delete $self->{db}) }); | ||
| 27 | $db->listen($_) for keys %{$self->{chans}}, 'mojo.pubsub'; | ||
| 28 | delete $self->{reconnecting}; | ||
| 29 | $self->emit(reconnect => $db); | ||
| 30 | |||
| 31 | return $db; | ||
| 32 | } | ||
| 33 | |||
| 34 | sub DESTROY { shift->reset unless ${^GLOBAL_PHASE} eq 'DESTRUCT' } | ||
| 35 | |||
| 36 | sub json { ++$_[0]{json}{$_[1]} and return $_[0] } | ||
| 37 | |||
| 38 | sub listen { | ||
| 39 | my ($self, $name, $cb) = @_; | ||
| 40 | $self->db->listen($name) if !@{$self->{chans}{$name} ||= []} && !$self->{reconnecting}; | ||
| 41 | push @{$self->{chans}{$name}}, $cb; | ||
| 42 | return $cb; | ||
| 43 | } | ||
| 44 | |||
| 45 | sub new { | ||
| 46 | my $self = shift->SUPER::new(@_); | ||
| 47 | $self->on(disconnect => \&_disconnect); | ||
| 48 | return $self; | ||
| 49 | } | ||
| 50 | |||
| 51 | sub notify { $_[0]->db->notify(_json(@_)) and return $_[0] } | ||
| 52 | |||
| 53 | sub reset { | ||
| 54 | my $self = shift; | ||
| 55 | delete @$self{qw(chans json pid)}; | ||
| 56 | return unless my $db = delete $self->{db}; | ||
| 57 | ++$db->dbh->{private_mojo_no_reuse} and $db->_unwatch; | ||
| 58 | } | ||
| 59 | |||
| 60 | sub unlisten { | ||
| 61 | my ($self, $name, $cb) = @_; | ||
| 62 | |||
| 63 | my $chan = $self->{chans}{$name}; | ||
| 64 | unless (@$chan = $cb ? grep { $cb ne $_ } @$chan : ()) { | ||
| 65 | $self->db->unlisten($name) unless $self->{reconnecting}; | ||
| 66 | delete $self->{chans}{$name}; | ||
| 67 | } | ||
| 68 | |||
| 69 | return $self; | ||
| 70 | } | ||
| 71 | |||
| 72 | sub _disconnect { | ||
| 73 | my $self = shift; | ||
| 74 | |||
| 75 | $self->{reconnecting} = 1; | ||
| 76 | |||
| 77 | weaken $self; | ||
| 78 | my $r; | ||
| 79 | $r = Mojo::IOLoop->recurring( | ||
| 80 | $self->reconnect_interval => sub { | ||
| 81 | Mojo::IOLoop->remove($r) if eval { $self->db }; | ||
| 82 | } | ||
| 83 | ); | ||
| 84 | } | ||
| 85 | |||
| 86 | sub _json { $_[1], $_[0]{json}{$_[1]} ? to_json $_[2] : $_[2] } | ||
| 87 | |||
| 88 | 1; | ||
| 89 | |||
| 90 | =encoding utf8 | ||
| 91 | |||
| 92 | =head1 NAME | ||
| 93 | |||
| 94 | Mojo::Pg::PubSub - Publish/Subscribe | ||
| 95 | |||
| 96 | =head1 SYNOPSIS | ||
| 97 | |||
| 98 | use Mojo::Pg::PubSub; | ||
| 99 | |||
| 100 | my $pubsub = Mojo::Pg::PubSub->new(pg => $pg); | ||
| 101 | my $cb = $pubsub->listen(foo => sub ($pubsub, $payload) { | ||
| 102 | say "Received: $payload"; | ||
| 103 | }); | ||
| 104 | $pubsub->notify(foo => 'I ♥ Mojolicious!'); | ||
| 105 | $pubsub->unlisten(foo => $cb); | ||
| 106 | |||
| 107 | =head1 DESCRIPTION | ||
| 108 | |||
| 109 | L<Mojo::Pg::PubSub> is a scalable implementation of the publish/subscribe pattern used by L<Mojo::Pg>. It is based on | ||
| 110 | PostgreSQL notifications and allows many consumers to share the same database connection, to avoid many common | ||
| 111 | scalability problems. | ||
| 112 | |||
| 113 | =head1 EVENTS | ||
| 114 | |||
| 115 | L<Mojo::Pg::PubSub> inherits all events from L<Mojo::EventEmitter> and can emit the following new ones. | ||
| 116 | |||
| 117 | =head2 disconnect | ||
| 118 | |||
| 119 | $pubsub->on(disconnect => sub ($pubsub, $db) { | ||
| 120 | ... | ||
| 121 | }); | ||
| 122 | |||
| 123 | Emitted after the current database connection is lost. | ||
| 124 | |||
| 125 | =head2 reconnect | ||
| 126 | |||
| 127 | $pubsub->on(reconnect => sub ($pubsub, $db) { | ||
| 128 | ... | ||
| 129 | }); | ||
| 130 | |||
| 131 | Emitted after switching to a new database connection for sending and receiving notifications. | ||
| 132 | |||
| 133 | =head1 ATTRIBUTES | ||
| 134 | |||
| 135 | L<Mojo::Pg::PubSub> implements the following attributes. | ||
| 136 | |||
| 137 | =head2 pg | ||
| 138 | |||
| 139 | my $pg = $pubsub->pg; | ||
| 140 | $pubsub = $pubsub->pg(Mojo::Pg->new); | ||
| 141 | |||
| 142 | L<Mojo::Pg> object this publish/subscribe container belongs to. Note that this attribute is weakened. | ||
| 143 | |||
| 144 | =head2 reconnect_interval | ||
| 145 | |||
| 146 | my $interval = $pubsub->reconnect_interval; | ||
| 147 | $pubsub = $pubsub->reconnect_interval(0.1); | ||
| 148 | |||
| 149 | Amount of time in seconds to wait to reconnect after disconnecting, defaults to C<1>. | ||
| 150 | |||
| 151 | =head1 METHODS | ||
| 152 | |||
| 153 | L<Mojo::Pg::PubSub> inherits all methods from L<Mojo::EventEmitter> and implements the following new ones. | ||
| 154 | |||
| 155 | =head2 db | ||
| 156 | |||
| 157 | my $db = $pubsub->db; | ||
| 158 | |||
| 159 | Build and cache or get cached L<Mojo::Pg::Database> connection from L</"pg">. Used to reconnect if disconnected. | ||
| 160 | |||
| 161 | # Reconnect immediately | ||
| 162 | $pubsub->unsubscribe('disconnect')->on(disconnect => sub ($pubsub, $db) { pubsub->db }); | ||
| 163 | |||
| 164 | =head2 json | ||
| 165 | |||
| 166 | $pubsub = $pubsub->json('foo'); | ||
| 167 | |||
| 168 | Activate automatic JSON encoding and decoding with L<Mojo::JSON/"to_json"> and L<Mojo::JSON/"from_json"> for a channel. | ||
| 169 | |||
| 170 | # Send and receive data structures | ||
| 171 | $pubsub->json('foo')->listen(foo => sub ($pubsub, $payload) { | ||
| 172 | say $payload->{bar}; | ||
| 173 | }); | ||
| 174 | $pubsub->notify(foo => {bar => 'I ♥ Mojolicious!'}); | ||
| 175 | |||
| 176 | =head2 listen | ||
| 177 | |||
| 178 | my $cb = $pubsub->listen(foo => sub {...}); | ||
| 179 | |||
| 180 | Subscribe to a channel, there is no limit on how many subscribers a channel can have. Automatic decoding of JSON text | ||
| 181 | to Perl values can be activated with L</"json">. | ||
| 182 | |||
| 183 | # Subscribe to the same channel twice | ||
| 184 | $pubsub->listen(foo => sub ($pubsub, $payload) { | ||
| 185 | say "One: $payload"; | ||
| 186 | }); | ||
| 187 | $pubsub->listen(foo => sub ($pubsub, $payload) { | ||
| 188 | say "Two: $payload"; | ||
| 189 | }); | ||
| 190 | |||
| 191 | =head2 new | ||
| 192 | |||
| 193 | my $pubsub = Mojo::Pg::PubSub->new; | ||
| 194 | my $pubsub = Mojo::Pg::PubSub->new(pg => Mojo::Pg->new); | ||
| 195 | my $pubsub = Mojo::Pg::PubSub->new({pg => Mojo::Pg->new}); | ||
| 196 | |||
| 197 | Construct a new L<Mojo::Pg::PubSub> object and subscribe to the L</"disconnect"> event with default reconnect logic. | ||
| 198 | |||
| 199 | =head2 notify | ||
| 200 | |||
| 201 | $pubsub = $pubsub->notify('foo'); | ||
| 202 | $pubsub = $pubsub->notify(foo => 'I ♥ Mojolicious!'); | ||
| 203 | $pubsub = $pubsub->notify(foo => {bar => 'baz'}); | ||
| 204 | |||
| 205 | Notify a channel. Automatic encoding of Perl values to JSON text can be activated with L</"json">. | ||
| 206 | |||
| 207 | =head2 reset | ||
| 208 | |||
| 209 | $pubsub->reset; | ||
| 210 | |||
| 211 | Reset all subscriptions and the database connection. This is usually done after a new process has been forked, to | ||
| 212 | prevent the child process from stealing notifications meant for the parent process. | ||
| 213 | |||
| 214 | =head2 unlisten | ||
| 215 | |||
| 216 | $pubsub = $pubsub->unlisten('foo'); | ||
| 217 | $pubsub = $pubsub->unlisten(foo => $cb); | ||
| 218 | |||
| 219 | Unsubscribe from a channel. | ||
| 220 | |||
| 221 | =head1 SEE ALSO | ||
| 222 | |||
| 223 | L<Mojo::Pg>, L<Mojolicious::Guides>, L<https://mojolicious.org>. | ||
| 224 | |||
| 225 | =cut |