diff options
author | Daniel Friesel <derf@finalrewind.org> | 2019-07-20 20:58:28 +0200 |
---|---|---|
committer | Daniel Friesel <derf@finalrewind.org> | 2019-07-20 20:58:28 +0200 |
commit | 90dd96e08fcac4c1d4e8761a108553d3e039f52a (patch) | |
tree | 3a12cda6b371db5621838d95e0f08c9bbb835b45 /lib | |
parent | 7c7b5e9f9536f70c92d66c4055090b46e26c4c11 (diff) |
Handle race condition when several workers are updating the same history entry1.8.5
Diffstat (limited to 'lib')
-rwxr-xr-x | lib/Travelynx.pm | 32 |
1 files changed, 25 insertions, 7 deletions
diff --git a/lib/Travelynx.pm b/lib/Travelynx.pm index 3ef7011..2bc151d 100755 --- a/lib/Travelynx.pm +++ b/lib/Travelynx.pm @@ -1485,15 +1485,33 @@ sub startup { ); my $stats = $self->compute_journey_stats(@journeys); - $self->pg->db->insert( - 'journey_stats', + eval { + $self->pg->db->insert( + 'journey_stats', + { + user_id => $uid, + year => $year, + month => $month, + data => JSON->new->encode($stats), + } + ); + }; + if ( my $err = $@ ) { + if ( $err =~ m{duplicate key value violates unique constraint} ) { - user_id => $uid, - year => $year, - month => $month, - data => JSON->new->encode($stats), + # When a user opens the same history page several times in + # short succession, there is a race condition where several + # Mojolicious workers execute this helper, notice that there is + # no up-to-date history, compute it, and insert it using the + # statement above. This will lead to a uniqueness violation + # in each successive insert. However, this is harmless, and + # thus ignored. } - ); + else { + # Otherwise we probably have a problem. + die($@); + } + } return $stats; } |