completed caching framework api

This commit is contained in:
roberto@sirius
2010-12-13 18:15:11 +01:00
parent 43c347d20e
commit 381da45196
8 changed files with 138 additions and 20 deletions
+1 -1
View File
@@ -321,7 +321,7 @@ static int uwsgi_handler(request_rec *r) {
vecptr = uwsgi_add_var(uwsgi_vars, vecptr, "SERVER_NAME", (char *) ap_get_server_name(r), &pkt_size) ;
vecptr = uwsgi_add_var(uwsgi_vars, vecptr, "SERVER_PORT", apr_psprintf(r->pool, "%u",ap_get_server_port(r)), &pkt_size) ;
vecptr = uwsgi_add_var(uwsgi_vars, vecptr, "SERVER_PROTOCOL", r->protocol, &pkt_size) ;
vecptr = uwsgi_add_var(uwsgi_vars, vecptr, "REQUEST_URI", r->uri, &pkt_size) ;
vecptr = uwsgi_add_var(uwsgi_vars, vecptr, "REQUEST_URI", r->unparsed_uri, &pkt_size) ;
vecptr = uwsgi_add_var(uwsgi_vars, vecptr, "REMOTE_ADDR", r->connection->remote_ip, &pkt_size) ;
vecptr = uwsgi_add_var(uwsgi_vars, vecptr, "REMOTE_USER", r->user ? r->user : "", &pkt_size) ;
if (r->user) {
+60
View File
@@ -94,6 +94,7 @@ int uwsgi_cache_set(char *key, uint16_t keylen, char *val, uint16_t vallen, uint
uwsgi_lock(uwsgi.cache_lock);
if (uwsgi.shared->cache_first_available_item >= uwsgi.cache_max_items) goto end;
uwsgi_log("putting cache data in key %.*s\n", keylen, key);
index = uwsgi_cache_get_index(key, keylen);
if (!index) {
index = uwsgi.shared->cache_first_available_item;
@@ -123,3 +124,62 @@ end:
return ret;
}
void cache_command(char *key, uint16_t keylen, char *val, uint16_t vallen, void *data) {
struct wsgi_request *wsgi_req = (struct wsgi_request *) data;
if (vallen > 0) {
if (!uwsgi_strncmp(key, keylen, "key", 3)) {
val = uwsgi_cache_get(val, vallen, &vallen);
if (val && vallen > 0) {
wsgi_req->response_size = write(wsgi_req->poll.fd, val, vallen);
}
}
}
}
int uwsgi_cache_request(struct wsgi_request *wsgi_req) {
uint16_t vallen = 0;
char *value;
switch(wsgi_req->uh.modifier2) {
case 0:
// get
if (wsgi_req->uh.pktsize > 0) {
value = uwsgi_cache_get(wsgi_req->buffer, wsgi_req->uh.pktsize, &vallen);
if (value && vallen > 0) {
wsgi_req->response_size = write(wsgi_req->poll.fd, value, vallen);
}
}
break;
case 1:
// set
break;
case 2:
// del
if (wsgi_req->uh.pktsize > 0) {
uwsgi_cache_del(wsgi_req->buffer, wsgi_req->uh.pktsize);
}
break;
case 4:
// dict
if (wsgi_req->uh.pktsize > 0) {
uwsgi_hooked_parse(wsgi_req->buffer, wsgi_req->uh.pktsize, cache_command, (void *) wsgi_req);
}
break;
}
return 0;
}
struct uwsgi_plugin uwsgi_cache_plugin = {
.name = "cache",
.modifier1 = 111,
.request = uwsgi_cache_request,
};
+2 -2
View File
@@ -23,7 +23,7 @@ char *new_cluster_hostname;
char *new_cluster_address;
char *new_cluster_workers;
void print_dict(char *key, uint16_t keylen, char *val, uint16_t vallen) {
void print_dict(char *key, uint16_t keylen, char *val, uint16_t vallen, void *data) {
uwsgi_log("%.*s = %.*s\n", keylen, key, vallen, val);
if (!uwsgi_strncmp("hostname", 8, key, keylen)) {
@@ -393,7 +393,7 @@ void master_loop(char **argv, char **environ) {
new_cluster_hostname = NULL;
new_cluster_address = NULL;
new_cluster_workers = NULL;
uwsgi_hooked_parse(uwsgi.wsgi_requests[0]->buffer, uwsgi.wsgi_requests[0]->uh.pktsize, print_dict);
uwsgi_hooked_parse(uwsgi.wsgi_requests[0]->buffer, uwsgi.wsgi_requests[0]->uh.pktsize, print_dict, NULL);
if (new_cluster_hostname && new_cluster_address && new_cluster_workers) {
uwsgi_cluster_add_node(new_cluster_address, atoi(new_cluster_workers), CLUSTER_NODE_DYNAMIC);
}
+61 -1
View File
@@ -111,6 +111,60 @@ static char *encode_lua_table(lua_State *L, int index, uint16_t *size) {
return buf;
}
static int uwsgi_api_cache_set(lua_State *L) {
int args = lua_gettop(L);
const char *key ;
const char *value ;
uint64_t expires = 0;
size_t vallen;
if (args > 1) {
key = lua_tolstring(L, 1, NULL);
value = lua_tolstring(L, 2, &vallen);
if (args > 2) {
expires = lua_tonumber(L, 3);
}
uwsgi_cache_set((char *)key, strlen(key), (char *)value, (uint16_t) vallen, expires);
}
lua_pushnil(L);
return 1;
}
static int uwsgi_api_cache_get(lua_State *L) {
char *value ;
uint16_t valsize;
const char *key ;
lca(L, 1);
if (lua_isstring(L, 1)) {
key = lua_tolstring(L, 1, NULL);
value = uwsgi_cache_get((char *)key, strlen(key), &valsize);
if (value) {
lua_pushlstring(L, value, valsize);
return 1;
}
}
lua_pushnil(L);
return 1;
}
static int uwsgi_api_send_message(lua_State *L) {
int args = lua_gettop(L);
@@ -144,7 +198,11 @@ static int uwsgi_api_send_message(lua_State *L) {
// passed a table
pkt = encode_lua_table(L, 4, &pktsize);
}
uwsgi_send_message(uwsgi_fd, modifier1, modifier2, pkt, pktsize, input_fd, input_size, timeout);
if (uwsgi_send_message(uwsgi_fd, modifier1, modifier2, pkt, pktsize, input_fd, input_size, timeout) == -1) {
free(pkt);
lua_pushnil(L);
return 1;
}
free(pkt);
for(;;) {
@@ -197,6 +255,8 @@ static const luaL_reg uwsgi_api[] = {
{"cl", uwsgi_api_cl},
{"req_fd", uwsgi_api_req_fd},
{"send_message", uwsgi_api_send_message},
{"cache_get", uwsgi_api_cache_get},
{"cache_set", uwsgi_api_cache_set},
{NULL, NULL}
};
+5 -5
View File
@@ -343,7 +343,7 @@ int uwsgi_parse_vars(struct wsgi_request *wsgi_req) {
#endif
ptrbuf += 2;
if (ptrbuf + strsize <= bufferend) {
uwsgi_log("uwsgi %.*s = %.*s\n", wsgi_req->hvec[wsgi_req->var_cnt].iov_len, wsgi_req->hvec[wsgi_req->var_cnt].iov_base, strsize, ptrbuf);
//uwsgi_log("uwsgi %.*s = %.*s\n", wsgi_req->hvec[wsgi_req->var_cnt].iov_len, wsgi_req->hvec[wsgi_req->var_cnt].iov_base, strsize, ptrbuf);
if (!uwsgi_strncmp("SCRIPT_NAME", 11, wsgi_req->hvec[wsgi_req->var_cnt].iov_base, wsgi_req->hvec[wsgi_req->var_cnt].iov_len)) {
wsgi_req->script_name = ptrbuf;
wsgi_req->script_name_len = strsize;
@@ -642,7 +642,7 @@ int uwsgi_get_dgram(int fd, struct wsgi_request *wsgi_req) {
}
int uwsgi_hooked_parse(char *buffer, size_t len, void (*hook)(char *, uint16_t, char *, uint16_t)) {
int uwsgi_hooked_parse(char *buffer, size_t len, void (*hook)(char *, uint16_t, char *, uint16_t, void*), void *data) {
char *ptrbuf, *bufferend;
uint16_t keysize = 0, valsize = 0;
@@ -677,7 +677,7 @@ int uwsgi_hooked_parse(char *buffer, size_t len, void (*hook)(char *, uint16_t,
if (ptrbuf + valsize > bufferend) return -1;
// now call the hook
hook(key, keysize, ptrbuf, valsize);
hook(key, keysize, ptrbuf, valsize, data);
ptrbuf += valsize;
}
@@ -685,7 +685,7 @@ int uwsgi_hooked_parse(char *buffer, size_t len, void (*hook)(char *, uint16_t,
}
int uwsgi_hooked_parse_dict_dgram(int fd, char *buffer, size_t len, uint8_t modifier1, uint8_t modifier2, void (*hook)(char *, uint16_t, char *, uint16_t)) {
int uwsgi_hooked_parse_dict_dgram(int fd, char *buffer, size_t len, uint8_t modifier1, uint8_t modifier2, void (*hook)(char *, uint16_t, char *, uint16_t, void *), void *data) {
struct uwsgi_header *uh;
ssize_t rlen;
@@ -737,7 +737,7 @@ int uwsgi_hooked_parse_dict_dgram(int fd, char *buffer, size_t len, uint8_t modi
uwsgi_log("%p %p %d\n", ptrbuf, bufferend, bufferend-ptrbuf);
uwsgi_hooked_parse(ptrbuf, bufferend-ptrbuf, hook);
uwsgi_hooked_parse(ptrbuf, bufferend-ptrbuf, hook, data);
return 0;
-6
View File
@@ -338,7 +338,6 @@ void uwsgi_close_request(struct wsgi_request *wsgi_req) {
int tmp_fd = -1;
void *async_post = NULL;
uwsgi_log("ending request\n");
gettimeofday(&wsgi_req->end_of_request, NULL);
uwsgi.workers[uwsgi.mywid].running_time += (double) (((double) (wsgi_req->end_of_request.tv_sec * 1000000 + wsgi_req->end_of_request.tv_usec) - (double) (wsgi_req->start_of_request.tv_sec * 1000000 + wsgi_req->start_of_request.tv_usec)) / (double) 1000.0);
@@ -348,11 +347,9 @@ void uwsgi_close_request(struct wsgi_request *wsgi_req) {
get_memusage();
uwsgi_log("LEAVE_OPEN: %d\n", wsgi_req->leave_open);
// close the connection with the webserver
if (!wsgi_req->fd_closed && !wsgi_req->leave_open) {
// NOTE, if we close the socket before receiving eventually sent data, socket layer will send a RST
uwsgi_log("CLOSE()\n");
close(wsgi_req->poll.fd);
}
else if (wsgi_req->leave_open) {
@@ -1171,8 +1168,6 @@ int uwsgi_waitfd(int fd, int timeout) {
timeout = timeout*1000;
if (timeout < 0) timeout = -1;
uwsgi_log("waiting for max %d secs\n", timeout);
upoll[0].fd = fd;
upoll[0].events = POLLIN | POLLPRI;
upoll[0].revents = 0;
@@ -1183,7 +1178,6 @@ int uwsgi_waitfd(int fd, int timeout) {
}
else if (ret > 0) {
if (upoll[0].revents & POLLIN) {
uwsgi_log("DETECTED DATA\n");
return ret;
}
+6 -2
View File
@@ -31,6 +31,8 @@ static char *short_options = NULL;
static char *base_short_options = "s:p:t:x:d:l:v:b:mcaCTiMhrR:z:A:Q:Ly:";
extern struct uwsgi_plugin uwsgi_cache_plugin;
UWSGI_DECLARE_EMBEDDED_PLUGINS
static struct option long_base_options[] = {
@@ -596,7 +598,7 @@ waitfd:
else if (rlen > 0) {
// receive the packet
char clusterbuf[4096];
if (!uwsgi_hooked_parse_dict_dgram(uwsgi.cluster_fd, clusterbuf, 4096, 99, 1, manage_string_opt)) {
if (!uwsgi_hooked_parse_dict_dgram(uwsgi.cluster_fd, clusterbuf, 4096, 99, 1, manage_string_opt, NULL)) {
goto options_parsed;
}
else {
@@ -914,6 +916,8 @@ options_parsed:
exit(1);
}
uwsgi_lock_init(uwsgi.cache_lock);
uwsgi.p[111] = &uwsgi_cache_plugin;
}
@@ -2251,7 +2255,7 @@ void build_options() {
}
void manage_string_opt(char *key, uint16_t keylen, char *val, uint16_t vallen) {
void manage_string_opt(char *key, uint16_t keylen, char *val, uint16_t vallen, void *data) {
struct option *lopt, *aopt;
+3 -3
View File
@@ -1209,9 +1209,9 @@ ssize_t uwsgi_send_empty_pkt(int , char *, uint8_t , uint8_t);
int uwsgi_waitfd(int, int);
int uwsgi_hooked_parse_dict_dgram(int, char *, size_t, uint8_t, uint8_t, void (*)(char *, uint16_t, char *, uint16_t));
int uwsgi_hooked_parse(char *, size_t, void (*)(char *, uint16_t, char *, uint16_t));
void manage_string_opt(char *, uint16_t, char*, uint16_t);
int uwsgi_hooked_parse_dict_dgram(int, char *, size_t, uint8_t, uint8_t, void (*)(char *, uint16_t, char *, uint16_t, void*), void *);
int uwsgi_hooked_parse(char *, size_t, void (*)(char *, uint16_t, char *, uint16_t, void *), void *);
void manage_string_opt(char *, uint16_t, char*, uint16_t, void *);
int uwsgi_get_dgram(int, struct wsgi_request *);