Skip to content

Commit

Permalink
Allow to set a timeout for searches
Browse files Browse the repository at this point in the history
If supported by the chosen router, it will stop expanding its search
interval after the given timeout.
  • Loading branch information
jbruechert committed Feb 18, 2024
1 parent 2a1a2cb commit 8bd933a
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
1 change: 1 addition & 0 deletions modules/intermodal/include/motis/intermodal/intermodal.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ struct intermodal : public motis::module::module {

std::string router_{"routing"};
bool revise_{false};
unsigned timeout_{0};
ppr_profiles ppr_profiles_;
};

Expand Down
4 changes: 3 additions & 1 deletion modules/intermodal/src/intermodal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ namespace motis::intermodal {
intermodal::intermodal() : module("Intermodal Options", "intermodal") {
param(router_, "router", "routing module");
param(revise_, "revise", "revise connections");
param(timeout_, "timeout", "routing timeout in seconds (0 = no timeout)");
}

intermodal::~intermodal() = default;
Expand Down Expand Up @@ -535,7 +536,8 @@ msg_ptr intermodal::route(msg_ptr const& msg) {
CreateRoutingRequest(mc, start.start_type_, start.start_, dest.station_,
req->search_type(), req->search_dir(),
mc.CreateVector(std::vector<Offset<Via>>{}),
mc.CreateVector(edges))
mc.CreateVector(edges), true, true, true, 0,
timeout_)
.Union(),
router);

Expand Down
23 changes: 16 additions & 7 deletions modules/nigiri/src/routing.cc
Original file line number Diff line number Diff line change
Expand Up @@ -129,16 +129,18 @@ std::vector<n::routing::offset> get_offsets(
template <n::direction SearchDir>
auto run_search(n::routing::search_state& search_state,
n::routing::raptor_state& raptor_state, n::timetable const& tt,
n::rt_timetable const* rtt, n::routing::query&& q) {
n::rt_timetable const* rtt,
std::optional<std::chrono::seconds> timeout,
n::routing::query&& q) {
if (rtt == nullptr) {
using algo_t = n::routing::raptor<SearchDir, false>;
return n::routing::search<SearchDir, algo_t>{tt, nullptr, search_state,
raptor_state, std::move(q)}
return n::routing::search<SearchDir, algo_t>{
tt, nullptr, search_state, raptor_state, std::move(q), timeout}
.execute();
} else {
using algo_t = n::routing::raptor<SearchDir, true>;
return n::routing::search<SearchDir, algo_t>{tt, rtt, search_state,
raptor_state, std::move(q)}
return n::routing::search<SearchDir, algo_t>{
tt, rtt, search_state, raptor_state, std::move(q), timeout}
.execute();
}
}
Expand All @@ -155,6 +157,13 @@ motis::module::msg_ptr route(tag_lookup const& tags, n::timetable const& tt,
auto extend_interval_later = false;
auto start_time = n::routing::start_time_t{};
auto start_station = n::location_idx_t::invalid();
auto timeout = [&]() -> std::optional<std::chrono::seconds> {
if (req->timeout() == 0) {
return std::nullopt;
} else {
return {std::chrono::seconds(req->timeout())};
}
}();

if (req->start_type() == routing::Start_PretripStart) {
auto const start =
Expand Down Expand Up @@ -309,14 +318,14 @@ motis::module::msg_ptr route(tag_lookup const& tags, n::timetable const& tt,
n::routing::raptor_stats raptor_stats;
if (req->search_dir() == SearchDir_Forward) {
auto const r = run_search<n::direction::kForward>(
*search_state, *raptor_state, tt, rtt, std::move(q));
*search_state, *raptor_state, tt, rtt, timeout, std::move(q));
journeys = r.journeys_;
search_stats = r.search_stats_;
raptor_stats = r.algo_stats_;
search_interval = r.interval_;
} else {
auto const r = run_search<n::direction::kBackward>(
*search_state, *raptor_state, tt, rtt, std::move(q));
*search_state, *raptor_state, tt, rtt, timeout, std::move(q));
journeys = r.journeys_;
search_stats = r.search_stats_;
raptor_stats = r.algo_stats_;
Expand Down
1 change: 1 addition & 0 deletions protocol/routing/RoutingRequest.fbs
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,5 @@ table RoutingRequest {
use_dest_metas: bool = true;
use_start_footpaths: bool = true;
schedule: ulong; // schedule id (0 = default)
timeout: int = 0; // 0 = none
}

0 comments on commit 8bd933a

Please sign in to comment.