Skip to content

Commit

Permalink
Implement cache entry invalidation by making it stale. (#501)
Browse files Browse the repository at this point in the history
  • Loading branch information
keshonok committed Jun 20, 2016
1 parent a36c52a commit 3b3b36b
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 59 deletions.
22 changes: 16 additions & 6 deletions tempesta_fw/cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@

/* Flags stored in a Cache Entry. */
#define TFW_CE_MUST_REVAL 0x0001 /* MUST revalidate if stale. */
#define TFW_CE_INVALID 0x0100

/*
* @trec - Database record descriptor;
Expand Down Expand Up @@ -382,6 +381,9 @@ tfw_cache_entry_is_live(TfwHttpReq *req, TfwCacheEntry *ce)
time_t ce_age = tfw_cache_entry_age(ce);
time_t ce_lifetime, lt_fresh = UINT_MAX;

if (ce->lifetime <= 0)
return 0;

#define CC_LIFETIME_FRESH (TFW_HTTP_CC_MAX_AGE | TFW_HTTP_CC_MIN_FRESH)
if (req->cache_ctl.flags & CC_LIFETIME_FRESH) {
time_t lt_max_age = UINT_MAX, lt_min_fresh = UINT_MAX;
Expand Down Expand Up @@ -1135,19 +1137,18 @@ cache_req_process_node(TfwHttpReq *req, unsigned long key,

/*
* Invalidate a cache entry.
* In fact, this is implemented by making the cache entry stale.
*/
static int
tfw_cache_purge_set_expired(TfwHttpReq *req, unsigned long key)
tfw_cache_purge_invalidate(TfwHttpReq *req, unsigned long key)
{
TdbIter iter;
TDB *db = node_db();
TfwCacheEntry *ce = NULL;

if (!(ce = tfw_cache_dbce_get(db, &iter, req, key)))
return -ENOENT;

/* ce->lifetime = 0; */

ce->lifetime = 0;
tfw_cache_dbce_put(ce);
return 0;
}
Expand All @@ -1170,7 +1171,16 @@ tfw_cache_purge_method(TfwHttpReq *req, unsigned long key)
if (!tfw_capuacl_match(vhost, &saddr))
return tfw_http_send_403((TfwHttpMsg *)req);

if (tfw_cache_purge_set_expired(req, key))
/* Only "invalidate" option is implemented at this time. */
switch (vhost->cache_purge_mode) {
case TFW_D_CACHE_PURGE_INVALIDATE:
ret = tfw_cache_purge_invalidate(req, key);
break;
default:
return tfw_http_send_403((TfwHttpMsg *)req);
}

if (ret)
return tfw_http_send_404((TfwHttpMsg *)req);
else
return tfw_http_send_200((TfwHttpMsg *)req);
Expand Down
102 changes: 50 additions & 52 deletions tempesta_fw/vhost.c
Original file line number Diff line number Diff line change
Expand Up @@ -559,41 +559,6 @@ tfw_cleanup_locache(TfwCfgSpec *cs)
__tfw_cleanup_locache();
}

/*
* Process hdr_via directive.
* Default value is preset statically.
*/
static int
tfw_handle_out_hdr_via(TfwCfgSpec *cs, TfwCfgEntry *ce)
{
int len;
TfwVhost *vhost = &tfw_vhost_dflt;

if (ce->attr_n) {
TFW_ERR("%s: Arguments may not have the \'=\' sign\n",
cs->name);
return -EINVAL;
}
if (ce->val_n != 1) {
TFW_ERR("%s: Invalid number of arguments: %d\n",
cs->name, (int)ce->val_n);
return -EINVAL;
}

/*
* If a value is specified in the configuration file, then
* the default value is not used, even if the processing of
* the specified value results in an error.
*/
len = strlen(ce->vals[0]);
if ((vhost->hdr_via = kmalloc(len + 1, GFP_KERNEL)) == NULL)
return -ENOMEM;
memcpy((void *)vhost->hdr_via, (void *)ce->vals[0], len + 1);
vhost->hdr_via_len = len;

return 0;
}

/*
* Match the ip address against the ACL list.
*/
Expand Down Expand Up @@ -653,20 +618,6 @@ tfw_handle_cache_purge_acl(TfwCfgSpec *cs, TfwCfgEntry *ce)
return 0;
}

static void
__tfw_cleanup_hdrvia(void)
{
TfwVhost *vhost = &tfw_vhost_dflt;
if (vhost->hdr_via && (vhost->hdr_via != s_hdr_via_dflt))
kfree(vhost->hdr_via);
}

static void
tfw_cleanup_hdrvia(TfwCfgSpec *cs)
{
__tfw_cleanup_hdrvia();
}

/*
* Process the cache_purge directive.
*/
Expand All @@ -690,8 +641,6 @@ tfw_handle_cache_purge(TfwCfgSpec *cs, TfwCfgEntry *ce)
TFW_CFG_ENTRY_FOR_EACH_VAL(ce, i, val) {
if (!strcasecmp(val, "invalidate")) {
vhost->cache_purge_mode = TFW_D_CACHE_PURGE_INVALIDATE;
} else if (!strcasecmp(val, "delete")) {
vhost->cache_purge_mode = TFW_D_CACHE_PURGE_DELETE;
} else {
TFW_ERR("%s: unsupported argument: '%s'\n",
cs->name, val);
Expand All @@ -704,6 +653,55 @@ tfw_handle_cache_purge(TfwCfgSpec *cs, TfwCfgEntry *ce)
return 0;
}

/*
* Process hdr_via directive.
* Default value is preset statically.
*/
static int
tfw_handle_hdr_via(TfwCfgSpec *cs, TfwCfgEntry *ce)
{
int len;
TfwVhost *vhost = &tfw_vhost_dflt;

if (ce->attr_n) {
TFW_ERR("%s: Arguments may not have the \'=\' sign\n",
cs->name);
return -EINVAL;
}
if (ce->val_n != 1) {
TFW_ERR("%s: Invalid number of arguments: %d\n",
cs->name, (int)ce->val_n);
return -EINVAL;
}

/*
* If a value is specified in the configuration file, then
* the default value is not used, even if the processing of
* the specified value results in an error.
*/
len = strlen(ce->vals[0]);
if ((vhost->hdr_via = kmalloc(len + 1, GFP_KERNEL)) == NULL)
return -ENOMEM;
memcpy((void *)vhost->hdr_via, (void *)ce->vals[0], len + 1);
vhost->hdr_via_len = len;

return 0;
}

static void
__tfw_cleanup_hdrvia(void)
{
TfwVhost *vhost = &tfw_vhost_dflt;
if (vhost->hdr_via && (vhost->hdr_via != s_hdr_via_dflt))
kfree(vhost->hdr_via);
}

static void
tfw_cleanup_hdrvia(TfwCfgSpec *cs)
{
__tfw_cleanup_hdrvia();
}

static int
tfw_vhost_cfg_start(void)
{
Expand Down Expand Up @@ -742,7 +740,7 @@ static TfwCfgSpec tfw_location_specs[] = {
static TfwCfgSpec tfw_vhost_cfg_specs[] = {
{
"hdr_via", NULL,
tfw_handle_out_hdr_via,
tfw_handle_hdr_via,
.allow_none = true,
.allow_repeat = false,
.cleanup = tfw_cleanup_hdrvia
Expand Down
1 change: 0 additions & 1 deletion tempesta_fw/vhost.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ typedef struct {
/* Cache purge configuration modes. */
enum {
TFW_D_CACHE_PURGE_INVALIDATE,
TFW_D_CACHE_PURGE_DELETE,
};

/*
Expand Down

0 comments on commit 3b3b36b

Please sign in to comment.