Skip to content

Commit

Permalink
check train/trip id when updating associated data
Browse files Browse the repository at this point in the history
this avoid race conditions when a user changes their checkin between the time
of request (old trip id) and the time of data update (new trip id). These
could lead to funny effects such as buses suddenly having S-Bahn carriage
formations.
  • Loading branch information
derf committed May 24, 2024
1 parent f7a9213 commit 5a2285b
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 34 deletions.
43 changes: 26 additions & 17 deletions lib/Travelynx.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1241,17 +1241,19 @@ sub startup {
return;
}

my $route = $in_transit->{route};
my $route = $in_transit->{route};
my $train_id = $in_transit->{train_id};

# TODO get_tripid_p is only needed on the first call, afterwards the tripid is known.
$self->hafas->get_tripid_p( train => $train )->then(
sub {
my ($trip_id) = @_;

$self->in_transit->update_data(
uid => $uid,
db => $db,
data => { trip_id => $trip_id }
uid => $uid,
db => $db,
data => { trip_id => $trip_id },
train_id => $train_id,
);

return $self->hafas->get_route_p(
Expand Down Expand Up @@ -1340,6 +1342,7 @@ sub startup {
$train->qos_messages
],
him_messages => \@messages,
train_id => $train_id,
);

if ($polyline) {
Expand All @@ -1348,6 +1351,7 @@ sub startup {
db => $db,
polyline => $polyline,
old_id => $in_transit->{polyline_id},
train_id => $train_id,
);
}

Expand Down Expand Up @@ -1421,24 +1425,27 @@ sub startup {
}
}
$self->in_transit->update_data(
uid => $uid,
db => $db,
data => $data
uid => $uid,
db => $db,
data => $data,
train_id => $train_id,
);
$self->in_transit->update_user_data(
uid => $uid,
db => $db,
user_data => $user_data
user_data => $user_data,
train_id => $train_id,
);
}
elsif ( not $is_departure
and not exists $wagonorder->{error} )
{
$data->{wagonorder_arr} = $wagonorder;
$self->in_transit->update_data(
uid => $uid,
db => $db,
data => $data
uid => $uid,
db => $db,
data => $data,
train_id => $train_id,
);
}
return;
Expand All @@ -1458,9 +1465,10 @@ sub startup {
my $data = { stationinfo_dep => $station_info };

$self->in_transit->update_data(
uid => $uid,
db => $db,
data => $data
uid => $uid,
db => $db,
data => $data,
train_id => $train_id,
);
return;
}
Expand All @@ -1479,9 +1487,10 @@ sub startup {
my $data = { stationinfo_arr => $station_info };

$self->in_transit->update_data(
uid => $uid,
db => $db,
data => $data
uid => $uid,
db => $db,
data => $data,
train_id => $train_id,
);
return;
}
Expand Down
48 changes: 31 additions & 17 deletions lib/Travelynx/Model/InTransit.pm
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,8 @@ sub set_polyline {
$self->set_polyline_id(
uid => $uid,
db => $db,
polyline_id => $polyline_id
polyline_id => $polyline_id,
train_id => $opt{train_id},
);
}

Expand All @@ -590,11 +591,13 @@ sub set_polyline_id {
my $db = $opt{db} // $self->{pg}->db;
my $polyline_id = $opt{polyline_id};

$db->update(
'in_transit',
{ polyline_id => $polyline_id },
{ user_id => $uid }
);
my %where = ( user_id => $uid );

if ( $opt{train_id} ) {
$where{train_id} = $opt{train_id};
}

$db->update( 'in_transit', { polyline_id => $polyline_id }, \%where );
}

sub set_route_data {
Expand All @@ -607,6 +610,12 @@ sub set_route_data {
my $qos_msg = $opt{qos_messages};
my $him_msg = $opt{him_messages};

my %where = ( user_id => $uid );

if ( $opt{train_id} ) {
$where{train_id} = $opt{train_id};
}

my $res_h = $db->select( 'in_transit', ['data'], { user_id => $uid } )
->expand->hash;

Expand All @@ -623,7 +632,7 @@ sub set_route_data {
route => JSON->new->encode($route),
data => JSON->new->encode($data)
},
{ user_id => $uid }
\%where
);
}

Expand Down Expand Up @@ -850,6 +859,12 @@ sub update_data {
my $db = $opt{db} // $self->{pg}->db;
my $new_data = $opt{data} // {};

my %where = ( user_id => $uid );

if ( $opt{train_id} ) {
$where{train_id} = $opt{train_id};
}

my $res_h = $db->select( 'in_transit', ['data'], { user_id => $uid } )
->expand->hash;

Expand All @@ -859,11 +874,7 @@ sub update_data {
$data->{$k} = $v;
}

$db->update(
'in_transit',
{ data => JSON->new->encode($data) },
{ user_id => $uid }
);
$db->update( 'in_transit', { data => JSON->new->encode($data) }, \%where );
}

sub update_user_data {
Expand All @@ -873,6 +884,12 @@ sub update_user_data {
my $db = $opt{db} // $self->{pg}->db;
my $new_data = $opt{user_data} // {};

my %where = ( user_id => $uid );

if ( $opt{train_id} ) {
$where{train_id} = $opt{train_id};
}

my $res_h = $db->select( 'in_transit', ['user_data'], { user_id => $uid } )
->expand->hash;

Expand All @@ -882,11 +899,8 @@ sub update_user_data {
$data->{$k} = $v;
}

$db->update(
'in_transit',
{ user_data => JSON->new->encode($data) },
{ user_id => $uid }
);
$db->update( 'in_transit',
{ user_data => JSON->new->encode($data) }, \%where );
}

sub update_visibility {
Expand Down

0 comments on commit 5a2285b

Please sign in to comment.