mojolicious/mojo-pg

max_connections can't work on non-blocking search

darrenli912 opened this issue · 2 comments

Steps to reproduce the behavior

  1. My postgresql db max_connections is 100;
  2. I set my hypnotoad with 4 workers ;
  3. My Mojolicious pg setting:
    $self->helper(
    pg => sub {
    state $pg = Mojo::Pg->new('postgresql://apple@/devel');
    $pg->max_connections(12);
    }
    );
    From the above settings, the total max connection should be 12*4=48 from mojo::pg.

My 4 non-blocking search in my controller model:
my $base_info = $self->pg->db->query_p(...);
my $product_content = $self->pg->db->query_p(...);
my $photos = $self->pg->db->query_p(...);
my $similars = $self->pg->db->query_p(...);

Mojo::Promise->all( $base_info, $product_content, $photos, $similars )
  ->then(
    sub {
        my ( $base_info, $product_content, $photos, $similars ) = @_;

.......
}
);

Now I test with ab:
ab -n 4000 -c 100 'http://127.0.0.1:3000/url'

Expected behavior

The max db connection should be 48. No fail connection.

Actual behavior

Get the connection error:
FATAL: remaining connection slots are reserved for non-replication superuser connections at /usr/local/share/perl/5.26.1/Mojo/Pg.pm line 91.

The connections are larger than 100 during my ab test. Many test are failed because of failed connections from mojo::pg.

The connection will get back to 48, after the ab test finished.

Got it. Thank you Grinnz.