commit 5853216c1b9c070a90ec695453707c7c49b093c4 (patch)
parent 0b9144ba7c3b78c6f1b7bd1ac84db9529b03e776
Author: Alex Karle <alex@karle.co>
Date: Thu, 7 May 2020 00:10:41 -0400
Euchre::Host: Add recurring table pruning action
To prevent memory leaks, we want to prune tables with no players over
time. The easiest way to do this is just to periodically check. A future
commit will make this process more proactive.
Diffstat:
3 files changed, 22 insertions(+), 0 deletions(-)
diff --git a/gloat.pl b/gloat.pl
@@ -5,6 +5,7 @@
# ~ Andy Karle
use Mojolicious::Lite;
use Mojo::JSON qw(decode_json);
+use Mojo::IOLoop;
use FindBin;
use lib "$FindBin::RealBin/lib";
@@ -46,4 +47,7 @@ websocket '/play' => sub {
});
};
+my $cleanup_time = $ENV{DEBUG} ? 1 : 300;
+Mojo::IOLoop->recurring($cleanup_time => \&prune_tables);
+
app->start;
diff --git a/lib/Euchre/Dealer.pm b/lib/Euchre/Dealer.pm
@@ -268,4 +268,11 @@ sub player_names {
return $seated;
}
+# Used to decide when to cleanup
+sub is_active {
+ my ($self) = @_;
+ my $n = scalar keys %{$self->players};
+ return $n != 0;
+}
+
1;
diff --git a/lib/Euchre/Host.pm b/lib/Euchre/Host.pm
@@ -15,6 +15,7 @@ our @EXPORT = qw(
handle_msg
register_player
gloaters_never_win
+ prune_tables
stats
);
@@ -203,4 +204,14 @@ sub require_keys {
return 1;
}
+# Prune empty tables, to be called in a IOLoop
+sub prune_tables {
+ for my $k (keys %DEALERS) {
+ if (!$DEALERS{$k}->is_active) {
+ print "Deleting inactive table " . $DEALERS{$k}->id . "\n";
+ delete $DEALERS{$k};
+ }
+ }
+}
+
1;