Skip to content

Commit

Permalink
Extract method for locking the buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardehrenfried committed May 22, 2024
1 parent 2d06fee commit 80c17a6
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class AbstractTimetableSnapshotSource implements TimetableSnapshotProvide
/**
* Lock to indicate that buffer is in use
*/
protected final ReentrantLock bufferLock = new ReentrantLock(true);
private final ReentrantLock bufferLock = new ReentrantLock(true);

/**
* The working copy of the timetable snapshot. Should not be visible to routing threads. Should
Expand Down Expand Up @@ -137,4 +137,15 @@ protected final boolean purgeExpiredData() {
protected final LocalDate localDateNow() {
return localDateNow.get();
}

protected final void withLock(Runnable action) {
bufferLock.lock();

try {
action.run();
} finally {
// Always release lock
bufferLock.unlock();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,13 @@ public class SiriTimetableSnapshotSource extends AbstractTimetableSnapshotSource

private final TransitService transitService;

/** Should expired real-time data be purged from the graph. */
private final boolean purgeExpiredData;

public SiriTimetableSnapshotSource(
TimetableSnapshotSourceParameters parameters,
TransitModel transitModel
) {
super(transitModel.getTransitLayerUpdater(), parameters, LocalDate::now);
this.transitModel = transitModel;
this.transitService = new DefaultTransitService(transitModel);
this.purgeExpiredData = parameters.purgeExpiredData();
this.tripPatternCache =
new SiriTripPatternCache(tripPatternIdGenerator, transitService::getPatternForTrip);

Expand All @@ -92,12 +88,9 @@ public UpdateResult applyEstimatedTimetable(
return UpdateResult.empty();
}

// Acquire lock on buffer
bufferLock.lock();

List<Result<UpdateSuccess, UpdateError>> results = new ArrayList<>();

try {
withLock(() -> {
if (fullDataset) {
// Remove all updates from the buffer
buffer.clear(feedId);
Expand All @@ -116,10 +109,8 @@ public UpdateResult applyEstimatedTimetable(
LOG.debug("message contains {} trip updates", updates.size());

purgeAndCommit();
} finally {
// Always release lock
bufferLock.unlock();
}
});

return UpdateResult.ofResults(results);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,10 @@ public UpdateResult applyTripUpdates(
return UpdateResult.empty();
}

// Acquire lock on buffer
bufferLock.lock();

Map<TripDescriptor.ScheduleRelationship, Integer> failuresByRelationship = new HashMap<>();
List<Result<UpdateSuccess, UpdateError>> results = new ArrayList<>();

try {
withLock(() -> {
if (fullDataset) {
// Remove all updates from the buffer
buffer.clear(feedId);
Expand Down Expand Up @@ -253,10 +250,7 @@ public UpdateResult applyTripUpdates(
}

purgeAndCommit();
} finally {
// Always release lock
bufferLock.unlock();
}
});

var updateResult = UpdateResult.ofResults(results);

Expand Down

0 comments on commit 80c17a6

Please sign in to comment.