patx/mrhttp-asgi
Error if caching wrong type
Commit 597bedf · Mark Reed · 2024-03-22T13:11:26-07:00
Comments
No comments yet.
Diff
diff --git a/dotests.py b/dotests.py
index 17486e8..c0215ff 100644
--- a/dotests.py
+++ b/dotests.py
@@ -113,13 +113,13 @@ async def run_benchmarks():
opts = ('-H','Cookie: mrsession=43709dd361cc443e976b05714581a7fb; foo=fdsfdasdfasdfdsfasdfsdfsdfasdfas; short=fazc;')
print(" Pipelined")
- print (" Hello ", await run_wrk(loop, 'http://localhost:8080/',lua='tests/lua/pipeline.lua'), "Requests/second" )
+ print (" Hello (cached) ", await run_wrk(loop, 'http://localhost:8080/cached',lua='tests/lua/q-cached.lua'), "Requests/second" )
print (" Hello ", await run_wrk(loop, 'http://localhost:8080/',lua='tests/lua/pipeline.lua'), "Requests/second" )
print (" More hdrs ", await run_wrk(loop, 'http://localhost:8080/',options=more_headers,lua='tests/lua/pipeline.lua'), "Requests/second" )
print (" Sessions ", await run_wrk(loop, 'http://localhost:8080/s',lua='tests/lua/q-session.lua'), "Requests/second" )
print (" File Upload ", await run_wrk(loop, 'http://localhost:8080/upload',lua='tests/lua/q-upload.lua'), "Requests/second" )
- print (" mrpacker ", await run_wrk(loop, 'http://localhost:8080/mrpacker',lua='tests/lua/q-mrp.lua'), "Requests/second" )
- print (" Form ", await run_wrk(loop, 'http://localhost:8080/form',lua='tests/lua/q-form.lua'), "Requests/second" )
+ #print (" mrpacker ", await run_wrk(loop, 'http://localhost:8080/mrpacker',lua='tests/lua/q-mrp.lua'), "Requests/second" )
+ #print (" Form ", await run_wrk(loop, 'http://localhost:8080/form',lua='tests/lua/q-form.lua'), "Requests/second" )
if 1:
print("")
diff --git a/src/mrhttp/app.py b/src/mrhttp/app.py
index 0007fad..456c939 100644
--- a/src/mrhttp/app.py
+++ b/src/mrhttp/app.py
@@ -145,7 +145,6 @@ class Application(mrhttp.CApp):
for item in self.static_cached_files:
fn = item[1]
if os.path.getmtime(fn) > self.static_cached_timestamp:
- print("DELME timer updating cache of ",fn)
with open(fn, 'rb') as f:
b = f.read()
self.router.update_cached_route( [item[0], b] )
diff --git a/src/mrhttp/internals/protocol.c b/src/mrhttp/internals/protocol.c
index 682bf0b..620ddb0 100644
--- a/src/mrhttp/internals/protocol.c
+++ b/src/mrhttp/internals/protocol.c
@@ -539,12 +539,11 @@ Protocol* Protocol_handle_request(Protocol* self, Request* request, Route* r) {
if ( self->request == request ) self->request = (Request*)MrhttpApp_get_request( self->app );
}
+ //if ( r->dyncache ) { // Lookup path in dict
+ //}
// If we have cached bytes
if ( r->cached ) {
- if ( PyBytes_Check( r->cached ) ) {
- if(!protocol_write_response(self, request, r->cached)) goto error;
- }
- // Else dynamic cache
+ if(!protocol_write_response(self, request, r->cached)) goto error;
}
if(!(result = protocol_callPageHandler(self, r->func, request)) ) {
diff --git a/src/mrhttp/internals/router.c b/src/mrhttp/internals/router.c
index 05dc136..eb44486 100644
--- a/src/mrhttp/internals/router.c
+++ b/src/mrhttp/internals/router.c
@@ -64,7 +64,9 @@ PyObject* Router_update_cached_route(Router* self, PyObject* item) {
//DBG printf("request path len %d - %.*s\n", (int)request->path_len, (int)request->path_len, request->path);
//DBG printf("route path %.*s \n", (int)r->len, r->path);
if ( plen == r->len && !memcmp(r->path, p, plen) ) {
+ if ( r->cached ) { Py_DECREF(b); }
r->cached = b;
+ Py_INCREF(b);
Py_RETURN_NONE;
}
}
@@ -103,9 +105,16 @@ PyObject *Router_setupRoutes (Router* self) {
o = PyDict_GetItemString( r, "type" );
if (o) rte->mtype = PyLong_AsLong(o);
rte->user_key = PyDict_GetItemString( r, "user_key" );
- rte->cached = PyDict_GetItemString( r, "cached" );
+ PyObject *b = PyDict_GetItemString( r, "cached" );
+ rte->cached = b; if (b) Py_INCREF(b);
if ( Py_True == PyDict_GetItemString( r, "cache" ) ) {
- rte->cached = PyObject_CallFunctionObjArgs(handler, r, NULL);
+ b = PyObject_CallFunctionObjArgs(handler, r, NULL);
+ rte->cached = b; Py_INCREF(b);
+ if ( !( PyBytes_Check(b) || PyUnicode_Check(b) ) ) {
+ printf(" ERROR - cached route must return a unicode or bytes object\n");
+ printf(" path: "); PyObject_Print(PyDict_GetItemString(r,"path"),stdout,0); printf("\n");
+ Py_DECREF(rte->cached); rte->cached = 0;
+ }
}
DBG printf(" path %.*s func ptr %p\n", (int)rte->len, rte->path, rte->func);
diff --git a/src/mrhttp/internals/router.h b/src/mrhttp/internals/router.h
index 25887f2..f857f8d 100644
--- a/src/mrhttp/internals/router.h
+++ b/src/mrhttp/internals/router.h
@@ -21,6 +21,7 @@ typedef struct {
int max_byte_size;
PyObject *cached;
+ PyObject *dyncache;
PyObject *user_key;
//char *user_key;
//long user_key_len;
diff --git a/tests/lua/pipeline.lua b/tests/lua/pipeline.lua
index bc37741..2a2530b 100755
--- a/tests/lua/pipeline.lua
+++ b/tests/lua/pipeline.lua
@@ -2,38 +2,37 @@
init = function(args)
local r = {}
- r[1] = wrk.format(nil, "/")
- r[2] = wrk.format(nil, "/")
- r[3] = wrk.format(nil, "/")
- r[4] = wrk.format(nil, "/")
- r[5] = wrk.format(nil, "/")
- r[6] = wrk.format(nil, "/")
- r[7] = wrk.format(nil, "/")
- r[8] = wrk.format(nil, "/")
- r[9] = wrk.format(nil, "/")
- r[10] = wrk.format(nil, "/")
- r[11] = wrk.format(nil, "/")
- r[12] = wrk.format(nil, "/")
- r[13] = wrk.format(nil, "/")
- r[14] = wrk.format(nil, "/")
- r[15] = wrk.format(nil, "/")
- r[16] = wrk.format(nil, "/")
- r[17] = wrk.format(nil, "/")
- r[18] = wrk.format(nil, "/")
- r[19] = wrk.format(nil, "/")
- r[20] = wrk.format(nil, "/")
- r[21] = wrk.format(nil, "/")
- r[22] = wrk.format(nil, "/")
- r[23] = wrk.format(nil, "/")
- r[24] = wrk.format(nil, "/")
- r[25] = wrk.format(nil, "/")
- r[26] = wrk.format(nil, "/")
- r[27] = wrk.format(nil, "/")
- r[28] = wrk.format(nil, "/")
- r[29] = wrk.format(nil, "/")
- r[30] = wrk.format(nil, "/")
- r[31] = wrk.format(nil, "/")
- r[32] = wrk.format(nil, "/")
+ table.insert( r, wrk.format(nil, "/") )
+ table.insert( r, wrk.format(nil, "/") )
+ table.insert( r, wrk.format(nil, "/") )
+ table.insert( r, wrk.format(nil, "/") )
+ table.insert( r, wrk.format(nil, "/") )
+ table.insert( r, wrk.format(nil, "/") )
+ table.insert( r, wrk.format(nil, "/") )
+ table.insert( r, wrk.format(nil, "/") )
+ table.insert( r, wrk.format(nil, "/") )
+ table.insert( r, wrk.format(nil, "/") )
+ table.insert( r, wrk.format(nil, "/") )
+ table.insert( r, wrk.format(nil, "/") )
+ table.insert( r, wrk.format(nil, "/") )
+ table.insert( r, wrk.format(nil, "/") )
+ table.insert( r, wrk.format(nil, "/") )
+ table.insert( r, wrk.format(nil, "/") )
+ table.insert( r, wrk.format(nil, "/") )
+ table.insert( r, wrk.format(nil, "/") )
+ table.insert( r, wrk.format(nil, "/") )
+ table.insert( r, wrk.format(nil, "/") )
+ table.insert( r, wrk.format(nil, "/") )
+ table.insert( r, wrk.format(nil, "/") )
+ table.insert( r, wrk.format(nil, "/") )
+ table.insert( r, wrk.format(nil, "/") )
+ table.insert( r, wrk.format(nil, "/") )
+ table.insert( r, wrk.format(nil, "/") )
+ table.insert( r, wrk.format(nil, "/") )
+ table.insert( r, wrk.format(nil, "/") )
+ table.insert( r, wrk.format(nil, "/") )
+ table.insert( r, wrk.format(nil, "/") )
+ table.insert( r, wrk.format(nil, "/") )
req = table.concat(r)
end
diff --git a/tests/lua/q-cached.lua b/tests/lua/q-cached.lua
new file mode 100755
index 0000000..95e3736
--- /dev/null
+++ b/tests/lua/q-cached.lua
@@ -0,0 +1,43 @@
+-- example script demonstrating HTTP pipelining
+
+init = function(args)
+ local r = {}
+ table.insert( r, wrk.format(nil, "/cached") )
+ table.insert( r, wrk.format(nil, "/cached") )
+ table.insert( r, wrk.format(nil, "/cached") )
+ table.insert( r, wrk.format(nil, "/cached") )
+ table.insert( r, wrk.format(nil, "/cached") )
+ table.insert( r, wrk.format(nil, "/cached") )
+ table.insert( r, wrk.format(nil, "/cached") )
+ table.insert( r, wrk.format(nil, "/cached") )
+ table.insert( r, wrk.format(nil, "/cached") )
+ table.insert( r, wrk.format(nil, "/cached") )
+ table.insert( r, wrk.format(nil, "/cached") )
+ table.insert( r, wrk.format(nil, "/cached") )
+ table.insert( r, wrk.format(nil, "/cached") )
+ table.insert( r, wrk.format(nil, "/cached") )
+ table.insert( r, wrk.format(nil, "/cached") )
+ table.insert( r, wrk.format(nil, "/cached") )
+ table.insert( r, wrk.format(nil, "/cached") )
+ table.insert( r, wrk.format(nil, "/cached") )
+ table.insert( r, wrk.format(nil, "/cached") )
+ table.insert( r, wrk.format(nil, "/cached") )
+ table.insert( r, wrk.format(nil, "/cached") )
+ table.insert( r, wrk.format(nil, "/cached") )
+ table.insert( r, wrk.format(nil, "/cached") )
+ table.insert( r, wrk.format(nil, "/cached") )
+ table.insert( r, wrk.format(nil, "/cached") )
+ table.insert( r, wrk.format(nil, "/cached") )
+ table.insert( r, wrk.format(nil, "/cached") )
+ table.insert( r, wrk.format(nil, "/cached") )
+ table.insert( r, wrk.format(nil, "/cached") )
+ table.insert( r, wrk.format(nil, "/cached") )
+ table.insert( r, wrk.format(nil, "/cached") )
+ table.insert( r, wrk.format(nil, "/cached") )
+ table.insert( r, wrk.format(nil, "/cached") )
+ req = table.concat(r)
+end
+
+request = function()
+ return req
+end
diff --git a/tst.py b/tst.py
index 6c2cb56..c140b13 100755
--- a/tst.py
+++ b/tst.py
@@ -12,8 +12,8 @@ app.config["memcache"] = [ ("127.0.0.1", 11211) ]
#app.c = asyncmrq.Client()
#await app.c.connect(servers=[("127.0.0.1",7100)])
-#@app.route('/',options=['cache'])
[email protected]('/')
+#@app.route('/')
[email protected]('/',options=['cache'])
def index(r):
return "hello world"
@@ -35,7 +35,7 @@ def firstarg(r,a):
try:
- app.run(cores=1)
+ app.run(cores=4)
except Exception as e:
print("YAY",e)