diff options
author | Jakub Hrozek <jhrozek@redhat.com> | 2012-01-09 15:03:12 +0100 |
---|---|---|
committer | Stephen Gallagher <sgallagh@redhat.com> | 2012-02-05 19:16:16 -0500 |
commit | e24a0656252c167e644b4758e5e53afe69be02e1 (patch) | |
tree | ddf1d686c7d20463a50a0f6f8275b2284efac071 /src/responder/common | |
parent | 98076cabc2a8b8f71dc3bc1263519827f71a5fcc (diff) | |
download | sssd-e24a0656252c167e644b4758e5e53afe69be02e1.tar.gz sssd-e24a0656252c167e644b4758e5e53afe69be02e1.tar.xz sssd-e24a0656252c167e644b4758e5e53afe69be02e1.zip |
Split the logic to check cache expiration into separate function
Diffstat (limited to 'src/responder/common')
-rw-r--r-- | src/responder/common/responder.h | 5 | ||||
-rw-r--r-- | src/responder/common/responder_cmd.c | 51 |
2 files changed, 56 insertions, 0 deletions
diff --git a/src/responder/common/responder.h b/src/responder/common/responder.h index 0eabe647d..4a205c85b 100644 --- a/src/responder/common/responder.h +++ b/src/responder/common/responder.h @@ -169,6 +169,11 @@ errno_t setent_add_ref(TALLOC_CTX *memctx, void setent_notify(struct setent_req_list *list, errno_t err); void setent_notify_done(struct setent_req_list *list); +errno_t +sss_cmd_check_cache(struct ldb_message *msg, + int cache_refresh_percent, + uint64_t cache_expire); + typedef void (*sss_dp_callback_t)(uint16_t err_maj, uint32_t err_min, const char *err_msg, void *ptr); diff --git a/src/responder/common/responder_cmd.c b/src/responder/common/responder_cmd.c index 28315e060..f36ea14af 100644 --- a/src/responder/common/responder_cmd.c +++ b/src/responder/common/responder_cmd.c @@ -19,6 +19,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. */ #include <errno.h> +#include "db/sysdb.h" #include "util/util.h" #include "responder/common/responder.h" #include "responder/common/responder_packet.h" @@ -234,3 +235,53 @@ void setent_notify_done(struct setent_req_list *list) { return setent_notify(list, EOK); } + +/* + * Return values: + * EOK - cache hit + * EAGAIN - cache hit, but schedule off band update + * ENOENT - cache miss + */ +errno_t +sss_cmd_check_cache(struct ldb_message *msg, + int cache_refresh_percent, + uint64_t cache_expire) +{ + uint64_t lastUpdate; + uint64_t midpoint_refresh = 0; + time_t now; + + now = time(NULL); + lastUpdate = ldb_msg_find_attr_as_uint64(msg, SYSDB_LAST_UPDATE, 0); + midpoint_refresh = 0; + + if(cache_refresh_percent) { + midpoint_refresh = lastUpdate + + (cache_expire - lastUpdate)*cache_refresh_percent/100; + if (midpoint_refresh - lastUpdate < 10) { + /* If the percentage results in an expiration + * less than ten seconds after the lastUpdate time, + * that's too often we will simply set it to 10s + */ + midpoint_refresh = lastUpdate+10; + } + } + + if (cache_expire > now) { + /* cache still valid */ + + if (midpoint_refresh && midpoint_refresh < now) { + /* We're past the the cache refresh timeout + * We'll return the value from the cache, but we'll also + * queue the cache entry for update out-of-band. + */ + return EAGAIN; + } else { + /* Cache is still valid. */ + return EOK; + } + } + + /* Cache needs to be updated */ + return ENOENT; +} |