| 1 | package Mercury::Pattern::PushPull; | ||||||||||||||||||||||||||||
| 2 | 6 | our $VERSION = '0.016'; | |||||||||||||||||||||||||||
| 3 | # ABSTRACT: Manage a push/pull pattern for a single topic | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | #pod =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 6 | #pod | ||||||||||||||||||||||||||||
| 7 | #pod # Connect the pusher | ||||||||||||||||||||||||||||
| 8 | #pod my $push_ua = Mojo::UserAgent->new; | ||||||||||||||||||||||||||||
| 9 | #pod my $push_tx = $ua->websocket( '/push/foo' ); | ||||||||||||||||||||||||||||
| 10 | #pod | ||||||||||||||||||||||||||||
| 11 | #pod # Connect the puller socket | ||||||||||||||||||||||||||||
| 12 | #pod my $pull_ua = Mojo::UserAgent->new; | ||||||||||||||||||||||||||||
| 13 | #pod my $pull_tx = $ua->websocket( '/pull/foo' ); | ||||||||||||||||||||||||||||
| 14 | #pod | ||||||||||||||||||||||||||||
| 15 | #pod # Connect the two sockets using push/pull | ||||||||||||||||||||||||||||
| 16 | #pod my $pattern = Mercury::Pattern::PushPull->new; | ||||||||||||||||||||||||||||
| 17 | #pod $pattern->add_pusher( $push_tx ); | ||||||||||||||||||||||||||||
| 18 | #pod $pattern->add_puller( $pull_tx ); | ||||||||||||||||||||||||||||
| 19 | #pod | ||||||||||||||||||||||||||||
| 20 | #pod # Send a message | ||||||||||||||||||||||||||||
| 21 | #pod $pull_tx->on( message => sub { | ||||||||||||||||||||||||||||
| 22 | #pod my ( $tx, $msg ) = @_; | ||||||||||||||||||||||||||||
| 23 | #pod print $msg; # Hello, World! | ||||||||||||||||||||||||||||
| 24 | #pod } ); | ||||||||||||||||||||||||||||
| 25 | #pod $push_tx->send( 'Hello, World!' ); | ||||||||||||||||||||||||||||
| 26 | #pod | ||||||||||||||||||||||||||||
| 27 | #pod =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 28 | #pod | ||||||||||||||||||||||||||||
| 29 | #pod This pattern connects pushers, which send messages, to pullers, which | ||||||||||||||||||||||||||||
| 30 | #pod recieve messages. Each message sent by a pusher will be received by | ||||||||||||||||||||||||||||
| 31 | #pod a single puller. This pattern is useful for dealing out jobs to workers. | ||||||||||||||||||||||||||||
| 32 | #pod | ||||||||||||||||||||||||||||
| 33 | #pod =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 34 | #pod | ||||||||||||||||||||||||||||
| 35 | #pod =over | ||||||||||||||||||||||||||||
| 36 | #pod | ||||||||||||||||||||||||||||
| 37 | #pod =item L<Mercury::Controller::PushPull> | ||||||||||||||||||||||||||||
| 38 | #pod | ||||||||||||||||||||||||||||
| 39 | #pod =item L<Mercury> | ||||||||||||||||||||||||||||
| 40 | #pod | ||||||||||||||||||||||||||||
| 41 | #pod =back | ||||||||||||||||||||||||||||
| 42 | #pod | ||||||||||||||||||||||||||||
| 43 | #pod =cut | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 6 | ▶ | use Mojo::Base '-base'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | #pod =attr pullers | ||||||||||||||||||||||||||||
| 48 | #pod | ||||||||||||||||||||||||||||
| 49 | #pod Connected websockets ready to receive messages. | ||||||||||||||||||||||||||||
| 50 | #pod | ||||||||||||||||||||||||||||
| 51 | #pod =cut | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 3 | ▶ | has pullers => sub { [] }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | #pod =attr pushers | ||||||||||||||||||||||||||||
| 56 | #pod | ||||||||||||||||||||||||||||
| 57 | #pod Connected websockets who will be pushing messages. | ||||||||||||||||||||||||||||
| 58 | #pod | ||||||||||||||||||||||||||||
| 59 | #pod =cut | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 2 | ▶ | has pushers => sub { [] }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | #pod =attr current_puller_index | ||||||||||||||||||||||||||||
| 64 | #pod | ||||||||||||||||||||||||||||
| 65 | #pod The puller we will use to send the next message from a pusher. | ||||||||||||||||||||||||||||
| 66 | #pod | ||||||||||||||||||||||||||||
| 67 | #pod =cut | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 3 | ▶ | has current_puller_index => sub { 0 }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | #pod =method add_puller | ||||||||||||||||||||||||||||
| 72 | #pod | ||||||||||||||||||||||||||||
| 73 | #pod $pat->add_puller( $tx ); | ||||||||||||||||||||||||||||
| 74 | #pod | ||||||||||||||||||||||||||||
| 75 | #pod Add a puller to this broker. Pullers are given messages in a round-robin, one | ||||||||||||||||||||||||||||
| 76 | #pod at a time, by pushers. | ||||||||||||||||||||||||||||
| 77 | #pod | ||||||||||||||||||||||||||||
| 78 | #pod =cut | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 6 | ▶ | sub add_puller { | ||||||||||||||||||||||||||
|
Subroutine
add_puller: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 81 | 6 | my ( $self, $tx ) = @_; | |||||||||||||||||||||||||||
| 82 | 5 | ▶ | $tx->on( finish => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 83 | 5 | my ( $tx ) = @_; | |||||||||||||||||||||||||||
| 84 | 5 | $self->remove_puller( $tx ); | |||||||||||||||||||||||||||
| 85 | 6 | } ); | |||||||||||||||||||||||||||
| 86 | 6 | push @{ $self->pullers }, $tx; | |||||||||||||||||||||||||||
| 87 | 6 | return; | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | #pod =method add_pusher | ||||||||||||||||||||||||||||
| 91 | #pod | ||||||||||||||||||||||||||||
| 92 | #pod $pat->add_pusher( $tx ); | ||||||||||||||||||||||||||||
| 93 | #pod | ||||||||||||||||||||||||||||
| 94 | #pod Add a pusher to this broker. Pushers send messages to be processed by pullers. | ||||||||||||||||||||||||||||
| 95 | #pod | ||||||||||||||||||||||||||||
| 96 | #pod =cut | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 2 | ▶ | sub add_pusher { | ||||||||||||||||||||||||||
|
Subroutine
add_pusher: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 99 | 2 | my ( $self, $tx ) = @_; | |||||||||||||||||||||||||||
| 100 | 7 | ▶ | $tx->on( message => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 101 | 7 | my ( $tx, $msg ) = @_; | |||||||||||||||||||||||||||
| 102 | 7 | $self->send_message( $msg ); | |||||||||||||||||||||||||||
| 103 | 2 | } ); | |||||||||||||||||||||||||||
| 104 | 2 | ▶ | $tx->on( finish => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 105 | 2 | my ( $tx ) = @_; | |||||||||||||||||||||||||||
| 106 | 2 | $self->remove_pusher( $tx ); | |||||||||||||||||||||||||||
| 107 | 2 | } ); | |||||||||||||||||||||||||||
| 108 | 2 | push @{ $self->pushers }, $tx; | |||||||||||||||||||||||||||
| 109 | 2 | return; | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | #pod =method send_message | ||||||||||||||||||||||||||||
| 113 | #pod | ||||||||||||||||||||||||||||
| 114 | #pod $pat->send_message( $msg ); | ||||||||||||||||||||||||||||
| 115 | #pod | ||||||||||||||||||||||||||||
| 116 | #pod Send the given message to the next puller in line. | ||||||||||||||||||||||||||||
| 117 | #pod | ||||||||||||||||||||||||||||
| 118 | #pod =cut | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 8 | ▶ | sub send_message { | ||||||||||||||||||||||||||
|
Subroutine
send_message: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 121 | 8 | my ( $self, $msg ) = @_; | |||||||||||||||||||||||||||
| 122 | 8 | my $i = $self->current_puller_index; | |||||||||||||||||||||||||||
| 123 | 8 | my @pullers = @{ $self->pullers }; | |||||||||||||||||||||||||||
| 124 | 8 | $pullers[ $i ]->send( $msg ); | |||||||||||||||||||||||||||
| 125 | 8 | $self->current_puller_index( ( $i + 1 ) % @pullers ); | |||||||||||||||||||||||||||
| 126 | 8 | return; | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | #pod =method remove_puller | ||||||||||||||||||||||||||||
| 130 | #pod | ||||||||||||||||||||||||||||
| 131 | #pod $pat->remove_puller( $tx ); | ||||||||||||||||||||||||||||
| 132 | #pod | ||||||||||||||||||||||||||||
| 133 | #pod Remove a puller from the list. Called automatically when the puller socket | ||||||||||||||||||||||||||||
| 134 | #pod is closed. | ||||||||||||||||||||||||||||
| 135 | #pod | ||||||||||||||||||||||||||||
| 136 | #pod =cut | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 5 | ▶ | sub remove_puller { | ||||||||||||||||||||||||||
|
Subroutine
remove_puller: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 139 | 5 | my ( $self, $tx ) = @_; | |||||||||||||||||||||||||||
| 140 | 5 | my @pullers = @{ $self->pullers }; | |||||||||||||||||||||||||||
| 141 | 5 | for my $i ( 0.. $#pullers ) { | |||||||||||||||||||||||||||
| 142 | 6 | ▶ | if ( $pullers[$i] eq $tx ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | 5 | splice @{ $self->pullers }, $i, 1; | |||||||||||||||||||||||||||
| 144 | 5 | my $current_puller_index = $self->current_puller_index; | |||||||||||||||||||||||||||
| 145 | 5 | ▶ | if ( $i > 0 && $current_puller_index >= $i ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$i > 0 and $current_puller_index >= $i
MC/DC50%1/2
$i > 0 and $current_puller_index >= $i
$i > 0
$current_puller_index >= $i
Truth table66%2/3
A and B
A= $i > 0B= $current_puller_index >= $i
| |||||||||||||||||||||||||||||
| 146 | 1 | $self->current_puller_index( $current_puller_index - 1 ); | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | 5 | return; | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | #pod =method remove_pusher | ||||||||||||||||||||||||||||
| 154 | #pod | ||||||||||||||||||||||||||||
| 155 | #pod $pat->remove_pusher( $tx ); | ||||||||||||||||||||||||||||
| 156 | #pod | ||||||||||||||||||||||||||||
| 157 | #pod Remove a pusher from the list. Called automatically when the pusher socket | ||||||||||||||||||||||||||||
| 158 | #pod is closed. | ||||||||||||||||||||||||||||
| 159 | #pod | ||||||||||||||||||||||||||||
| 160 | #pod =cut | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 2 | ▶ | sub remove_pusher { | ||||||||||||||||||||||||||
|
Subroutine
remove_pusher: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 163 | 2 | my ( $self, $tx ) = @_; | |||||||||||||||||||||||||||
| 164 | 2 | my @pushers = @{ $self->pushers }; | |||||||||||||||||||||||||||
| 165 | 2 | for my $i ( 0.. $#pushers ) { | |||||||||||||||||||||||||||
| 166 | 2 | ▶ | if ( $pushers[$i] eq $tx ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 167 | 2 | splice @pushers, $i, 1; | |||||||||||||||||||||||||||
| 168 | 2 | return; | |||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 6 | 1; | |||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | __END__ | ||||||||||||||||||||||||||||