Line data Source code
1 : #include "source/extensions/config_subscription/grpc/eds_resources_cache_impl.h" 2 : 3 : #include "source/common/common/logger.h" 4 : 5 : namespace Envoy { 6 : namespace Config { 7 : 8 : void EdsResourcesCacheImpl::setResource( 9 : absl::string_view resource_name, 10 0 : const envoy::config::endpoint::v3::ClusterLoadAssignment& resource) { 11 0 : resources_map_.insert_or_assign(resource_name, ResourceData(resource)); 12 0 : } 13 : 14 0 : void EdsResourcesCacheImpl::removeResource(absl::string_view resource_name) { 15 0 : if (const auto& resource_it = resources_map_.find(resource_name); 16 0 : resource_it != resources_map_.end()) { 17 : // Invoke the callbacks, and remove all watchers. 18 0 : for (auto& removal_cb : resource_it->second.removal_cbs_) { 19 0 : removal_cb->onCachedResourceRemoved(resource_name); 20 0 : } 21 0 : resource_it->second.removal_cbs_.clear(); 22 : 23 : // Remove the resource entry from the cache. 24 0 : resources_map_.erase(resource_it); 25 0 : } 26 : // Remove the expiration timers (if any) as there's no longer interest in the resource. 27 0 : expiry_timers_.erase(resource_name); 28 0 : } 29 : 30 : OptRef<const envoy::config::endpoint::v3::ClusterLoadAssignment> 31 : EdsResourcesCacheImpl::getResource(absl::string_view resource_name, 32 0 : EdsResourceRemovalCallback* removal_cb) { 33 0 : if (const auto& resource_it = resources_map_.find(resource_name); 34 0 : resource_it != resources_map_.end()) { 35 0 : ENVOY_LOG_MISC(trace, "Returning resource {} from the xDS resource cache", resource_name); 36 : // Add the removal callback to the list associated with the resource. 37 0 : if (removal_cb != nullptr) { 38 0 : resource_it->second.removal_cbs_.push_back(removal_cb); 39 0 : } 40 0 : return resource_it->second.resource_; 41 0 : } 42 : // The resource doesn't exist in the resource map. 43 0 : return {}; 44 0 : } 45 : 46 : void EdsResourcesCacheImpl::removeCallback(absl::string_view resource_name, 47 0 : EdsResourceRemovalCallback* removal_cb) { 48 0 : if (const auto& resource_it = resources_map_.find(resource_name); 49 0 : resource_it != resources_map_.end()) { 50 0 : ENVOY_LOG_MISC(trace, "Removing callback for resource {} from the xDS resource cache", 51 0 : resource_name); 52 0 : resource_it->second.removal_cbs_.erase(std::remove(resource_it->second.removal_cbs_.begin(), 53 0 : resource_it->second.removal_cbs_.end(), 54 0 : removal_cb)); 55 0 : } 56 0 : } 57 : 58 0 : uint32_t EdsResourcesCacheImpl::cacheSizeForTest() const { return resources_map_.size(); } 59 : 60 : void EdsResourcesCacheImpl::setExpiryTimer(absl::string_view resource_name, 61 0 : std::chrono::milliseconds ms) { 62 0 : auto it = expiry_timers_.find(resource_name); 63 0 : if (it == expiry_timers_.end()) { 64 : // No timer for this resource, create one, and create a copy of resource_name that will outlive 65 : // this function. 66 0 : Event::TimerPtr resource_timeout = 67 0 : dispatcher_.createTimer([this, str_resource_name = std::string(resource_name)]() -> void { 68 : // On expiration the resource is removed (from the cache and from the watchers). 69 0 : removeResource(str_resource_name); 70 0 : }); 71 0 : it = expiry_timers_.emplace(resource_name, std::move(resource_timeout)).first; 72 0 : } 73 0 : (it->second)->enableTimer(ms); 74 0 : } 75 : 76 0 : void EdsResourcesCacheImpl::disableExpiryTimer(absl::string_view resource_name) { 77 0 : auto it = expiry_timers_.find(resource_name); 78 0 : if (it != expiry_timers_.end()) { 79 0 : (it->second)->disableTimer(); 80 : // Remove the timer as it is no longer needed. 81 0 : expiry_timers_.erase(it); 82 0 : } 83 0 : } 84 : 85 : } // namespace Config 86 : } // namespace Envoy