patx/mrhttp-asgi
calloc the routes
Commit e41dd1f · Mark Reed · 2024-03-20T14:51:29-07:00
Comments
No comments yet.
Diff
diff --git a/examples/9_mrworkserver.py b/examples/9_mrworkserver.py
index af65bbd..2a22ce4 100755
--- a/examples/9_mrworkserver.py
+++ b/examples/9_mrworkserver.py
@@ -25,17 +25,18 @@
# python tst.py 7100
# python tst.py 7101
#
-# curl -i --raw http://localhost:8080/q/0/0/ -X POST -d '{"username":"xyz"}'
+# curl -H "Content-Type: application/mrpacker" --data-binary @tests/lua/test.mrp http://localhost:8080/q/0
# curl -H "Content-Type: application/mrpacker" --data-binary @tests/lua/test.mrp http://localhost:8080/q2/0
#
+
# To fetch a user's session and pass it to mrworkserver:
#
# Set a session in memcached by logging in:
-# curl -i --raw http://localhost:8080/
+# curl -v http://localhost:8080/login
# example output: AeeZjIBxhxDEaTKc69MTge3tq_kqf7Bh
#
# Use the session key that was output:
-# curl -i --raw http://localhost:8080/sq/ -H "Cookie: mrsession=AeeZjIBxhxDEaTKc69MTge3tq_kqf7Bh;" -X POST -d '{"test":"xyz"}'
+# curl -v http://localhost:8080/sq2/ -H "Cookie: mrsession=AeexlGcHq_dFZd6pJnvjgqu3NhrAS4qu;" -H "Content-Type: application/mrpacker" --data-binary @tests/lua/test.mrp
import mrhttp
import mrjson as json
@@ -44,21 +45,28 @@ app = mrhttp.Application()
app.config["memcache"] = [ ("127.0.0.1", 11211) ]
# We setup 2 clusters of 1 server each
-app.config["mrq"] = [("127.0.0.1",7100)]
+app.config["mrq"] = [("127.0.0.1",7100)]
app.config["mrq2"] = [("127.0.0.1",7101)]
[email protected]('/q/{}/{}/',options=['mrq'])
-def queue(r, s, t):
[email protected]('/q/{}/',options=['mrq'])
+def queue(r, arg1):
+ print("DELME arg1",arg1)
if r.servers_down:
return "Servers not available, try again later"
return 'Hello World!'
[email protected]('/')
-async def login(r):
[email protected]('/q2/{}/',options=['mrq2'])
+def q2(r, arg1):
+ if r.servers_down:
+ return "Servers not available, try again later"
+ return 'Hello World!'
+
[email protected]('/login')
+async def login(r):
user_id = 10999
user = {"name":"Mark"}
- return str( app.setUserSessionAndCookies( r, user_id, user, json=True ) ) + "\n"
+ return str( app.setUserSessionAndCookies( r, user_id, user ) ) + "\n"
@app.route('/sq/',options=['session','mrq','append_user'])
@@ -69,8 +77,10 @@ def session_queue(r):
return "Servers not available, try again later"
return 'Hello World!'
[email protected]('/q2/{}',options=['mrq2'])
-def queue2(r, some_id):
[email protected]('/sq2/',options=['session','mrq2','append_user'])
+def sq2(r):
+ if r.user == None:
+ return "Not logged in"
if r.servers_down:
return "Servers not available, try again later"
return 'Hello World!'
diff --git a/src/mrhttp/app.py b/src/mrhttp/app.py
index ea45879..0007fad 100644
--- a/src/mrhttp/app.py
+++ b/src/mrhttp/app.py
@@ -72,6 +72,7 @@ class Application(mrhttp.CApp):
self._mrq2 = None
self._mrc = None
self.static_cached_files = []
+ self.static_cached_update_time = 10
self.session_backend = "memcached"
self.uses_session = False
self.uses_mrq = False
@@ -144,11 +145,12 @@ 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] )
self.static_cached_timestamp = ts
- self.loop.call_later(10, self.static_cached_timer)
+ self.loop.call_later(self.static_cached_update_time, self.static_cached_timer) # Default 10 seconds
#TODO Use brotli'd files - if [path].br exists use that instead
@@ -166,6 +168,7 @@ class Application(mrhttp.CApp):
if os.path.isdir(fn): continue
with open(fn, 'rb') as f:
b = f.read()
+ print("DELME loaded",fn," bytes ",len(b))
if not root.startswith('/'): root = '/'+root
diff --git a/src/mrhttp/internals/common.h b/src/mrhttp/internals/common.h
index 38cd661..f0a09ce 100644
--- a/src/mrhttp/internals/common.h
+++ b/src/mrhttp/internals/common.h
@@ -1,12 +1,12 @@
// Would this be better?
//#define DBG(x) if (0) { x }
-#define DBG if(0)
-#define DBG_PARSER if(0)
-#define DBG_RESP if(0)
+#define DBG if(1)
+#define DBG_PARSER if(1)
+#define DBG_RESP if(1)
-#define DBG_MEMCAC if(0)
-#define DBG_MRQ if(0)
+#define DBG_MEMCAC if(1)
+#define DBG_MRQ if(1)
#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)
diff --git a/src/mrhttp/internals/router.c b/src/mrhttp/internals/router.c
index 0c8d487..05dc136 100644
--- a/src/mrhttp/internals/router.c
+++ b/src/mrhttp/internals/router.c
@@ -78,7 +78,7 @@ PyObject *Router_setupRoutes (Router* self) {
PyObject *routes = PyObject_GetAttrString((PyObject*)self, "routes");
int l = PyList_Size(sroutes);
- self->staticRoutes = malloc( l * sizeof(Route) );
+ self->staticRoutes = calloc( l , sizeof(Route) );
self->numStaticRoutes = l;
PyObject *r, *o;
@@ -121,7 +121,7 @@ PyObject *Router_setupRoutes (Router* self) {
else l = 0;
DBG printf(" len routes %d\n", l );
- self->routes = malloc( l * sizeof(Route) );
+ self->routes = calloc( l , sizeof(Route) );
self->numRoutes = l;
rte = self->routes;