user3654762
user3654762

Reputation: 41

Using sessions with perl Dancer/plack/Starman and multiple workers

I'm running a perl Dancer application using Starman via plack (hopefully that is describing things correctly), and mostly this has been a painless experience. I've just recently been trying to get sessions working (really simple stuff - I just want to store a couple of strings, and I am using session: "Simple"), and I am running into really strange issues when running Starman with multiple workers.

Using the following very simple code (at the bottom) results in the following:

Standalone app: Works fine - counter increments when you click on it.

Starman - 1 worker: Works fine

Starman - 2+ workers: The session appears to exist for approximately 1 second, and is subsequently destroyed - the counter always "expires" after a very very short period of time. It doesn't appear to be a worker-specific session, it just resets to nothing. If you hammer the link more than once a second, it increments normally forever (or for as long as I could be bothered to test it).

Am I doing anything wrong, or is this just not going to work? It isn't terribly critical, but it would be nice to be able to get simple sessions working.

Thanks,

Dave

##
## Code to reproduce via:
## plackup -D -E env -s Starman --workers=3 -p 3000 -a myapp.pl
##

get '/sessiontest' => sub {
    return(&sessiontest());
};

sub sessiontest {
  my $testcounter = session 'testcounter' || 0;
  $testcounter++;
  session 'testcounter' => $testcounter;
  info "SESSION COUNTER($testcounter)";
  my $return = <<EOF;
<html>
<body>
<a href=\"/sessiontest\">$testcounter</a>
</body>
</html>
EOF
  return($return);
}

Upvotes: 0

Views: 518

Answers (2)

w.k
w.k

Reputation: 8386

Like docs say:

This module implements a very simple session backend, holding all session data in memory. This means that sessions are volatile, and no longer exist when the process exits. This module is likely to be most useful for testing purposes.

I think that two different workers don't share session information, because they both have their own copy in memory.

EDIT

Seems I was too enthusiastic with hammering. When I made bigger pause between request, session info vanished, indeed.

Upvotes: 1

user3654762
user3654762

Reputation: 41

Interestingly enough, I just retested using session: "YAML" and it looks like it works fine - it is just Simple that seems to be having issues. I'm going to go ahead and use that for the time being, but I would still be curious to receive an answer if anyone has any insight.

Dave

Upvotes: 0

Related Questions