NAME

Mercury::Pattern::PushPull - Manage a push/pull pattern for a single topic

VERSION

version 0.016

SYNOPSIS

# Connect the pusher
my $push_ua = Mojo::UserAgent->new;
my $push_tx = $ua->websocket( '/push/foo' );

# Connect the puller socket
my $pull_ua = Mojo::UserAgent->new;
my $pull_tx = $ua->websocket( '/pull/foo' );

# Connect the two sockets using push/pull
my $pattern = Mercury::Pattern::PushPull->new;
$pattern->add_pusher( $push_tx );
$pattern->add_puller( $pull_tx );

# Send a message
$pull_tx->on( message => sub {
    my ( $tx, $msg ) = @_;
    print $msg; # Hello, World!
} );
$push_tx->send( 'Hello, World!' );

DESCRIPTION

This pattern connects pushers, which send messages, to pullers, which recieve messages. Each message sent by a pusher will be received by a single puller. This pattern is useful for dealing out jobs to workers.

ATTRIBUTES

pullers

Connected websockets ready to receive messages.

pushers

Connected websockets who will be pushing messages.

current_puller_index

The puller we will use to send the next message from a pusher.

METHODS

add_puller

$pat->add_puller( $tx );

Add a puller to this broker. Pullers are given messages in a round-robin, one at a time, by pushers.

add_pusher

$pat->add_pusher( $tx );

Add a pusher to this broker. Pushers send messages to be processed by pullers.

send_message

$pat->send_message( $msg );

Send the given message to the next puller in line.

remove_puller

$pat->remove_puller( $tx );

Remove a puller from the list. Called automatically when the puller socket is closed.

remove_pusher

$pat->remove_pusher( $tx );

Remove a pusher from the list. Called automatically when the pusher socket is closed.

SEE ALSO

Mercury::Controller::PushPull
Mercury

AUTHOR

Doug Bell <preaction@cpan.org>

COPYRIGHT AND LICENSE

This software is copyright (c) 2018 by Doug Bell.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.