++ed by:
SALVA

1 PAUSE user

Andrey Kuzmin
and 1 contributors

NAME

AnyEvent::Pg::Pool::Multiserver - Asyncronious multiserver requests to Postgresql with AnyEvent::Pg

SYNOPSIS

  my $servers = [
    {
      id   => 1,
      name => 'remote 1',
      conn => 'host=remote1 port=5432 dbname=mydb user=myuser password=mypass',
    },
    {
      id   => 2,
      name => 'remote 2',
      conn => 'host=remote2 port=5432 dbname=mydb user=myuser password=mypass',
    },
  ];
  my $pool = AnyEvent::Pg::Pool::Multiserver->new( servers => $servers, local => 1 );

  # multi-server request

  $pool->selectall_arrayref(
    query  => 'SELECT val FROM ( SELECT 1 AS val ) tmp WHERE tmp.val = $1;',
    args   => [ 1 ],
    cb     => sub {
      my $results = shift;
      my $errors  = shift;

      if ( $errors ) {
        foreach my $srv ( @$errors ) {
          say "err $srv->{error} with $srv->{server_name} $srv->{server_id}";
        }
      }

      if ( $results ) {
        foreach my $val ( @$results ) {
          say "server_id=$val->{_server_id} value=$val->{val}";
        }
      }
    },
  );

  # single-server request

  $pool->selectall_arrayref(
    query     => 'SELECT val FROM ( SELECT 1 AS val ) tmp WHERE tmp.val = $1;',
    args      => [ 1 ],
    server_id => 1,
    cb        => sub { ... },
  );

  # multi-server request with sub-callbacks to some data manipulation
  # and may be to make another request to current server

  # main request | server_1 select -> ... select end -> cb_server call -> subrequests to current server | wait both   | global callback
  #              | server_2 select -> ... select end -> cb_server call -> subrequests to current server | subrequests |

  $pool->selectall_arrayref(
    query  => 'SELECT val FROM ( SELECT 1 AS val ) tmp WHERE tmp.val = $1;',
    args   => [ 1 ],
    cb     => sub { ... },
    cb_server => sub {
      my $params = { @_ };

      my $result_of_main_request = $params->{result};

      # Now we can do some sub-request to current server

      # And MUST call cb
      $params->{cb}->();
    },
  );

  # single-server request to select row in arrayref

  $pool->selectrow_array(
    query     => 'SELECT val FROM ( SELECT 1 AS val ) tmp WHERE tmp.val = $1;',
    args      => [ 1 ],
    server_id => 1,
    cb        => sub {
      my $result = shift;
      my $error  = shift;

      if ( $error ) {
        say "err $error->{error} with $error->{server_name} $error->{server_id}";
      }

      if ( $result ) {
        say "server_id=$result->[ 0 ] value=$result->[ 1 ]";
      }
    },
  );

  # single-server request to select row in hashref

  $pool->selectrow_hashref(
    query     => 'SELECT val FROM ( SELECT 1 AS val ) tmp WHERE tmp.val = $1;',
    args      => [ 1 ],
    server_id => 1,
    cb        => sub {
      my $result = shift;
      my $error  = shift;

      if ( $error ) {
        say "err $error->{error} with $error->{server_name} $error->{server_id}";
      }

      if ( $result ) {
        say "server_id=$result->{_server_id} value=$result->{val}";
      }
    },
  );

  # single-server request to do something

  $pool->do(
    query     => 'UPDATE table SET column = 1 WHERE id = $1;',
    args      => [ 1 ],
    server_id => 1,
    cb        => sub {
      my $result = shift;
      my $error  = shift;

      if ( $error ) {
        say "err $error->{error} with $error->{server_name} $error->{server_id}";
      }

      if ( $result ) {
        say "server_id=$result->[ 0 ] updated=$result->[ 1 ]";
      }
    },
  );

  # local-server request to do something

  $pool->do(
    query     => 'UPDATE table SET column = 1 WHERE id = $1;',
    args      => [ 1 ],
    server_id => $pool->local(),
    cb        => sub { ... },
  );

DESCRIPTION

selectall_arrayref

query and args are the same, that in AnyEvent::Pg

Required: query, cb Optional: args, cb_server, server_id

selectrow_array

query and args are the same, that in AnyEvent::Pg

Required: query, server_id, cb Optional: args, cb_server

selectrow_hashref

query and args are the same, that in AnyEvent::Pg

Required: query, server_id, cb Optional: args, cb_server

do

query and args are the same, that in AnyEvent::Pg

Required: query, server_id, cb Optional: args, cb_server

SOURCE AVAILABILITY

The source code for this module is available at Github

AUTHOR

Andrey Kuzmin, <kak-tus@mail.ru>

COPYRIGHT AND LICENSE

Copyright (C) 2014 by Andrey Kuzmin

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