提交 95b157c0 编写于 作者: S slguan

Fix the issue #127, Check dead http links and close them

上级 412db1ab
...@@ -140,6 +140,7 @@ typedef struct HttpContext { ...@@ -140,6 +140,7 @@ typedef struct HttpContext {
void * signature; void * signature;
int fd; int fd;
uint32_t accessTimes; uint32_t accessTimes;
uint32_t lastAccessTime;
uint8_t httpVersion : 1; uint8_t httpVersion : 1;
uint8_t httpChunked : 1; uint8_t httpChunked : 1;
uint8_t httpKeepAlive : 2; // http1.0 and not keep-alive, close connection immediately uint8_t httpKeepAlive : 2; // http1.0 and not keep-alive, close connection immediately
......
...@@ -36,6 +36,7 @@ ...@@ -36,6 +36,7 @@
#include "tlog.h" #include "tlog.h"
#include "tsocket.h" #include "tsocket.h"
#include "tutil.h" #include "tutil.h"
#include "ttime.h"
#include "http.h" #include "http.h"
#include "httpCode.h" #include "httpCode.h"
...@@ -70,6 +71,7 @@ HttpContext *httpCreateContext(HttpServer *pServer) { ...@@ -70,6 +71,7 @@ HttpContext *httpCreateContext(HttpServer *pServer) {
pContext->signature = pContext; pContext->signature = pContext;
pContext->httpVersion = HTTP_VERSION_10; pContext->httpVersion = HTTP_VERSION_10;
pContext->lastAccessTime = taosGetTimestampSec();
if (pthread_mutex_init(&(pContext->mutex), NULL) < 0) { if (pthread_mutex_init(&(pContext->mutex), NULL) < 0) {
httpFreeContext(pServer, pContext); httpFreeContext(pServer, pContext);
return NULL; return NULL;
...@@ -140,6 +142,7 @@ void httpCleanUpContext(HttpThread *pThread, HttpContext *pContext) { ...@@ -140,6 +142,7 @@ void httpCleanUpContext(HttpThread *pThread, HttpContext *pContext) {
bool httpInitContext(HttpContext *pContext) { bool httpInitContext(HttpContext *pContext) {
pContext->accessTimes++; pContext->accessTimes++;
pContext->lastAccessTime = taosGetTimestampSec();
pContext->httpVersion = HTTP_VERSION_10; pContext->httpVersion = HTTP_VERSION_10;
pContext->httpKeepAlive = HTTP_KEEPALIVE_NO_INPUT; pContext->httpKeepAlive = HTTP_KEEPALIVE_NO_INPUT;
pContext->httpChunked = HTTP_UNCUNKED; pContext->httpChunked = HTTP_UNCUNKED;
...@@ -216,12 +219,9 @@ void httpCleanUpConnect(HttpServer *pServer) { ...@@ -216,12 +219,9 @@ void httpCleanUpConnect(HttpServer *pServer) {
pThread = pServer->pThreads + i; pThread = pServer->pThreads + i;
taosCloseSocket(pThread->pollFd); taosCloseSocket(pThread->pollFd);
pthread_mutex_lock(&pThread->threadMutex);
while (pThread->pHead) { while (pThread->pHead) {
httpCleanUpContext(pThread, pThread->pHead); httpCleanUpContext(pThread, pThread->pHead);
pThread->pHead = pThread->pHead;
} }
pthread_mutex_unlock(&pThread->threadMutex);
pthread_cancel(pThread->thread); pthread_cancel(pThread->thread);
pthread_join(pThread->thread, NULL); pthread_join(pThread->thread, NULL);
...@@ -233,6 +233,20 @@ void httpCleanUpConnect(HttpServer *pServer) { ...@@ -233,6 +233,20 @@ void httpCleanUpConnect(HttpServer *pServer) {
httpTrace("http server:%s is cleaned up", pServer->label); 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 // read all the data, then just discard it
void httpReadDirtyData(int fd) { void httpReadDirtyData(int fd) {
char data[1024] = {0}; char data[1024] = {0};
...@@ -392,6 +406,7 @@ void httpAcceptHttpConnection(void *arg) { ...@@ -392,6 +406,7 @@ void httpAcceptHttpConnection(void *arg) {
struct sockaddr_in clientAddr; struct sockaddr_in clientAddr;
int sockFd; int sockFd;
int threadId = 0; int threadId = 0;
int connThreshold = 2 * tsHttpCacheSessions / tsHttpMaxThreads;
HttpThread * pThread; HttpThread * pThread;
HttpServer * pServer; HttpServer * pServer;
HttpContext * pContext; HttpContext * pContext;
...@@ -475,6 +490,9 @@ void httpAcceptHttpConnection(void *arg) { ...@@ -475,6 +490,9 @@ void httpAcceptHttpConnection(void *arg) {
pContext, connFd, inet_ntoa(clientAddr.sin_addr), htons(clientAddr.sin_port), pThread->label, pContext, connFd, inet_ntoa(clientAddr.sin_addr), htons(clientAddr.sin_port), pThread->label,
pThread->numOfFds); pThread->numOfFds);
if (pThread->numOfFds > connThreshold) {
httpCloseDeadConnects(pThread);
}
// pick up next thread for next connection // pick up next thread for next connection
threadId++; threadId++;
threadId = threadId % pServer->numOfThreads; threadId = threadId % pServer->numOfThreads;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册