more custom routing example and optimizations

This commit is contained in:
roberto@mrspurr
2010-12-05 10:45:13 +01:00
parent 047aac8426
commit fd49ba36eb
8 changed files with 137 additions and 8 deletions
+7
View File
@@ -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
+54
View File
@@ -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, ""},
+30
View File
@@ -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;
}
+5
View File
@@ -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);
+1 -3
View File
@@ -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
+1 -5
View File
@@ -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
+25
View File
@@ -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
+14
View File
@@ -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