Pg/PubSub.pm

filter: Statementstmt 26.0% 37 / 142 Branchbran 10.2% 8 / 78 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 47.6% 10 / 21 Podpod 55.5% 5 / 9 totaltotal 24.7% 63 / 255 CC 42 SCAR 31.0
CC
42
Cov
21%
_process_notifications
56.0
notify
42.0
unsubscribe
30.0
CRAP 22.2
↑ Summary Pg/Util.pm »
1 package IO::Async::Pg::PubSub;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use parent 'IO::Async::Notifier';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use IO::Async::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
11 2 my ($class, %args) = @_;
12  
13 my $self = bless {
14 conn => undef,
15 pool => $args{pool},
16 2 channels => {}, # channel => [callbacks]
17 connected => 0,
18 _handle => undef,
19 }, $class;
20  
21 2 return $self;
22 }
23  
24 # Accessors
25 1 ▶sub is_connected { shift->{connected} }
Subroutine is_connected: called CC=1 SCAR=0.0
Pod undocumented
26 1 ▶sub subscribed_channels { scalar keys %{shift->{channels}} }
Subroutine subscribed_channels: called CC=1 SCAR=0.0
Pod undocumented
27  
28 # Validate channel name to prevent injection
29 7 ▶sub _validate_channel {
Subroutine _validate_channel: called CC=5 SCAR=16.2
30 7 my ($self, $channel) = @_;
31  
32 7 ▶ return 0 unless defined $channel && length $channel;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $channel and length $channel 1 6 7
Condition66%2/3
defined $channel and length $channel
!ll&&!rl&&r
016
MC/DC50%1/2
defined $channel and length $channel
defined $channel length $channel
Truth table66%2/3
A and B
A= defined $channelB= length $channel
ABresult
0X0
100
111
33 6 ▶ return 0 if $channel =~ /[\s;'"\\]/; # No whitespace, semicolon, quotes, backslash
Branch: 1 branch
BranchTrueFalseTotal
if $channel =~ /[\s;'"\\]/ 3 3 6
34 3 ▶ return 0 if $channel =~ /[\x00-\x1f]/; # No control characters
Branch: 1 branch
BranchTrueFalseTotal
if $channel =~ /[\x00-\x1f]/ 0 3 3
35 3 return 1;
36 }
37  
38 # Connect to the database for listening
39 0 ▶async sub connect {
Subroutine connect: not called CC=3 SCAR=24.8
Pod undocumented
40 0 my ($self) = @_;
41  
42 0 ▶ return if $self->{connected};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"connected"} 0 0 0
43  
44 0 ▶ my $pool = $self->{pool} or die "No pool configured";
Branch: 1 branch
BranchTrueFalseTotal
my $pool = $self->{"pool"} or die "No pool configured" 0 0 0
45  
46 # Get a dedicated connection for listening
47 0 $self->{conn} = await $pool->connection;
48 0 $self->{connected} = 1;
49  
50 # Set up socket watching
51 0 $self->_setup_socket_watcher;
52  
53 0 return $self;
54 1 }
55  
56 # Subscribe to a channel
57 0 ▶async sub subscribe {
Subroutine subscribe: not called CC=4 SCAR=30.0
Pod documented
58 0 my ($self, $channel, $callback) = @_;
59  
60 0 ▶ die "Invalid channel name: $channel"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_validate_channel($channel) 0 0 0
61 unless $self->_validate_channel($channel);
62  
63 # Ensure connected
64 0 ▶ await $self->connect unless $self->{connected};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"connected"} 0 0 0
65  
66 # Add callback to list
67 0 push @{$self->{channels}{$channel}}, $callback;
68  
69 # If first subscription to this channel, send LISTEN
70 0 ▶ if (@{$self->{channels}{$channel}} == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"channels"}{$channel};} == 1) 0 0 0
71 0 await $self->{conn}->query("LISTEN $channel");
72 }
73  
74 0 return $self;
75 1 }
76  
77 # Unsubscribe from a channel
78 0 ▶async sub unsubscribe {
Subroutine unsubscribe: not called CC=5 SCAR=34.0
Pod documented
79 0 my ($self, $channel, $callback) = @_;
80  
81 0 ▶ return unless exists $self->{channels}{$channel};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"channels"}{$channel} 0 0 0
82  
83 0 ▶ if ($callback) {
Branch: 1 branch
BranchTrueFalseTotal
if ($callback) { } 0 0 0
84 # Remove specific callback
85 0 @{$self->{channels}{$channel}} =
86 0 grep { $_ != $callback } @{$self->{channels}{$channel}};
87 }
88 else {
89 # Remove all callbacks
90 0 $self->{channels}{$channel} = [];
91 }
92  
93 # If no more callbacks, send UNLISTEN
94 0 ▶ if (!@{$self->{channels}{$channel}}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$self->{"channels"}{$channel};}) 0 0 0
95 0 delete $self->{channels}{$channel};
96 0 ▶ if ($self->{conn}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"conn"}) 0 0 0
97 0 await $self->{conn}->query("UNLISTEN $channel");
98 }
99 }
100  
101 0 return $self;
102 1 }
103  
104 # Unsubscribe from all channels
105 0 ▶async sub unsubscribe_all {
Subroutine unsubscribe_all: not called CC=2 SCAR=17.9
Pod undocumented
106 0 my ($self) = @_;
107  
108 0 $self->{channels} = {};
109  
110 0 ▶ if ($self->{conn}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"conn"}) 0 0 0
111 0 await $self->{conn}->query("UNLISTEN *");
112 }
113  
114 0 return $self;
115 1 }
116  
117 # Publish/notify a channel
118 0 ▶async sub notify {
Subroutine notify: not called CC=6 SCAR=37.4
Pod documented
119 0 my ($self, $channel, $payload) = @_;
120  
121 0 ▶ die "Invalid channel name: $channel"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_validate_channel($channel) 0 0 0
122 unless $self->_validate_channel($channel);
123  
124 # We can use any connection for NOTIFY
125 0 my $pool = $self->{pool};
126 0 my $conn = $self->{conn};
127  
128 0 ▶ if (!$conn) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $conn) { } 0 0 0
129 # Get a temporary connection
130 0 $conn = await $pool->connection;
131 0 eval {
132 0 ▶ if (defined $payload) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $payload) { } 0 0 0
133 0 await $conn->query("NOTIFY $channel, \$1", $payload);
134 }
135 else {
136 0 await $conn->query("NOTIFY $channel");
137 }
138 };
139 0 my $err = $@;
140 0 $conn->release;
141 0 ▶ die $err if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 0 0
142 }
143 else {
144 0 ▶ if (defined $payload) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $payload) { } 0 0 0
145 0 await $conn->query("NOTIFY $channel, \$1", $payload);
146 }
147 else {
148 0 await $conn->query("NOTIFY $channel");
149 }
150 }
151  
152 0 return $self;
153 1 }
154  
155 # Set up socket watcher for incoming notifications
156 0 ▶sub _setup_socket_watcher {
Subroutine _setup_socket_watcher: not called CC=5 SCAR=34.0
157 0 my ($self) = @_;
158  
159 0 ▶ my $conn = $self->{conn} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $conn = $self->{"conn"} or return 0 0 0
160 0 ▶ my $dbh = $conn->dbh or return;
Branch: 1 branch
BranchTrueFalseTotal
my $dbh = $conn->dbh or return 0 0 0
161  
162 0 my $socket_fd = $dbh->{pg_socket};
163 0 ▶ return unless defined $socket_fd;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $socket_fd 0 0 0
164  
165 0 my $handle;
166 $handle = IO::Async::Handle->new(
167 read_fileno => $socket_fd,
168 want_readready => 1,
169 0 ▶ on_read_ready => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
170 0 $self->_process_notifications;
171 },
172 0 );
173  
174 0 $self->{_handle} = $handle;
175  
176 0 ▶ if (my $loop = $self->loop) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $loop = $self->loop) 0 0 0
177 0 $loop->add($handle);
178 }
179 }
180  
181 # Process incoming notifications
182 0 ▶sub _process_notifications {
Subroutine _process_notifications: not called CC=7 SCAR=40.3
183 0 my ($self) = @_;
184  
185 0 ▶ my $conn = $self->{conn} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $conn = $self->{"conn"} or return 0 0 0
186 0 ▶ my $dbh = $conn->dbh or return;
Branch: 1 branch
BranchTrueFalseTotal
my $dbh = $conn->dbh or return 0 0 0
187  
188 # Check for notifications
189 0 ▶ while (my $notification = $dbh->pg_notifies) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $notification = $dbh->pg_notifies) 0 0 0
190 0 my ($channel, $pid, $payload) = @$notification;
191  
192 # Call all registered callbacks for this channel
193 0 ▶ if (my $callbacks = $self->{channels}{$channel}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $callbacks = $self->{"channels"}{$channel}) 0 0 0
194 0 for my $cb (@$callbacks) {
195 0 eval { $cb->($channel, $payload, $pid) };
196 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
197 0 warn "PubSub callback error for $channel: $@";
198 }
199 }
200 }
201 }
202 }
203  
204 # Override to add handle to loop
205 0 ▶sub _add_to_loop {
Subroutine _add_to_loop: not called CC=3 SCAR=24.8
206 0 my ($self, $loop) = @_;
207 0 $self->SUPER::_add_to_loop($loop);
208  
209 0 ▶ if (my $handle = $self->{_handle}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $handle = $self->{"_handle"}) 0 0 0
210 0 ▶ $loop->add($handle) unless $handle->loop;
Branch: 1 branch
BranchTrueFalseTotal
unless $handle->loop 0 0 0
211 }
212 }
213  
214 # Override to remove handle from loop
215 0 ▶sub _remove_from_loop {
Subroutine _remove_from_loop: not called CC=3 SCAR=24.8
216 0 my ($self, $loop) = @_;
217  
218 0 ▶ if (my $handle = $self->{_handle}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $handle = $self->{"_handle"}) 0 0 0
219 0 ▶ $loop->remove($handle) if $handle->loop;
Branch: 1 branch
BranchTrueFalseTotal
if $handle->loop 0 0 0
220 }
221  
222 0 $self->SUPER::_remove_from_loop($loop);
223 }
224  
225 # Disconnect and cleanup
226 0 ▶async sub disconnect {
Subroutine disconnect: not called CC=5 SCAR=34.0
Pod documented
227 0 my ($self) = @_;
228  
229 0 ▶ return unless $self->{connected};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"connected"} 0 0 0
230  
231 # Remove socket watcher
232 0 ▶ if (my $handle = $self->{_handle}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $handle = $self->{"_handle"}) 0 0 0
233 0 ▶ if (my $loop = $handle->loop) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $loop = $handle->loop) 0 0 0
234 0 $loop->remove($handle);
235 }
236 0 $self->{_handle} = undef;
237 }
238  
239 # UNLISTEN all
240 0 ▶ if ($self->{conn}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"conn"}) 0 0 0
241 0 eval { await $self->{conn}->query("UNLISTEN *") };
242 0 $self->{conn}->release;
243 0 $self->{conn} = undef;
244 }
245  
246 0 $self->{channels} = {};
247 0 $self->{connected} = 0;
248  
249 0 return $self;
250 1 }
251  
252 2 ▶sub DESTROY {
Subroutine DESTROY: called CC=5 SCAR=21.7
253 2 my ($self) = @_;
254  
255 2 ▶ return if ${^GLOBAL_PHASE} eq 'DESTRUCT';
Branch: 1 branch
BranchTrueFalseTotal
if ${^GLOBAL_PHASE} eq "DESTRUCT" 0 2 2
256  
257 # Synchronous cleanup only
258 2 ▶ if (my $handle = $self->{_handle}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $handle = $self->{"_handle"}) 0 2 2
259 0 ▶ if (my $loop = $handle->loop) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $loop = $handle->loop) 0 0 0
260 0 $loop->remove($handle);
261 }
262 }
263  
264 2 ▶ if (my $conn = $self->{conn}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $conn = $self->{"conn"}) 0 2 2
265 0 $conn->release;
266 }
267 }
268  
269 1 1;
270  
271 __END__
↑ Summary Pg/Util.pm »