blib/lib/HTTP/Promise/Pool.pm untested

filter: Statementstmt 0.0% 0 / 63 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 17 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 117 CC 5 SCAR 14.3
CC
5
Cov
0%
host_port
12.0
steal
6.0
FREEZE
6.0
CRAP 4.2
1 ##----------------------------------------------------------------------------
2 ## Asynchronous HTTP Request and Promise - ~/lib/HTTP/Promise/Pool.pm
3 ## Version v0.2.0
4 ## Copyright(c) 2022 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2022/03/27
7 ## Modified 2023/09/08
8 ## All rights reserved.
9 ##
10 ##
11 ## This program is free software; you can redistribute it and/or modify it
12 ## under the same terms as Perl itself.
13 ##----------------------------------------------------------------------------
14 package HTTP::Promise::Pool;
15 BEGIN
16 {
17 use strict;
18 use warnings;
19 warnings::register_categories( 'HTTP::Promise' );
20 use parent qw( Module::Generic );
21 our $VERSION = 'v0.2.0';
22 };
23  
24 use strict;
25 use warnings;
26  
27 sub init
28 {
29 my $self = shift( @_ );
30 $self->{host} = undef;
31 $self->{port} = undef;
32 $self->{sock} = undef;
33 $self->{_init_strict_use_sub} = 1;
34 $self->SUPER::init( @_ ) || return( $self->pass_error );
35 return( $self );
36 }
37  
38 sub host { return( shift->_set_get_scalar_as_object( 'host', @_ ) ); }
39  
40 sub host_port
41 {
42 my $self = shift( @_ );
43 my $host = $self->host || '';
44 my $port = $self->port || '';
45 return( join( ':', $host, $port ) ) if( length( $host ) && length( $port ) );
46 return( $host ) if( length( $host ) );
47 return;
48 }
49  
50 sub port { return( shift->_set_get_number( 'port', @_ ) ); }
51  
52 sub push
53 {
54 my $self = shift( @_ );
55 my( $host, $port, $sock ) = @_;
56 $self->host( $host );
57 $self->port( $port );
58 $self->sock( $sock );
59 return( $self );
60 }
61  
62 sub reset
63 {
64 my $self = shift( @_ );
65 $self->{host} = undef;
66 $self->{port} = undef;
67 return( $self );
68 }
69  
70 sub sock { return( shift->_set_get_scalar( 'sock', @_ ) ); }
71  
72 sub steal
73 {
74 my $self = shift( @_ );
75 my( $host, $port ) = @_;
76 my $host_port = $self->host_port;
77 if( defined( $host_port ) &&
78 $host_port eq "${host}:${port}" )
79 {
80 my $sock = $self->sock;
81 $self->reset;
82 return( $sock );
83 }
84 else
85 {
86 return;
87 }
88 }
89  
90 sub FREEZE
91 {
92 my $self = CORE::shift( @_ );
93 my $serialiser = CORE::shift( @_ ) // '';
94 my $class = CORE::ref( $self );
95 my %hash = %$self;
96 CORE::delete( @hash{ qw( sock ) } );
97 # Return an array reference rather than a list so this works with Sereal and CBOR
98 CORE::return( [$class, \%hash] ) if( $serialiser eq 'Sereal' || $serialiser eq 'CBOR' );
99 # But Storable want a list with the first element being the serialised element
100 CORE::return( $class, \%hash );
101 }
102  
103 sub STORABLE_freeze { CORE::return( CORE::shift->FREEZE( @_ ) ); }
104  
105 sub STORABLE_thaw { CORE::return( CORE::shift->THAW( @_ ) ); }
106  
107 # NOTE: sub THAW is inherited
108  
109 1;
110 # NOTE: POD
111 __END__
112  
113 =encoding utf-8
114  
115 =head1 NAME
116  
117 HTTP::Promise::Pool - HTTP Connections Cache
118  
119 =head1 SYNOPSIS
120  
121 use HTTP::Promise::Pool;
122 my $this = HTTP::Promise::Pool->new ||
123 die( HTTP::Promise::Pool->error, "\n" );
124  
125 =head1 VERSION
126  
127 v0.2.0
128  
129 =head1 DESCRIPTION
130  
131 This modules managed a cache of HTTP connections.
132  
133 =head1 METHODS
134  
135 =head2 host
136  
137 Sets or gets the host. Returns a L<scalar object|Module::Generic::Scalar>
138  
139 =head2 host_port
140  
141 Read-only. Returns the host and port separated by a semi colon if a port is defined, otherwise returns just the host.
142  
143 =head2 port
144  
145 Sets or gets the port. Returns a L<number object|Module::Generic::Number>
146  
147 =head2 push
148  
149 Add the C<host>, C<port> and C<socket> provided to the stack.
150  
151 =head2 reset
152  
153 Resets the C<host> and C<port>
154  
155 =head2 sock
156  
157 Sets or gets the socket.
158  
159 =head2 steal
160  
161 Provided with an C<host> and C<port>, this checks if those matches the current values, and returns the current socket after resetting the object.
162  
163 =head1 THREAD-SAFETY
164  
165 This module is thread-safe for all operations, as it operates on per-object state and uses thread-safe external libraries.
166  
167 =head1 AUTHOR
168  
169 Jacques Deguest E<lt>F<jack@deguest.jp>E<gt>
170  
171 =head1 SEE ALSO
172  
173 L<HTTP::Promise>, L<HTTP::Promise::Request>, L<HTTP::Promise::Response>, L<HTTP::Promise::Message>, L<HTTP::Promise::Entity>, L<HTTP::Promise::Headers>, L<HTTP::Promise::Body>, L<HTTP::Promise::Body::Form>, L<HTTP::Promise::Body::Form::Data>, L<HTTP::Promise::Body::Form::Field>, L<HTTP::Promise::Status>, L<HTTP::Promise::MIME>, L<HTTP::Promise::Parser>, L<HTTP::Promise::IO>, L<HTTP::Promise::Stream>, L<HTTP::Promise::Exception>
174  
175 =head1 COPYRIGHT & LICENSE
176  
177 Copyright(c) 2022 DEGUEST Pte. Ltd.
178  
179 All rights reserved.
180  
181 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
182  
183 =cut