From fd49ba36ebfc19f831f039474e902b794230859c Mon Sep 17 00:00:00 2001 From: "roberto@mrspurr" Date: Sun, 5 Dec 2010 10:45:13 +0100 Subject: [PATCH] more custom routing example and optimizations --- master.c | 7 +++++ plugins/python/uwsgi_pymodule.c | 54 +++++++++++++++++++++++++++++++++ uwsgi.c | 30 ++++++++++++++++++ uwsgi.h | 5 +++ uwsgirouter.py | 4 +-- uwsgirouter2.py | 6 +--- uwsgirouter3.py | 25 +++++++++++++++ uwsgirouter4.py | 14 +++++++++ 8 files changed, 137 insertions(+), 8 deletions(-) create mode 100644 uwsgirouter3.py create mode 100644 uwsgirouter4.py diff --git a/master.c b/master.c index fb21beb3..a895df75 100644 --- a/master.c +++ b/master.c @@ -494,6 +494,13 @@ void master_loop(char **argv, char **environ) { } } } + else if (ucn->name[0] != 0 && ucn->type == CLUSTER_NODE_DYNAMIC) { + // if the last_seen attr is higher than 30 secs ago, mark the node as dead + if ( (time(NULL) - ucn->last_seen) > 30) { + uwsgi_log_verbose("no presence announce in the last 30 seconds by node %s, i assume it is dead.\n", ucn->name); + ucn->name[0] = 0 ; + } + } } // reannounce myself every 10 cycles diff --git a/plugins/python/uwsgi_pymodule.c b/plugins/python/uwsgi_pymodule.c index 1d5e19c2..642c0491 100644 --- a/plugins/python/uwsgi_pymodule.c +++ b/plugins/python/uwsgi_pymodule.c @@ -983,6 +983,7 @@ typedef struct { PyObject_HEAD int fd; int timeout; + int close; } uwsgi_Iter; @@ -1012,6 +1013,10 @@ PyObject* uwsgi_Iter_next(PyObject *self) { uwsgi_log("uwsgi request timed out waiting for response\n"); } + if (ui->close) { + close(ui->fd); + } + UWSGI_GET_GIL PyErr_SetNone(PyExc_StopIteration); @@ -1190,6 +1195,7 @@ PyObject *py_uwsgi_send_message(PyObject * self, PyObject * args) { ui->fd = uwsgi_fd; ui->timeout = timeout; + ui->close = close_fd; return (PyObject *) ui; @@ -1353,6 +1359,14 @@ clear: } +PyObject *py_uwsgi_cl(PyObject * self, PyObject * args) { + + struct wsgi_request *wsgi_req = current_wsgi_req(); + + return PyLong_FromLong(wsgi_req->post_cl); + +} + PyObject *py_uwsgi_disconnect(PyObject * self, PyObject * args) { struct wsgi_request *wsgi_req = current_wsgi_req(); @@ -1540,6 +1554,43 @@ PyObject *py_uwsgi_suspend(PyObject * self, PyObject * args) { } +#ifdef UWSGI_MULTICAST +PyObject *py_uwsgi_cluster_nodes(PyObject * self, PyObject * args) { + + struct uwsgi_cluster_node *ucn; + int i; + + PyObject *clist = PyList_New(0); + + for (i = 0; i < MAX_CLUSTER_NODES; i++) { + ucn = &uwsgi.shared->nodes[i]; + if (ucn->name[0] != 0) { + if (ucn->status == UWSGI_NODE_OK) { + PyList_Append(clist, PyString_FromString(ucn->name)); + } + } + } + + return clist; + +} + +PyObject *py_uwsgi_cluster_best_node(PyObject * self, PyObject * args) { + + char *node = uwsgi_cluster_best_node() ; + if (node == NULL) goto clear; + if (node[0] == 0) goto clear; + return PyString_FromString(node); + +clear: + + Py_INCREF(Py_None); + return Py_None; +} + + +#endif + static PyMethodDef uwsgi_advanced_methods[] = { {"send_message", py_uwsgi_send_message, METH_VARARGS, ""}, @@ -1562,6 +1613,7 @@ static PyMethodDef uwsgi_advanced_methods[] = { {"lock", py_uwsgi_lock, METH_VARARGS, ""}, {"unlock", py_uwsgi_unlock, METH_VARARGS, ""}, {"send", py_uwsgi_send, METH_VARARGS, ""}, + {"cl", py_uwsgi_cl, METH_VARARGS, ""}, #ifdef UWSGI_SENDFILE {"sendfile", py_uwsgi_advanced_sendfile, METH_VARARGS, ""}, #endif @@ -1571,6 +1623,8 @@ static PyMethodDef uwsgi_advanced_methods[] = { {"logsize", py_uwsgi_logsize, METH_VARARGS, ""}, #ifdef UWSGI_MULTICAST {"send_multicast_message", py_uwsgi_multicast, METH_VARARGS, ""}, + {"cluster_nodes", py_uwsgi_cluster_nodes, METH_VARARGS, ""}, + {"cluster_best_node", py_uwsgi_cluster_best_node, METH_VARARGS, ""}, #endif #ifdef UWSGI_ASYNC {"async_sleep", py_uwsgi_async_sleep, METH_VARARGS, ""}, diff --git a/uwsgi.c b/uwsgi.c index 52cd90aa..1256f0d7 100644 --- a/uwsgi.c +++ b/uwsgi.c @@ -2066,6 +2066,7 @@ void uwsgi_cluster_add_node(char *nodename, int workers, int type) { if (ucn->name[0] != 0) { if (!strcmp(ucn->name, nodename)) { ucn->status = UWSGI_NODE_OK; + ucn->last_seen = time(NULL); return; } } @@ -2363,3 +2364,32 @@ void uwsgi_stdin_sendto(char *socket_name, uint8_t modifier1, uint8_t modifier2) } } + + +char *uwsgi_cluster_best_node() { + + int i; + int best_node = -1; + struct uwsgi_cluster_node *ucn; + + for (i = 0; i < MAX_CLUSTER_NODES; i++) { + ucn = &uwsgi.shared->nodes[i]; + if (ucn->name[0] != 0 && ucn->status == UWSGI_NODE_OK) { + if (best_node == -1) { + best_node = i; + } + else { + if (ucn->last_choosen < uwsgi.shared->nodes[best_node].last_choosen) { + best_node = i; + } + } + } + } + + if (best_node == -1) { + return NULL; + } + + uwsgi.shared->nodes[best_node].last_choosen = time(NULL); + return uwsgi.shared->nodes[best_node].name; +} diff --git a/uwsgi.h b/uwsgi.h index 9184eb2a..1cc60138 100644 --- a/uwsgi.h +++ b/uwsgi.h @@ -824,6 +824,9 @@ struct uwsgi_cluster_node { time_t last_seen; int errors; + + time_t last_choosen; + }; #ifdef UWSGI_SNMP @@ -1183,3 +1186,5 @@ char *generate_socket_name(char *); #define UMIN(a,b) ((a)>(b)?(b):(a)) ssize_t uwsgi_send_message(int, uint8_t, uint8_t, char *, uint16_t, int, size_t, int); + +char *uwsgi_cluster_best_node(void); diff --git a/uwsgirouter.py b/uwsgirouter.py index 23af9d8d..32b56f80 100644 --- a/uwsgirouter.py +++ b/uwsgirouter.py @@ -29,9 +29,7 @@ def application(env, start_response): # uwsgi.recv will use always an internal buffer of 4096, but can be limited in the number of bytes to read # does thir request has a body ? - cl = 0 - if env.has_key('CONTENT_LENGTH'): - cl = int(env['CONTENT_LENGTH']) + cl = uwsgi.cl() if cl > 0: # get the input fd diff --git a/uwsgirouter2.py b/uwsgirouter2.py index c80acb71..430336b7 100644 --- a/uwsgirouter2.py +++ b/uwsgirouter2.py @@ -4,9 +4,5 @@ import uwsgi def application(e,s): - cl = 0 - if e.has_key('CONTENT_LENGTH'): - cl = int(e['CONTENT_LENGTH']) - - for part in uwsgi.send_message("192.168.173.100:3032", 0, 0, e, 0, e['wsgi.input'].fileno(), cl): + for part in uwsgi.send_message("192.168.173.100:3032", 0, 0, e, 0, e['wsgi.input'].fileno(), uwsgi.cl()): yield part diff --git a/uwsgirouter3.py b/uwsgirouter3.py new file mode 100644 index 00000000..a1941c1c --- /dev/null +++ b/uwsgirouter3.py @@ -0,0 +1,25 @@ + +import uwsgi + +current_node = 0 + +def application(e,s): + + global current_node + + nodes = uwsgi.cluster_nodes() + print nodes + + if len(nodes) == 0: + print "no cluster node available" + raise StopIteration + + if current_node >= len(nodes): + current_node = 0 + + node = nodes[current_node] + + for part in uwsgi.send_message(node, 0, 0, e, 0, e['wsgi.input'].fileno(), uwsgi.cl()): + yield part + + current_node+=1 diff --git a/uwsgirouter4.py b/uwsgirouter4.py new file mode 100644 index 00000000..ce8e6fc4 --- /dev/null +++ b/uwsgirouter4.py @@ -0,0 +1,14 @@ + +import uwsgi + +def application(e,s): + + node = uwsgi.cluster_best_node() + print node + + if not node: + print "sorry node unavailable" + raise StopIteration + + for part in uwsgi.send_message(node, 0, 0, e, 0, e['wsgi.input'].fileno(), uwsgi.cl()): + yield part