[traffic] Cache routing session state, introduce IsObserverInhibited()

Signed-off-by: mvglasow <michael -at- vonglasow.com>
This commit is contained in:
mvglasow
2025-11-08 16:32:28 +02:00
parent f7882636cd
commit 1d87e0e987
2 changed files with 24 additions and 16 deletions

View File

@@ -206,8 +206,7 @@ void TrafficManager::OnChangeRoutingSessionState(routing::SessionState previous,
LOG(LINFO, ("Routing session state changed from", previous, "to", current)); LOG(LINFO, ("Routing session state changed from", previous, "to", current));
LOG(LINFO, ("Running on thread", std::this_thread::get_id())); LOG(LINFO, ("Running on thread", std::this_thread::get_id()));
m_observerInhibited = ((current == routing::SessionState::RouteBuilding) m_routingSessionState = current;
|| (current == routing::SessionState::RouteRebuilding));
/* /*
* Filter based on session state (see routing_callbacks.hpp for states and transitions). * Filter based on session state (see routing_callbacks.hpp for states and transitions).
@@ -806,7 +805,7 @@ void TrafficManager::OnTrafficDataUpdate()
auto const storageAge = currentTime - m_lastStorageUpdate; auto const storageAge = currentTime - m_lastStorageUpdate;
notifyDrape = (drapeAge >= kDrapeUpdateInterval); notifyDrape = (drapeAge >= kDrapeUpdateInterval);
updateStorage = (storageAge >= kStorageUpdateInterval); updateStorage = (storageAge >= kStorageUpdateInterval);
if (!m_observerInhibited) if (!IsObserverInhibited())
{ {
/* /*
* To avoid resetting the route over and over again while building, inhibit periodic updates * To avoid resetting the route over and over again while building, inhibit periodic updates

View File

@@ -533,6 +533,20 @@ private:
std::for_each(activeMwms.begin(), activeMwms.end(), std::forward<F>(f)); std::for_each(activeMwms.begin(), activeMwms.end(), std::forward<F>(f));
} }
/**
* @brief Whether updates to the observer are currently inhibited.
*
* Updates are inhibited while a route calculation is in progress. In this state, the observer
* receives traffic updates only if the queue has run empty, not if nore locations are waiting
* to be decoded.
*
* Inhibtiting the observer is necessary as traffic updates during route calculation will cause
* it to restart from scratch. Once the route has been calculated, updates will trigger a
* recalculation, which is much faster (seconds or less).
*/
bool IsObserverInhibited() const { return (m_routingSessionState == routing::SessionState::RouteBuilding)
|| (m_routingSessionState == routing::SessionState::RouteRebuilding); }
DataSource & m_dataSource; DataSource & m_dataSource;
CountryInfoGetterFn m_countryInfoGetterFn; CountryInfoGetterFn m_countryInfoGetterFn;
CountryParentNameGetterFn m_countryParentNameGetterFn; CountryParentNameGetterFn m_countryParentNameGetterFn;
@@ -546,6 +560,14 @@ private:
*/ */
routing::RoutingSession & m_routingSession; routing::RoutingSession & m_routingSession;
/**
* @brief Cached state of the routing session.
*
* `m_routingSession` methods which query the state may only be called from the GUI thread,
* therefore we are caching this value when we get notified of a change.
*/
routing::SessionState m_routingSessionState = routing::SessionState::NoValidRoute;
df::DrapeEngineSafePtr m_drapeEngine; df::DrapeEngineSafePtr m_drapeEngine;
std::atomic<int64_t> m_currentDataVersion; std::atomic<int64_t> m_currentDataVersion;
@@ -651,19 +673,6 @@ private:
*/ */
std::chrono::time_point<std::chrono::steady_clock> m_lastObserverUpdate; std::chrono::time_point<std::chrono::steady_clock> m_lastObserverUpdate;
/**
* @brief Whether updates to the observer are currently inhibited.
*
* Updates are inhibited while a route calculation is in progress. In this state, the observer
* receives traffic updates only if the queue has run empty, not if nore locations are waiting
* to be decoded.
*
* Inhibtiting the observer is necessary as traffic updates during route calculation will cause
* it to restart from scratch. Once the route has been calculated, updates will trigger a
* recalculation, which is much faster (seconds or less).
*/
bool m_observerInhibited = false;
/** /**
* @brief When the cache file was last updated. * @brief When the cache file was last updated.
*/ */