Improved HTTP Proxy time measurement and behaviour.

This commit is contained in:
Thulinma 2012-09-08 13:26:12 +02:00
parent 97d7e74b89
commit 1f255ec7a4

View file

@ -11,6 +11,7 @@
#include <sys/wait.h> #include <sys/wait.h>
#include <getopt.h> #include <getopt.h>
#include <ctime> #include <ctime>
#include <sys/time.h>//for gettimeofday
#include <set> #include <set>
#include <openssl/md5.h> #include <openssl/md5.h>
#include <mist/socket.h> #include <mist/socket.h>
@ -199,7 +200,6 @@ namespace Connector_HTTP{
H.SetHeader("X-Origin", conn->getHost());//add the UID to the headers before copying H.SetHeader("X-Origin", conn->getHost());//add the UID to the headers before copying
std::string request = H.BuildRequest();//copy the request for later forwarding to the connector std::string request = H.BuildRequest();//copy the request for later forwarding to the connector
std::string orig_url = H.getUrl(); std::string orig_url = H.getUrl();
int starttime = time(0);
H.Clean(); H.Clean();
//check if a connection exists, and if not create one //check if a connection exists, and if not create one
@ -278,10 +278,6 @@ namespace Connector_HTTP{
Socket::Connection * myConn = connconn[uid]->conn; Socket::Connection * myConn = connconn[uid]->conn;
connconn[uid]->conn = new Socket::Connection(); connconn[uid]->conn = new Socket::Connection();
connconn[uid]->in_use.unlock(); connconn[uid]->in_use.unlock();
//print some debug info
#if DEBUG >= 4
std::cout << "Handling request: " << orig_url << " => " << connector << " (" << uid << ")" << std::endl;
#endif
//continue sending data from this socket and keep it permanently in use //continue sending data from this socket and keep it permanently in use
while (myConn->connected() && conn->connected()){ while (myConn->connected() && conn->connected()){
if (myConn->spool()){ if (myConn->spool()){
@ -289,16 +285,14 @@ namespace Connector_HTTP{
conn->Send(myConn->Received()); conn->Send(myConn->Received());
myConn->Received().clear(); myConn->Received().clear();
conn->flush(); conn->flush();
}else{
usleep(30000);
} }
usleep(30000);
} }
myConn->close(); myConn->close();
delete myConn; delete myConn;
conn->close();
} }
//print some debug info
#if DEBUG >= 4
std::cout << "Finished request: " << orig_url << " => " << connector << " (" << uid << ") in " << (time(0) - starttime) << "s" << std::endl;
#endif
} }
} }
@ -331,17 +325,25 @@ namespace Connector_HTTP{
return "none"; return "none";
} }
/// Gets the current system time in milliseconds.
long long int getNowMS(){
timeval t;
gettimeofday(&t, 0);
return t.tv_sec * 1000 + t.tv_usec/1000;
}//getNowMS
/// Thread for handling a single HTTP connection /// Thread for handling a single HTTP connection
void Handle_HTTP_Connection(void * pointer){ void Handle_HTTP_Connection(void * pointer){
Socket::Connection * conn = (Socket::Connection *)pointer; Socket::Connection * conn = (Socket::Connection *)pointer;
conn->setBlocking(false);//do not block on conn.spool() when no data is available conn->setBlocking(false);//do not block on conn.spool() when no data is available
HTTP::Parser Client; HTTP::Parser Client;
while (conn->connected()){ while (conn->connected()){
if (conn->spool()){ if (conn->spool() || !conn->Received().empty()){
if (Client.Read(conn->Received())){ if (Client.Read(conn->Received())){
std::string handler = getHTTPType(Client); std::string handler = getHTTPType(Client);
long long int startms = getNowMS();
#if DEBUG >= 4 #if DEBUG >= 4
std::cout << "Received request: " << Client.getUrl() << " => " << handler << " (" << Client.GetVar("stream") << ")" << std::endl; std::cout << "Received request: " << Client.getUrl() << " (" << conn->getSocket() << ") => " << handler << " (" << Client.GetVar("stream") << ")" << std::endl;
#endif #endif
if (handler == "none" || handler == "internal"){ if (handler == "none" || handler == "internal"){
if (handler == "internal"){ if (handler == "internal"){
@ -352,11 +354,15 @@ namespace Connector_HTTP{
}else{ }else{
Handle_Through_Connector(Client, conn, handler); Handle_Through_Connector(Client, conn, handler);
} }
#if DEBUG >= 4
std::cout << "Completed request (" << conn->getSocket() << ") " << handler << " in " << (getNowMS() - startms) << " ms" << std::endl;
#endif
Client.Clean(); //clean for any possible next requests Client.Clean(); //clean for any possible next requests
}else{ }else{
#if DEBUG >= 3 #if DEBUG >= 3
fprintf(stderr, "Could not parse the following:\n%s\n", conn->Received().c_str()); fprintf(stderr, "Could not parse the following:\n%s\n", conn->Received().c_str());
#endif #endif
usleep(100000);//sleep 100ms
} }
}else{ }else{
usleep(10000);//sleep 10ms usleep(10000);//sleep 10ms