add timeout per connection

This commit is contained in:
lxsang 2018-10-16 12:22:13 +02:00
parent 0530317f89
commit 848da534b5
3 changed files with 23 additions and 9 deletions

View File

@ -180,15 +180,15 @@ void *accept_request(void *data)
} }
if (sel == 0 || (!FD_ISSET(client->sock, &read_flags) && !FD_ISSET(client->sock, &write_flags))) if (sel == 0 || (!FD_ISSET(client->sock, &read_flags) && !FD_ISSET(client->sock, &write_flags)))
{ {
if(client->last_wait == 0) client->last_wait = time(NULL);
// retry it later // retry it later
if(client->attempt > MAX_ATTEMPT) if(time(NULL) - client->last_wait > MAX_WAIT_S)
{ {
LOG("Too much attempt for read and write, give up on %d\n", client->sock); LOG("Read and write timeout, give up on %d\n", client->sock);
server_config.connection++; server_config.connection++;
unknow(rq->client); unknow(rq->client);
return task; return task;
} }
client->attempt++;
task->handle = accept_request; task->handle = accept_request;
return task; return task;
} }
@ -208,7 +208,7 @@ void *accept_request(void *data)
case SSL_ERROR_NONE: case SSL_ERROR_NONE:
//LOG("RETRY SSL %d\n", client->sock); //LOG("RETRY SSL %d\n", client->sock);
task->handle = accept_request; task->handle = accept_request;
task->priority = HIGH_PRIORITY; //task->priority = HIGH_PRIORITY;
//task->type = LIGHT; //task->type = LIGHT;
return task; return task;
default: default:
@ -220,19 +220,31 @@ void *accept_request(void *data)
} }
} }
client->status = 1; client->status = 1;
// reset the waiting
client->last_wait = 0;
task->handle = accept_request; task->handle = accept_request;
LOG("Handshake finish for %d\n", client->sock);
return task; return task;
} }
else else
{ {
if (!FD_ISSET(client->sock, &read_flags)) if (!FD_ISSET(client->sock, &read_flags))
{ {
if(client->last_wait == 0) client->last_wait = time(NULL);
if(time(NULL) - client->last_wait > MAX_WAIT_S)
{
server_config.connection++;
unknow(rq->client);
LOG("Read timeout, give up on %d\n", client->sock);
return task;
}
task->handle = accept_request; task->handle = accept_request;
return task; return task;
} }
} }
#endif #endif
client->attempt = 0; LOG("Ready for reading %d\n", client->sock);
client->last_wait = time(NULL);
server_config.connection++; server_config.connection++;
read_buf(rq->client, buf, sizeof(buf)); read_buf(rq->client, buf, sizeof(buf));
line = buf; line = buf;
@ -516,7 +528,7 @@ int startup(unsigned *port)
error_die("getsockname"); error_die("getsockname");
*port = ntohs(name.sin_port); *port = ntohs(name.sin_port);
} }
printf("back log is %d\n", server_config.backlog); LOG("back log is %d\n", server_config.backlog);
if (listen(httpd, server_config.backlog) < 0) if (listen(httpd, server_config.backlog) < 0)
error_die("listen"); error_die("listen");
return (httpd); return (httpd);
@ -604,7 +616,9 @@ void *decode_request_header(void *data)
//if(line) free(line); //if(line) free(line);
memset(buf, 0, sizeof(buf)); memset(buf, 0, sizeof(buf));
strcat(buf, url); strcat(buf, url);
LOG("Original query: %s\n", url);
query = apply_rules(host, buf); query = apply_rules(host, buf);
LOG("Processed query: %s\n", query);
dput(rq->request, "RESOURCE_PATH", url_decode(buf)); dput(rq->request, "RESOURCE_PATH", url_decode(buf));
if (query) if (query)
{ {

View File

@ -188,7 +188,7 @@ int main(int argc, char* argv[])
#ifdef USE_OPENSSL #ifdef USE_OPENSSL
client->ssl = NULL; client->ssl = NULL;
client->status = 0; client->status = 0;
client->attempt = 0; client->last_wait = 0;
if(config()->usessl == 1) if(config()->usessl == 1)
{ {
client->ssl = (void*)SSL_new(ctx); client->ssl = (void*)SSL_new(ctx);

View File

@ -26,7 +26,7 @@
#define __RESULT__ "{\"result\":%d,\"msg\":\"%s\"}" #define __RESULT__ "{\"result\":%d,\"msg\":\"%s\"}"
#define FORM_URL_ENCODE "application/x-www-form-urlencoded" #define FORM_URL_ENCODE "application/x-www-form-urlencoded"
#define FORM_MULTI_PART "multipart/form-data" #define FORM_MULTI_PART "multipart/form-data"
#define MAX_ATTEMPT 1000 #define MAX_WAIT_S 20 // 1/3 minute
#ifdef USE_OPENSSL #ifdef USE_OPENSSL
int __attribute__((weak)) usessl(); int __attribute__((weak)) usessl();
#endif #endif
@ -38,7 +38,7 @@ typedef struct{
#ifdef USE_OPENSSL #ifdef USE_OPENSSL
int status; int status;
#endif #endif
int attempt; time_t last_wait;
} antd_client_t; } antd_client_t;
typedef struct { typedef struct {