[cpp-opensaml] 05/06: SSPCPP-756 Helper method cacheEntity

Rod Widdowson rdw at steadingsoftware.com
Thu Nov 9 07:48:19 EST 2017


This is an automated email from the git hooks/post-receive script.

rdw pushed a commit to branch master
in repository cpp-opensaml.

View the commit online:
http://git.shibboleth.net/view/?p=cpp-opensaml.git;a=commit;h=fa83182f529397b592780cec82ed11a7c434ccbd

commit fa83182f529397b592780cec82ed11a7c434ccbd
Author: Rod Widdowson <rdw at steadingsoftware.com>
AuthorDate: Sun Nov 5 16:18:59 2017 +0000

    SSPCPP-756 Helper method cacheEntity
    
    https://issues.shibboleth.net/jira/browse/SSPCPP-756
    
    This method will be needed by the "recover from on disk cache"
    code in the SP.
    
    It does both the index (in the base class) and the lookup caching.
    It works out the cache expirey from the parameterization and the entity
    itself.
---
 .../metadata/AbstractDynamicMetadataProvider.h     | 10 ++++
 .../impl/AbstractDynamicMetadataProvider.cpp       | 57 +++++++++++++---------
 2 files changed, 45 insertions(+), 22 deletions(-)

diff --git a/saml/saml2/metadata/AbstractDynamicMetadataProvider.h b/saml/saml2/metadata/AbstractDynamicMetadataProvider.h
index b5fc758..6392f9f 100644
--- a/saml/saml2/metadata/AbstractDynamicMetadataProvider.h
+++ b/saml/saml2/metadata/AbstractDynamicMetadataProvider.h
@@ -28,6 +28,7 @@
 #define __saml2_dynmetadataprov_h__
 
 #include <saml/saml2/metadata/AbstractMetadataProvider.h>
+#include <xmltooling/Lockable.h>
 
 namespace xmltooling {
     class XMLTOOL_API CondWait;
@@ -73,6 +74,15 @@ namespace opensaml {
              */
             virtual EntityDescriptor* resolve(const Criteria& criteria) const = 0;
 
+            /**
+             * Index an entity and cache the fact of it being indexed.
+             *
+             * @param entity what to cache
+             * @param locked have we locked ourself exclusive first?
+             * @return the cache ttl (for logging purposes)
+             */
+            virtual time_t cacheEntity(EntityDescriptor* entity, bool locked = false) const;
+
         private:
             std::string m_id;
             std::auto_ptr<xmltooling::RWLock> m_lock;
diff --git a/saml/saml2/metadata/impl/AbstractDynamicMetadataProvider.cpp b/saml/saml2/metadata/impl/AbstractDynamicMetadataProvider.cpp
index 0277ac2..f2ad78d 100644
--- a/saml/saml2/metadata/impl/AbstractDynamicMetadataProvider.cpp
+++ b/saml/saml2/metadata/impl/AbstractDynamicMetadataProvider.cpp
@@ -315,22 +315,6 @@ pair<const EntityDescriptor*,const RoleDescriptor*> AbstractDynamicMetadataProvi
 
         log.info("caching resolved metadata for (%s)", name.c_str());
 
-        // Compute the smaller of the validUntil / cacheDuration constraints.
-        time_t cacheExp = (entity2->getValidUntil() ? entity2->getValidUntilEpoch() : SAMLTIME_MAX) - now;
-        if (entity2->getCacheDuration())
-            cacheExp = min(cacheExp, entity2->getCacheDurationEpoch());
-            
-        // Adjust for the delay factor.
-        cacheExp *= m_refreshDelayFactor;
-
-        // Bound by max and min.
-        if (cacheExp > m_maxCacheDuration)
-            cacheExp = m_maxCacheDuration;
-        else if (cacheExp < m_minCacheDuration)
-            cacheExp = m_minCacheDuration;
-
-        log.info("next refresh of metadata for (%s) no sooner than %u seconds", name.c_str(), cacheExp);
-
         // Upgrade our lock so we can cache the new metadata.
         m_lock->unlock();
         m_lock->wrlock();
@@ -339,13 +323,10 @@ pair<const EntityDescriptor*,const RoleDescriptor*> AbstractDynamicMetadataProvi
         // Notify observers.
         emitChangeEvent(*entity2);
 
-        // Record the proper refresh time.
-        m_cacheMap[entity2->getEntityID()] = now + cacheExp;
+        time_t cacheExp = cacheEntity(entity2.get(), true);
+
+        log.info("next refresh of metadata for (%s) no sooner than %u seconds", name.c_str(), cacheExp);
 
-        // Make sure we clear out any existing copies, including stale metadata or if somebody snuck in.
-        cacheExp = SAMLTIME_MAX;
-        unindex(entity2->getEntityID(), true);  // actually frees the old instance with this ID
-        indexEntity(entity2.get(), cacheExp);
         entity2.release();
 
         m_lastUpdate = now;
@@ -384,3 +365,35 @@ pair<const EntityDescriptor*,const RoleDescriptor*> AbstractDynamicMetadataProvi
     return getEntityDescriptor(criteria);
 }
 
+time_t  AbstractDynamicMetadataProvider::cacheEntity(EntityDescriptor* entity, bool writeLocked) const
+{
+    time_t now = time(nullptr);
+    if (!writeLocked) {
+        m_lock->wrlock();
+    }
+    Locker locker(writeLocked ? nullptr : const_cast<AbstractDynamicMetadataProvider*>(this));
+
+    // Compute the smaller of the validUntil / cacheDuration constraints.
+    time_t cacheExp = (entity->getValidUntil() ? entity->getValidUntilEpoch() : SAMLTIME_MAX) - now;
+    if (entity->getCacheDuration())
+        cacheExp = min(cacheExp, entity->getCacheDurationEpoch());
+
+    // Adjust for the delay factor.
+    cacheExp *= m_refreshDelayFactor;
+
+    // Bound by max and min.
+    if (cacheExp > m_maxCacheDuration)
+        cacheExp = m_maxCacheDuration;
+    else if (cacheExp < m_minCacheDuration)
+        cacheExp = m_minCacheDuration;
+
+    // Record the proper refresh time.
+    m_cacheMap[entity->getEntityID()] = now + cacheExp;
+
+    // Make sure we clear out any existing copies, including stale metadata or if somebody snuck in.
+    unindex(entity->getEntityID(), true);  // actually frees the old instance with this ID
+    time_t exp(SAMLTIME_MAX);
+    indexEntity(entity, exp);
+
+    return cacheExp;
+}

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the commits mailing list