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(client->last_wait == 0) client->last_wait = time(NULL);
// 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++;
unknow(rq->client);
return task;
}
client->attempt++;
task->handle = accept_request;
return task;
}
@ -208,7 +208,7 @@ void *accept_request(void *data)
case SSL_ERROR_NONE:
//LOG("RETRY SSL %d\n", client->sock);
task->handle = accept_request;
task->priority = HIGH_PRIORITY;
//task->priority = HIGH_PRIORITY;
//task->type = LIGHT;
return task;
default:
@ -220,19 +220,31 @@ void *accept_request(void *data)
}
}
client->status = 1;
// reset the waiting
client->last_wait = 0;
task->handle = accept_request;
LOG("Handshake finish for %d\n", client->sock);
return task;
}
else
{
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;
return task;
}
}
#endif
client->attempt = 0;
LOG("Ready for reading %d\n", client->sock);
client->last_wait = time(NULL);
server_config.connection++;
read_buf(rq->client, buf, sizeof(buf));
line = buf;
@ -516,7 +528,7 @@ int startup(unsigned *port)
error_die("getsockname");
*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)
error_die("listen");
return (httpd);
@ -604,7 +616,9 @@ void *decode_request_header(void *data)
//if(line) free(line);
memset(buf, 0, sizeof(buf));
strcat(buf, url);
LOG("Original query: %s\n", url);
query = apply_rules(host, buf);
LOG("Processed query: %s\n", query);
dput(rq->request, "RESOURCE_PATH", url_decode(buf));
if (query)
{

View File

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

View File

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