big code refactoring

This commit is contained in:
roberto@sirius
2009-12-28 11:20:34 +01:00
parent 18efb257eb
commit eeb299d743
5 changed files with 433 additions and 447 deletions
+20
View File
@@ -364,6 +364,11 @@ static int uwsgi_handler(request_rec *r) {
uwsgi_poll.events = POLLIN ;
/* neede to set the right http status code in logs */
char uwsgi_http_status[13] ;
int uwsgi_http_status_read = 0;
uwsgi_http_status[13] = 0 ;
for(;;) {
/* put -1 to disable timeout on zero */
cnt = poll(&uwsgi_poll, 1, (c->socket_timeout*1000)-1) ;
@@ -374,6 +379,16 @@ static int uwsgi_handler(request_rec *r) {
else if (cnt > 0) {
cnt = recv(uwsgi_poll.fd, buf, 4096, 0) ;
if (cnt > 0) {
if (uwsgi_http_status_read < 12) {
if (uwsgi_http_status_read + cnt >= 12) {
memcpy(uwsgi_http_status+uwsgi_http_status_read, buf, 12-uwsgi_http_status_read);
r->status = atoi(uwsgi_http_status+8);
}
else {
memcpy(uwsgi_http_status+uwsgi_http_status_read, buf, cnt);
uwsgi_http_status_read+=cnt;
}
}
apr_brigade_write(bb, NULL, NULL, buf, cnt);
}
else if (cnt == 0) {
@@ -391,6 +406,11 @@ static int uwsgi_handler(request_rec *r) {
close(uwsgi_poll.fd);
/* empty response return 500 */
if (uwsgi_http_status_read == 0) {
return HTTP_INTERNAL_SERVER_ERROR;
}
return ap_pass_brigade(r->output_filters, bb);;
}
+4 -5
View File
@@ -1,7 +1,6 @@
#include "uwsgi.h"
extern struct uwsgi_worker *workers;
extern int mywid ;
extern struct uwsgi_server uwsgi;
#ifdef __BIG_ENDIAN__
uint16_t uwsgi_swap16(uint16_t x) {
@@ -10,12 +9,12 @@ uint16_t uwsgi_swap16(uint16_t x) {
#endif
void set_harakiri(int sec) {
if (workers) {
if (uwsgi.workers) {
if (sec == 0) {
workers[mywid].harakiri = 0 ;
uwsgi.workers[uwsgi.mywid].harakiri = 0 ;
}
else {
workers[mywid].harakiri = time(NULL) + sec ;
uwsgi.workers[uwsgi.mywid].harakiri = time(NULL) + sec ;
}
}
else {
+273 -380
View File
File diff suppressed because it is too large Load Diff
+104 -21
View File
@@ -94,6 +94,106 @@
#define UWSGI_RELOAD_CODE 17
#define UWSGI_END_CODE 30
#define MAX_VARS 64
struct uwsgi_app {
#ifndef ROCK_SOLID
PyThreadState *interpreter ;
PyObject *pymain_dict ;
#endif
#ifdef ROCK_SOLID
PyObject *wsgi_module;
PyObject *wsgi_dict;
#endif
PyObject *wsgi_callable ;
PyObject *wsgi_environ ;
PyObject *wsgi_args;
PyObject *wsgi_harakiri;
#ifndef ROCK_SOLID
PyObject *wsgi_sendfile;
PyObject *wsgi_cprofile_run;
int requests ;
#endif
};
struct uwsgi_server {
char *pyhome;
int requests;
#ifndef ROCK_SOLID
int has_threads;
int wsgi_cnt;
int default_app;
int enable_profiler;
#endif
int manage_next_request;
int in_request;
int buffer_size;
char *test_module;
int numproc;
int max_vars ;
int vec_size ;
char *sharedarea ;
#ifndef __OpenBSD__
void *sharedareamutex ;
#endif
int sharedareasize ;
/* the list of workers */
struct uwsgi_worker *workers ;
pid_t mypid;
int mywid;
struct timeval start_tv;
#ifndef UNBIT
#ifndef ROCK_SOLID
int cgi_mode;
#endif
int abstract_socket;
int chmod_socket;
int listen_queue;
#ifndef ROCK_SOLID
char *xml_config;
char *python_path[64];
int python_path_cnt;
#endif
#endif
#ifndef ROCK_SOLID
char *wsgi_config;
#endif
#ifndef ROCK_SOLID
int single_interpreter;
int py_optimize;
PyObject *py_sendfile ;
PyObject *fastfuncslist ;
PyThreadState *main_thread ;
#endif
struct pollfd poll;
int harakiri_timeout;
int socket_timeout;
#ifndef ROCK_SOLID
int memory_debug;
#endif
#ifndef ROCK_SOLID
struct uwsgi_app wsgi_apps[64];
PyObject *py_apps;
#endif
};
struct __attribute__((packed)) uwsgi_worker {
pid_t pid;
@@ -150,27 +250,6 @@ struct __attribute__((packed)) wsgi_request {
};
struct uwsgi_app {
#ifndef ROCK_SOLID
PyThreadState *interpreter ;
PyObject *pymain_dict ;
#endif
#ifdef ROCK_SOLID
PyObject *wsgi_module;
PyObject *wsgi_dict;
#endif
PyObject *wsgi_callable ;
PyObject *wsgi_environ ;
PyObject *wsgi_args;
PyObject *wsgi_harakiri;
#ifndef ROCK_SOLID
PyObject *wsgi_sendfile;
PyObject *wsgi_cprofile_run;
int requests ;
#endif
};
char *uwsgi_get_cwd(void);
@@ -220,3 +299,7 @@ void set_harakiri(int);
#ifdef __BIG_ENDIAN__
uint16_t uwsgi_swap16( uint16_t );
#endif
#ifndef ROCK_SOLID
int init_uwsgi_app(PyObject *, PyObject *) ;
#endif
+32 -41
View File
@@ -1,19 +1,16 @@
#include "uwsgi.h"
extern char *sharedarea ;
extern void *sharedareamutex ;
extern int sharedareasize ;
char *spool_buffer = NULL ;
extern int buffer_size ;
extern struct uwsgi_server uwsgi;
#ifdef __APPLE__
#define LOCK_SHAREDAREA OSSpinLockLock((OSSpinLock *) sharedareamutex);
#define UNLOCK_SHAREDAREA OSSpinLockUnlock((OSSpinLock *) sharedareamutex);
#define LOCK_SHAREDAREA OSSpinLockLock((OSSpinLock *) uwsgi.sharedareamutex);
#define UNLOCK_SHAREDAREA OSSpinLockUnlock((OSSpinLock *) uwsgi.sharedareamutex);
#else
#ifndef __OpenBSD__
#define LOCK_SHAREDAREA pthread_mutex_lock((pthread_mutex_t *) sharedareamutex + sizeof(pthread_mutexattr_t));
#define UNLOCK_SHAREDAREA pthread_mutex_unlock((pthread_mutex_t *) sharedareamutex + sizeof(pthread_mutexattr_t));
#define LOCK_SHAREDAREA pthread_mutex_lock((pthread_mutex_t *) uwsgi.sharedareamutex + sizeof(pthread_mutexattr_t));
#define UNLOCK_SHAREDAREA pthread_mutex_unlock((pthread_mutex_t *) uwsgi.sharedareamutex + sizeof(pthread_mutexattr_t));
#else
#define LOCK_SHAREDAREA
#define UNLOCK_SHAREDAREA
@@ -25,7 +22,7 @@ PyObject *py_uwsgi_sharedarea_inclong(PyObject *self, PyObject *args) {
int pos = 0 ;
long value ;
if (sharedareasize <= 0) {
if (uwsgi.sharedareasize <= 0) {
Py_INCREF(Py_None);
return Py_None;
}
@@ -38,15 +35,15 @@ PyObject *py_uwsgi_sharedarea_inclong(PyObject *self, PyObject *args) {
pos = PyInt_AsLong(arg0);
if (pos+4 >= getpagesize()*sharedareasize) {
if (pos+4 >= getpagesize()*uwsgi.sharedareasize) {
Py_INCREF(Py_None);
return Py_None;
}
LOCK_SHAREDAREA
memcpy(&value, sharedarea+pos, 4);
memcpy(&value, uwsgi.sharedarea+pos, 4);
value++;
memcpy(sharedarea+pos, &value,4);
memcpy(uwsgi.sharedarea+pos, &value,4);
UNLOCK_SHAREDAREA
return PyInt_FromLong(value);
@@ -58,7 +55,7 @@ PyObject *py_uwsgi_sharedarea_writelong(PyObject *self, PyObject *args) {
int pos = 0 ;
long value ;
if (sharedareasize <= 0) {
if (uwsgi.sharedareasize <= 0) {
Py_INCREF(Py_None);
return Py_None;
}
@@ -71,7 +68,7 @@ PyObject *py_uwsgi_sharedarea_writelong(PyObject *self, PyObject *args) {
pos = PyInt_AsLong(arg0);
if (pos+4 >= getpagesize()*sharedareasize) {
if (pos+4 >= getpagesize()*uwsgi.sharedareasize) {
Py_INCREF(Py_None);
return Py_None;
}
@@ -84,7 +81,7 @@ PyObject *py_uwsgi_sharedarea_writelong(PyObject *self, PyObject *args) {
LOCK_SHAREDAREA
value = (long) PyInt_AsLong(arg1);
memcpy(sharedarea+pos, &value,4);
memcpy(uwsgi.sharedarea+pos, &value,4);
UNLOCK_SHAREDAREA
return PyInt_FromLong(value);
@@ -96,7 +93,7 @@ PyObject *py_uwsgi_sharedarea_write(PyObject *self, PyObject *args) {
int pos = 0 ;
char *value ;
if (sharedareasize <= 0) {
if (uwsgi.sharedareasize <= 0) {
Py_INCREF(Py_None);
return Py_None;
}
@@ -118,13 +115,13 @@ PyObject *py_uwsgi_sharedarea_write(PyObject *self, PyObject *args) {
value = PyString_AsString(arg1);
if (pos+strlen(value) >= getpagesize()*sharedareasize) {
if (pos+strlen(value) >= getpagesize()*uwsgi.sharedareasize) {
Py_INCREF(Py_None);
return Py_None;
}
LOCK_SHAREDAREA
memcpy(sharedarea+pos, value,strlen(value));
memcpy(uwsgi.sharedarea+pos, value,strlen(value));
UNLOCK_SHAREDAREA
return PyInt_FromLong(strlen(value));
@@ -136,7 +133,7 @@ PyObject *py_uwsgi_sharedarea_writebyte(PyObject *self, PyObject *args) {
int pos = 0 ;
char value ;
if (sharedareasize <= 0) {
if (uwsgi.sharedareasize <= 0) {
Py_INCREF(Py_None);
return Py_None;
}
@@ -149,7 +146,7 @@ PyObject *py_uwsgi_sharedarea_writebyte(PyObject *self, PyObject *args) {
pos = PyInt_AsLong(arg0);
if (pos >= getpagesize()*sharedareasize) {
if (pos >= getpagesize()*uwsgi.sharedareasize) {
Py_INCREF(Py_None);
return Py_None;
}
@@ -161,9 +158,9 @@ PyObject *py_uwsgi_sharedarea_writebyte(PyObject *self, PyObject *args) {
}
value = (char) PyInt_AsLong(arg1);
sharedarea[pos] = value;
uwsgi.sharedarea[pos] = value;
return PyInt_FromLong(sharedarea[pos]);
return PyInt_FromLong(uwsgi.sharedarea[pos]);
}
@@ -172,7 +169,7 @@ PyObject *py_uwsgi_sharedarea_readlong(PyObject *self, PyObject *args) {
int pos = 0 ;
long value ;
if (sharedareasize <= 0) {
if (uwsgi.sharedareasize <= 0) {
Py_INCREF(Py_None);
return Py_None;
}
@@ -185,13 +182,13 @@ PyObject *py_uwsgi_sharedarea_readlong(PyObject *self, PyObject *args) {
pos = PyInt_AsLong(arg0);
if (pos+4 >= getpagesize()*sharedareasize) {
if (pos+4 >= getpagesize()*uwsgi.sharedareasize) {
Py_INCREF(Py_None);
return Py_None;
}
LOCK_SHAREDAREA
memcpy(&value, sharedarea+pos, 4);
memcpy(&value, uwsgi.sharedarea+pos, 4);
UNLOCK_SHAREDAREA
return PyInt_FromLong(value);
@@ -203,7 +200,7 @@ PyObject *py_uwsgi_sharedarea_readbyte(PyObject *self, PyObject *args) {
PyObject *arg0 ;
int pos = 0 ;
if (sharedareasize <= 0) {
if (uwsgi.sharedareasize <= 0) {
Py_INCREF(Py_None);
return Py_None;
}
@@ -216,12 +213,12 @@ PyObject *py_uwsgi_sharedarea_readbyte(PyObject *self, PyObject *args) {
pos = PyInt_AsLong(arg0);
if (pos >= getpagesize()*sharedareasize) {
if (pos >= getpagesize()*uwsgi.sharedareasize) {
Py_INCREF(Py_None);
return Py_None;
}
return PyInt_FromLong(sharedarea[pos]);
return PyInt_FromLong(uwsgi.sharedarea[pos]);
}
@@ -231,7 +228,7 @@ PyObject *py_uwsgi_sharedarea_read(PyObject *self, PyObject *args) {
int len = 1 ;
int pos = 0 ;
if (sharedareasize <= 0) {
if (uwsgi.sharedareasize <= 0) {
Py_INCREF(Py_None);
return Py_None;
}
@@ -248,18 +245,17 @@ PyObject *py_uwsgi_sharedarea_read(PyObject *self, PyObject *args) {
pos = PyInt_AsLong(arg0);
if (pos+len >= getpagesize()*sharedareasize) {
if (pos+len >= getpagesize()*uwsgi.sharedareasize) {
Py_INCREF(Py_None);
return Py_None;
}
return PyString_FromStringAndSize(sharedarea+pos, len);
return PyString_FromStringAndSize(uwsgi.sharedarea+pos, len);
}
PyObject *py_uwsgi_send_spool(PyObject *self, PyObject *args) {
PyObject *spool_dict, *spool_vars ;
PyObject *zero, *key, *val;
extern int requests ;
uint16_t keysize, valsize ;
char *cur_buf ;
int i ;
@@ -291,7 +287,7 @@ PyObject *py_uwsgi_send_spool(PyObject *self, PyObject *args) {
keysize = PyString_Size(key) ;
valsize= PyString_Size(val) ;
if (cur_buf + keysize + 2 + valsize + 2 <= spool_buffer + buffer_size) {
if (cur_buf + keysize + 2 + valsize + 2 <= spool_buffer + uwsgi.buffer_size) {
#ifdef __BIG_ENDIAN__
keysize = uwsgi_swap16(keysize);
@@ -338,7 +334,7 @@ PyObject *py_uwsgi_send_spool(PyObject *self, PyObject *args) {
}
}
i = spool_request(spool_filename, requests+1, spool_buffer, cur_buf - spool_buffer) ;
i = spool_request(spool_filename, uwsgi.requests+1, spool_buffer, cur_buf - spool_buffer) ;
if (i > 0) {
return Py_True;
}
@@ -407,7 +403,7 @@ void init_uwsgi_module_spooler(PyObject *current_uwsgi_module) {
exit(1);
}
spool_buffer = malloc(buffer_size);
spool_buffer = malloc(uwsgi.buffer_size);
if (!spool_buffer) {
perror("malloc()");
exit(1);
@@ -454,8 +450,3 @@ void init_uwsgi_module_sharedarea(PyObject *current_uwsgi_module) {
Py_DECREF(func);
}
}
PyMethodDef null_methods[] = {
{NULL, NULL},
};