| 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 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 2 | ▶ | warnings::register_categories( 'HTTP::Promise' ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 2 | ▶ | use parent qw( Module::Generic ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 2 | our $VERSION = 'v0.2.0'; | |||||||||||||||||||||||||||
| 22 | }; | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | sub init | ||||||||||||||||||||||||||||
| 28 | { | ||||||||||||||||||||||||||||
| 29 | 2 | ▶ | my $self = shift( @_ ); | ||||||||||||||||||||||||||
|
Subroutine
init: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 30 | 2 | $self->{host} = undef; | |||||||||||||||||||||||||||
| 31 | 2 | $self->{port} = undef; | |||||||||||||||||||||||||||
| 32 | 2 | $self->{sock} = undef; | |||||||||||||||||||||||||||
| 33 | 2 | $self->{_init_strict_use_sub} = 1; | |||||||||||||||||||||||||||
| 34 | 2 | ▶ | $self->SUPER::init( @_ ) || return( $self->pass_error ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | 2 | return( $self ); | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 0 | ▶ | sub host { return( shift->_set_get_scalar_as_object( 'host', @_ ) ); } | ||||||||||||||||||||||||||
|
Subroutine
host: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | sub host_port | ||||||||||||||||||||||||||||
| 41 | { | ||||||||||||||||||||||||||||
| 42 | 0 | ▶ | my $self = shift( @_ ); | ||||||||||||||||||||||||||
|
Subroutine
host_port: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||
| 43 | 0 | ▶ | my $host = $self->host || ''; | ||||||||||||||||||||||||||
|
Condition0%0/2
$self->host || ''
MC/DC0%0/1
$self->host || ''
$self->host
Truth table0%0/2
A
A= $self->host
| |||||||||||||||||||||||||||||
| 44 | 0 | ▶ | my $port = $self->port || ''; | ||||||||||||||||||||||||||
|
Condition0%0/2
$self->port || ''
MC/DC0%0/1
$self->port || ''
$self->port
Truth table0%0/2
A
A= $self->port
| |||||||||||||||||||||||||||||
| 45 | 0 | ▶ | return( join( ':', $host, $port ) ) if( length( $host ) && length( $port ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
length $host and length $port
MC/DC0%0/2
length $host and length $port
length $host
length $port
Truth table0%0/3
A and B
A= length $hostB= length $port
| |||||||||||||||||||||||||||||
| 46 | 0 | ▶ | return( $host ) if( length( $host ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | 0 | return; | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 0 | ▶ | sub port { return( shift->_set_get_number( 'port', @_ ) ); } | ||||||||||||||||||||||||||
|
Subroutine
port: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | sub push | ||||||||||||||||||||||||||||
| 53 | { | ||||||||||||||||||||||||||||
| 54 | 0 | ▶ | my $self = shift( @_ ); | ||||||||||||||||||||||||||
|
Subroutine
push: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 55 | 0 | my( $host, $port, $sock ) = @_; | |||||||||||||||||||||||||||
| 56 | 0 | $self->host( $host ); | |||||||||||||||||||||||||||
| 57 | 0 | $self->port( $port ); | |||||||||||||||||||||||||||
| 58 | 0 | $self->sock( $sock ); | |||||||||||||||||||||||||||
| 59 | 0 | return( $self ); | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | sub reset | ||||||||||||||||||||||||||||
| 63 | { | ||||||||||||||||||||||||||||
| 64 | 0 | ▶ | my $self = shift( @_ ); | ||||||||||||||||||||||||||
|
Subroutine
reset: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 65 | 0 | $self->{host} = undef; | |||||||||||||||||||||||||||
| 66 | 0 | $self->{port} = undef; | |||||||||||||||||||||||||||
| 67 | 0 | return( $self ); | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 0 | ▶ | sub sock { return( shift->_set_get_scalar( 'sock', @_ ) ); } | ||||||||||||||||||||||||||
|
Subroutine
sock: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | sub steal | ||||||||||||||||||||||||||||
| 73 | { | ||||||||||||||||||||||||||||
| 74 | 0 | ▶ | my $self = shift( @_ ); | ||||||||||||||||||||||||||
|
Subroutine
steal: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 75 | 0 | my( $host, $port ) = @_; | |||||||||||||||||||||||||||
| 76 | 0 | my $host_port = $self->host_port; | |||||||||||||||||||||||||||
| 77 | 0 | ▶ | if( defined( $host_port ) && | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $host_port and $host_port eq "${host}:$port"
MC/DC0%0/2
defined $host_port and $host_port eq "${host}:$port"
defined $host_port
$host_port eq "${host}:$port"
Truth table0%0/3
A and B
A= defined $host_portB= $host_port eq "${host}:$port"
| |||||||||||||||||||||||||||||
| 78 | $host_port eq "${host}:${port}" ) | ||||||||||||||||||||||||||||
| 79 | { | ||||||||||||||||||||||||||||
| 80 | 0 | my $sock = $self->sock; | |||||||||||||||||||||||||||
| 81 | 0 | $self->reset; | |||||||||||||||||||||||||||
| 82 | 0 | return( $sock ); | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | else | ||||||||||||||||||||||||||||
| 85 | { | ||||||||||||||||||||||||||||
| 86 | 0 | return; | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | sub FREEZE | ||||||||||||||||||||||||||||
| 91 | { | ||||||||||||||||||||||||||||
| 92 | 2 | ▶ | my $self = CORE::shift( @_ ); | ||||||||||||||||||||||||||
|
Subroutine
FREEZE: called CC=4 SCAR=14.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 93 | 2 | ▶ | my $serialiser = CORE::shift( @_ ) // ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
shift @_ // ""
MC/DC0%0/1
shift @_ // ""
shift @_
Truth table50%1/2
A
A= shift @_
| |||||||||||||||||||||||||||||
| 94 | 2 | my $class = CORE::ref( $self ); | |||||||||||||||||||||||||||
| 95 | 2 | my %hash = %$self; | |||||||||||||||||||||||||||
| 96 | 2 | CORE::delete( @hash{ qw( sock ) } ); | |||||||||||||||||||||||||||
| 97 | # Return an array reference rather than a list so this works with Sereal and CBOR | ||||||||||||||||||||||||||||
| 98 | 2 | ▶ | CORE::return( [$class, \%hash] ) if( $serialiser eq 'Sereal' || $serialiser eq 'CBOR' ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$serialiser eq "Sereal" or $serialiser eq "CBOR"
MC/DC50%1/2
$serialiser eq "Sereal" or $serialiser eq "CBOR"
$serialiser eq "Sereal"
$serialiser eq "CBOR"
Truth table66%2/3
A or B
A= $serialiser eq "Sereal"B= $serialiser eq "CBOR"
| |||||||||||||||||||||||||||||
| 99 | # But Storable want a list with the first element being the serialised element | ||||||||||||||||||||||||||||
| 100 | 1 | CORE::return( $class, \%hash ); | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 1 | ▶ | sub STORABLE_freeze { CORE::return( CORE::shift->FREEZE( @_ ) ); } | ||||||||||||||||||||||||||
|
Subroutine
STORABLE_freeze: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 1 | ▶ | sub STORABLE_thaw { CORE::return( CORE::shift->THAW( @_ ) ); } | ||||||||||||||||||||||||||
|
Subroutine
STORABLE_thaw: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | # NOTE: sub THAW is inherited | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 2 | 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 | ||||||||||||||||||||||||||||