[core] static buffers for mtime_cache

This commit is contained in:
Glenn Strauss 2019-12-09 23:57:53 -05:00
parent b4a0ec05fd
commit 27e27e5e40
7 changed files with 42 additions and 43 deletions

View File

@ -2,8 +2,6 @@
#define _BASE_H_
#include "first.h"
#include "settings.h"
#include <sys/types.h>
#include <sys/time.h>
@ -242,11 +240,6 @@ typedef struct {
uint32_t used;
} connections;
typedef struct {
time_t mtime; /* the key */
buffer str; /* buffer for the string represenation */
} mtime_cache_type;
typedef struct {
void *ptr;
uint32_t used;
@ -349,9 +342,6 @@ struct server {
server_config srvconf;
/* caches */
mtime_cache_type mtime_cache[FILE_CACHE_MAX];
time_t loadts;
double loadavg[3];

View File

@ -117,26 +117,40 @@ int http_response_redirect_to_directory(connection *con, int status) {
return 0;
}
const buffer * strftime_cache_get(server *srv, time_t last_mod) {
static int i;
#define MTIME_CACHE_MAX 16
struct mtime_cache_type {
time_t mtime; /* key */
buffer str; /* buffer for string representation */
};
static struct mtime_cache_type mtime_cache[MTIME_CACHE_MAX];
static char mtime_cache_str[MTIME_CACHE_MAX][30];
/* 30-chars for "%a, %d %b %Y %H:%M:%S GMT" */
mtime_cache_type * const mtime_cache = srv->mtime_cache;
for (int j = 0; j < FILE_CACHE_MAX; ++j) {
if (mtime_cache[j].mtime == last_mod)
return &mtime_cache[j].str; /* found cache-entry */
}
void strftime_cache_reset(void) {
for (int i = 0; i < MTIME_CACHE_MAX; ++i) {
mtime_cache[i].mtime = (time_t)-1;
mtime_cache[i].str.ptr = mtime_cache_str[i];
mtime_cache[i].str.used = sizeof(mtime_cache_str[0]);
mtime_cache[i].str.size = sizeof(mtime_cache_str[0]);
}
}
if (++i == FILE_CACHE_MAX) {
i = 0;
}
const buffer * strftime_cache_get(const time_t last_mod) {
static int mtime_cache_idx;
mtime_cache[i].mtime = last_mod;
buffer * const b = &mtime_cache[i].str;
buffer_clear(b);
buffer_append_strftime(b, "%a, %d %b %Y %H:%M:%S GMT",
gmtime(&(mtime_cache[i].mtime)));
for (int j = 0; j < MTIME_CACHE_MAX; ++j) {
if (mtime_cache[j].mtime == last_mod)
return &mtime_cache[j].str; /* found cache-entry */
}
return b;
if (++mtime_cache_idx == MTIME_CACHE_MAX) mtime_cache_idx = 0;
const int i = mtime_cache_idx;
mtime_cache[i].mtime = last_mod;
strftime(mtime_cache[i].str.ptr, sizeof(mtime_cache_str[0]),
"%a, %d %b %Y %H:%M:%S GMT", gmtime(&mtime_cache[i].mtime));
return &mtime_cache[i].str;
}
@ -520,7 +534,7 @@ void http_response_send_file (connection *con, buffer *path) {
/* prepare header */
if (NULL == (mtime = http_header_response_get(con, HTTP_HEADER_LAST_MODIFIED, CONST_STR_LEN("Last-Modified")))) {
mtime = strftime_cache_get(con->srv, sce->st.st_mtime);
mtime = strftime_cache_get(sce->st.st_mtime);
http_header_response_set(con, HTTP_HEADER_LAST_MODIFIED, CONST_STR_LEN("Last-Modified"), CONST_BUF_LEN(mtime));
}

View File

@ -915,7 +915,7 @@ PHYSICALPATH_FUNC(mod_compress_physical) {
return HANDLER_GO_ON;
}
mtime = strftime_cache_get(con->srv, sce->st.st_mtime);
mtime = strftime_cache_get(sce->st.st_mtime);
/* try matching original etag of uncompressed version */
if (etag) {

View File

@ -1223,7 +1223,7 @@ static int mod_ssi_handle_request(connection *con, handler_ctx *p) {
etag_mutate(con->physical.etag, con->physical.etag);
http_header_response_set(con, HTTP_HEADER_ETAG, CONST_STR_LEN("ETag"), CONST_BUF_LEN(con->physical.etag));
mtime = strftime_cache_get(con->srv, st.st_mtime);
mtime = strftime_cache_get(st.st_mtime);
http_header_response_set(con, HTTP_HEADER_LAST_MODIFIED, CONST_STR_LEN("Last-Modified"), CONST_BUF_LEN(mtime));
if (HANDLER_FINISHED == http_response_handle_cachable(con, mtime)) {

View File

@ -52,5 +52,8 @@ void http_response_backend_done (connection *con);
void http_response_backend_error (connection *con);
void http_response_upgrade_read_body_unknown(connection *con);
const buffer * strftime_cache_get(server *srv, time_t last_mod);
__attribute_cold__
void strftime_cache_reset(void);
const buffer * strftime_cache_get(time_t last_mod);
#endif

View File

@ -2,19 +2,19 @@
#include "base.h"
#include "buffer.h"
#include "burl.h"
#include "network.h"
#include "log.h"
#include "rand.h"
#include "chunk.h"
#include "http_auth.h"
#include "http_vhostdb.h"
#include "http_auth.h" /* http_auth_dumbdata_reset() */
#include "http_vhostdb.h" /* http_vhostdb_dumbdata_reset() */
#include "fdevent.h"
#include "connections.h"
#include "sock_addr.h"
#include "stat_cache.h"
#include "plugin.h"
#include "network_write.h"
#include "network_write.h" /* network_write_show_handlers() */
#include "response.h" /* strftime_cache_reset() */
#ifdef HAVE_VERSIONSTAMP_H
# include "versionstamp.h"
@ -240,9 +240,7 @@ static server *server_init(void) {
CLEAN(tmp_buf);
#undef CLEAN
for (int i = 0; i < FILE_CACHE_MAX; ++i) {
srv->mtime_cache[i].mtime = (time_t)-1;
}
strftime_cache_reset();
li_rand_reseed();
@ -263,10 +261,6 @@ static server *server_init(void) {
__attribute_cold__
static void server_free(server *srv) {
for (int i = 0; i < FILE_CACHE_MAX; ++i) {
free(srv->mtime_cache[i].str.ptr);
}
if (oneshot_fd > 0) {
close(oneshot_fd);
}

View File

@ -2,8 +2,6 @@
#define _LIGHTTPD_SETTINGS_H_
#include "first.h"
#define FILE_CACHE_MAX 16
/**
* max size of a buffer which will just be reset
* to ->used = 0 instead of really freeing the buffer