1
#include "source/common/http/route_config_update_requster.h"
2

            
3
namespace Envoy {
4
namespace Http {
5

            
6
// TODO(chaoqin-li1123): Make on demand vhds and on demand srds works at the same time.
7
void RdsRouteConfigUpdateRequester::requestRouteConfigUpdate(
8
    RouteCache& route_cache, Http::RouteConfigUpdatedCallbackSharedPtr route_config_updated_cb,
9
    absl::optional<Router::ConfigConstSharedPtr> route_config, Event::Dispatcher& dispatcher,
10
76
    RequestHeaderMap& request_headers) {
11
76
  if (route_config.has_value() && route_config.value()->usesVhds()) {
12
24
    ASSERT(!request_headers.Host()->value().empty());
13
24
    const auto& host_header = absl::AsciiStrToLower(request_headers.getHostValue());
14
24
    requestVhdsUpdate(host_header, dispatcher, std::move(route_config_updated_cb));
15
24
    return;
16
55
  } else if (scope_key_builder_.has_value()) {
17
52
    Router::ScopeKeyPtr scope_key = scope_key_builder_->computeScopeKey(request_headers);
18
    // If scope_key is not null, the scope exists but RouteConfiguration is not initialized.
19
52
    if (scope_key != nullptr) {
20
52
      requestSrdsUpdate(route_cache, std::move(scope_key), dispatcher,
21
52
                        std::move(route_config_updated_cb));
22
52
      return;
23
52
    }
24
52
  }
25
  // Continue the filter chain if no on demand update is requested.
26
  (*route_config_updated_cb)(false);
27
}
28

            
29
void RdsRouteConfigUpdateRequester::requestVhdsUpdate(
30
    const std::string& host_header, Event::Dispatcher& thread_local_dispatcher,
31
24
    Http::RouteConfigUpdatedCallbackSharedPtr route_config_updated_cb) {
32
24
  route_config_provider_->requestVirtualHostsUpdate(host_header, thread_local_dispatcher,
33
24
                                                    std::move(route_config_updated_cb));
34
24
}
35

            
36
void RdsRouteConfigUpdateRequester::requestSrdsUpdate(
37
    RouteCache& route_cache, Router::ScopeKeyPtr scope_key,
38
    Event::Dispatcher& thread_local_dispatcher,
39
52
    Http::RouteConfigUpdatedCallbackSharedPtr route_config_updated_cb) {
40
  // Since inline scope_route_config_provider is not fully implemented and never used,
41
  // dynamic cast in constructor always succeed and the pointer should not be null here.
42
52
  ASSERT(scoped_route_config_provider_ != nullptr);
43
52
  Http::RouteConfigUpdatedCallback scoped_route_config_updated_cb =
44
52
      Http::RouteConfigUpdatedCallback(
45
52
          [weak_route_config_updated_cb =
46
52
               std::weak_ptr<Http::RouteConfigUpdatedCallback>(route_config_updated_cb),
47
52
           &route_cache](bool scope_exist) {
48
            // If the callback can be locked, this ActiveStream is still alive.
49
52
            if (auto cb = weak_route_config_updated_cb.lock()) {
50
              // Refresh the route before continue the filter chain.
51
48
              if (scope_exist) {
52
40
                route_cache.refreshCachedRoute();
53
40
              }
54
48
              (*cb)(scope_exist&& route_cache.hasCachedRoute());
55
48
            }
56
52
          });
57
52
  scoped_route_config_provider_->onDemandRdsUpdate(std::move(scope_key), thread_local_dispatcher,
58
52
                                                   std::move(scoped_route_config_updated_cb));
59
52
}
60

            
61
REGISTER_FACTORY(RdsRouteConfigUpdateRequesterFactory, RouteConfigUpdateRequesterFactory);
62

            
63
} // namespace Http
64
} // namespace Envoy