Authentication Problems

93 views
Skip to first unread message

graham.da...@gmail.com

unread,
May 27, 2015, 6:38:35 AM5/27/15
to mojol...@googlegroups.com
Hello,

I have created a mojolicious app which I am running on hypnotoad. 
I have implemented a simple login system using Mojolicious::Plugin::Authentication (exactly as described in the cpan documentation for this plugin).
Everything works perfectly users can log in and out succesfully.
The problem is that if the app is left up overnight the next day no one can log in all of the logins are refused.
If I restart hypnotoad everything starts working perfectly for another day.

Is there something simple I'm missing here?
Is Mojolicious::Plugin::Authentication a good choice?

Thanks,
Graham.

aleksander.groschev

unread,
May 27, 2015, 8:16:21 AM5/27/15
to mojol...@googlegroups.com
В Среда, 27 май. 2015 в 1:38 , graham.da...@gmail.com написал:

Is there something simple I'm missing here?

Yes. Mojolicious::Plugin::Authentication->validate_user using your code. I'm sure that coderef in 'validate_user' contain usage of connection to DB/LDAP/etc.
You try to check the connection to the remote service with which validation occurs. Try restore connection If it terminated.

graham.da...@gmail.com

unread,
May 27, 2015, 8:46:52 AM5/27/15
to mojol...@googlegroups.com
Thanks that sounds like a good  possibility. I've added a considerable amount of debug logging and should be able to confirm if the database connection is the issue by tomorrow morning.  I'm using Mojolicious::Plugin::Database and had hoped that plugin would manage the database connection and keep it alive will do some further reading on that. 

Ben van Staveren

unread,
May 27, 2015, 9:44:22 AM5/27/15
to mojol...@googlegroups.com

the database plugin won't do anything fancy, it was a proof of concept to show how to handle DBI in Mojolicious

--
You received this message because you are subscribed to the Google Groups "Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mojolicious...@googlegroups.com.
To post to this group, send email to mojol...@googlegroups.com.
Visit this group at http://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.

sri

unread,
May 27, 2015, 10:14:17 AM5/27/15
to mojol...@googlegroups.com, graham.da...@gmail.com

...and had hoped that plugin would manage the database connection and keep it alive will do some further reading on that. 

Mojo::Pg does that for you.

--
sebastian 

graham.da...@gmail.com

unread,
May 28, 2015, 6:42:02 AM5/28/15
to mojol...@googlegroups.com

Thanks everyone,

It looks like the database connection being dropped was the problem.
Im using a mysql database so I think Mojo::mysql might be a good option to go with as it handles dropped database connections.

One thing that concerns me is that Mojo::mysql does not use the standard 'fetchrow_hashref' and 'prepare' from DBI this means a lot of changes to my code it will not be a drop in replacement.

Im concerned that there is no way to prepare statements is Mojo::mysql a commonly used module is it a god choice to go with? 

aleksander.groschev

unread,
May 28, 2015, 8:02:12 AM5/28/15
to mojol...@googlegroups.com
В Четверг, 28 май. 2015 в 1:42 , graham.da...@gmail.com написал:
Im concerned that there is no way to prepare statements is Mojo::mysql a commonly used module is it a god choice to go with? 
You can use 'mysql_auto_reconnect' option when creating database handler. But don't pass it to `$dbh = DBI->connect(...)`, after connect do `$dbh->{mysql_auto_reconnect} = 1`. This is strange behavior and  maybe magic, but it works so only.

sri

unread,
May 28, 2015, 11:02:18 AM5/28/15
to mojol...@googlegroups.com, graham.da...@gmail.com
Im concerned that there is no way to prepare statements is Mojo::mysql a commonly used module is it a god choice to go with?

I'm not familiar with Mojo::mysql internals, but it should be based on Mojo::Pg, which handles all kinds of performance optimizations (like prepare statements) for you automatically.

--
sebastian

graham.da...@gmail.com

unread,
May 28, 2015, 11:39:10 AM5/28/15
to mojol...@googlegroups.com
Thanks again:

sri,

using DBI i can do the following:

# Insert a two rows
  my $qry = $db->prepare('insert into names (name) values (?);');
  $qry->execute('Sara');
  $qry->execute('Stefan');

to do the same with Mojo::Pg or Mojo::mysql I need to do the following:

# Insert a two rows
  my $db = $pg->db;
  $db->query('insert into names (name) values (?)', 'Sara');
  $db->query('insert into names (name) values (?)', 'Stefan');

My obesrvations are:
1. The DBI version clearly only prepares the statement once then can execute the statement as many times as is desired.
2. I don't know how many times the Mojo::Pg version is preparing the statement so it may be less efficient than DBI?
3. The DBI version DRY and the Mojo::Pg version isn't, the sql statement is repeated (i guess this could be overcome by storing the query string in a scalar).

What do you think?






sri

unread,
May 28, 2015, 12:19:26 PM5/28/15
to mojol...@googlegroups.com, graham.da...@gmail.com
2. I don't know how many times the Mojo::Pg version is preparing the statement so it may be less efficient than DBI?

Mojo::Pg should be quite a bit more efficient, because it does a lot more optimizations behind the scenes. It not only prepares and caches statement handles, it also stores statement handles with the database handle. So you automatically get great performance and reconnects without doing any additional work.

--
sebastian

sri

unread,
May 28, 2015, 12:27:00 PM5/28/15
to mojol...@googlegroups.com, kra...@googlemail.com, graham.da...@gmail.com
It not only prepares and caches statement handles, it also stores statement handles with the database handle.

Or to be more precise, it currently uses DBI::prepare_cached with $is_active=3 and a custom cache for database handles (to support async queries better).


I've done quite a bit of profiling, and this turned out to be the best performing solution.

--
sebastian 
Reply all
Reply to author
Forward
0 new messages