From 95b157c02c9a4d8c81e92445c154e5f245ddcf8a Mon Sep 17 00:00:00 2001 From: slguan Date: Sat, 20 Jul 2019 14:52:08 +0800 Subject: [PATCH] Fix the issue #127, Check dead http links and close them --- src/modules/http/inc/httpHandle.h | 1 + src/modules/http/src/httpServer.c | 24 +++++++++++++++++++++--- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/modules/http/inc/httpHandle.h b/src/modules/http/inc/httpHandle.h index cf316cd..a2f0f02 100644 --- a/src/modules/http/inc/httpHandle.h +++ b/src/modules/http/inc/httpHandle.h @@ -140,6 +140,7 @@ typedef struct HttpContext { void * signature; int fd; uint32_t accessTimes; + uint32_t lastAccessTime; uint8_t httpVersion : 1; uint8_t httpChunked : 1; uint8_t httpKeepAlive : 2; // http1.0 and not keep-alive, close connection immediately diff --git a/src/modules/http/src/httpServer.c b/src/modules/http/src/httpServer.c index cca854f..79c6a38 100644 --- a/src/modules/http/src/httpServer.c +++ b/src/modules/http/src/httpServer.c @@ -36,6 +36,7 @@ #include "tlog.h" #include "tsocket.h" #include "tutil.h" +#include "ttime.h" #include "http.h" #include "httpCode.h" @@ -70,6 +71,7 @@ HttpContext *httpCreateContext(HttpServer *pServer) { pContext->signature = pContext; pContext->httpVersion = HTTP_VERSION_10; + pContext->lastAccessTime = taosGetTimestampSec(); if (pthread_mutex_init(&(pContext->mutex), NULL) < 0) { httpFreeContext(pServer, pContext); return NULL; @@ -140,6 +142,7 @@ void httpCleanUpContext(HttpThread *pThread, HttpContext *pContext) { bool httpInitContext(HttpContext *pContext) { pContext->accessTimes++; + pContext->lastAccessTime = taosGetTimestampSec(); pContext->httpVersion = HTTP_VERSION_10; pContext->httpKeepAlive = HTTP_KEEPALIVE_NO_INPUT; pContext->httpChunked = HTTP_UNCUNKED; @@ -216,12 +219,9 @@ void httpCleanUpConnect(HttpServer *pServer) { pThread = pServer->pThreads + i; taosCloseSocket(pThread->pollFd); - pthread_mutex_lock(&pThread->threadMutex); while (pThread->pHead) { httpCleanUpContext(pThread, pThread->pHead); - pThread->pHead = pThread->pHead; } - pthread_mutex_unlock(&pThread->threadMutex); pthread_cancel(pThread->thread); pthread_join(pThread->thread, NULL); @@ -233,6 +233,20 @@ void httpCleanUpConnect(HttpServer *pServer) { httpTrace("http server:%s is cleaned up", pServer->label); } +void httpCloseDeadConnects(HttpThread *pThread) { + int32_t thresholdSec = taosGetTimestampSec() - 3600; + HttpContext *pContext = (HttpContext*)pThread->pHead; + while (pContext != NULL && pContext == pContext->signature) { + HttpContext *pContextNext = pContext->next; + if (pContext->lastAccessTime < thresholdSec) { + httpPrint("context:%p, fd:%d, ip:%s, lastAccessTime:%d smaller then threshold:%d, so close it", + pContext, pContext->fd, pContext->ipstr, pContext->lastAccessTime, thresholdSec); + httpCloseContextByServer(pThread, pContext); + } + pContext = pContextNext; + } +} + // read all the data, then just discard it void httpReadDirtyData(int fd) { char data[1024] = {0}; @@ -392,6 +406,7 @@ void httpAcceptHttpConnection(void *arg) { struct sockaddr_in clientAddr; int sockFd; int threadId = 0; + int connThreshold = 2 * tsHttpCacheSessions / tsHttpMaxThreads; HttpThread * pThread; HttpServer * pServer; HttpContext * pContext; @@ -475,6 +490,9 @@ void httpAcceptHttpConnection(void *arg) { pContext, connFd, inet_ntoa(clientAddr.sin_addr), htons(clientAddr.sin_port), pThread->label, pThread->numOfFds); + if (pThread->numOfFds > connThreshold) { + httpCloseDeadConnects(pThread); + } // pick up next thread for next connection threadId++; threadId = threadId % pServer->numOfThreads; -- GitLab