Skip to content

Commit

Permalink
fix http2 multi thread bug
Browse files Browse the repository at this point in the history
  • Loading branch information
jerrylucky committed Feb 17, 2020
1 parent 8f3c4cd commit 0107665
Show file tree
Hide file tree
Showing 8 changed files with 690 additions and 422 deletions.
114 changes: 70 additions & 44 deletions examples/HttpDemo/Http2Server/Http2Imp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ using namespace std;

TC_SpinLock Http2Imp::_mutex;

unordered_map<int32_t, TC_Http2Server*> Http2Imp::_http2;
unordered_map<int32_t, shared_ptr<TC_Http2Server>> Http2Imp::_http2;

//////////////////////////////////////////////////////
void Http2Imp::initialize()
Expand All @@ -36,52 +36,77 @@ void Http2Imp::destroy()
//destroy servant here:
//...
}

void doRequestFunc(const TC_Http2Server::Req_Type reqtype, const string &requri, const TC_Http::http_header_type &reqHeader, const string &reqBody, TC_Http2Server::Http2Response &rsp)
{
rsp.status = 200;
rsp.about = "OK";
rsp.body = "response helloworld 2";
}
//
//void doRequestFunc(const TC_Http2Server::Req_Type reqtype, const string &requri, const TC_Http::http_header_type &reqHeader, const string &reqBody, TC_Http2Server::Http2Response &rsp)
//{
// rsp.status = 200;
// rsp.about = "OK";
// rsp.body = "response helloworld 2";
//}

int Http2Imp::doRequest(TarsCurrentPtr current, vector<char> &buffer)
{

TC_Http2Server* session = getHttp2(current->getUId());

static bool flag = true;
if(flag)
{
//method 1:
vector<int32_t> vtReqid;
TC_Http2Server::doRequest(current->getRequestBuffer(), vtReqid);

// cout << "doRequest size:" << vtReqid.size() << endl;

TC_Http2Server::Http2Response rsp;
rsp.status = 200;
rsp.about = "OK";
rsp.body = "response helloworld 1";

for(size_t i = 0; i < vtReqid.size(); i++)
{
string rbody;
session->getBody(vtReqid[i], rbody);

// cout << vtReqid[i] << ", " << rbody << endl;

vector<char> data;
session->doResponse(vtReqid[i], rsp, data);
buffer.insert(buffer.end(), data.begin(), data.end());


}
}
else
{
//method 2:
session->doRequest(current->getRequestBuffer(), doRequestFunc, buffer);
}
shared_ptr<TC_Http2Server> session = getHttp2(current->getUId());

// cout << "doRequest:" << session << ", buffer size:" << current->getRequestBuffer().size() << endl;

vector<TC_Http2Server::Http2Context> contexts;

session->decodeRequest(contexts);

// cout << "doRequest context size:" << contexts.size() << endl;

for(size_t i = 0; i< contexts.size(); ++i)
{
TC_Http2Server::Http2Context & context = contexts[i];
vector<char> data;

context.response.setHeader("X-Header", "TARS");
context.response.setResponse(200, "OK", context.request.getContent());

int ret = session->encodeResponse(context, data);
if(ret != 0)
{
cout << "encodeResponse error:" << session->getErrMsg() << endl;
}
buffer.insert(buffer.end(), data.begin(), data.end());
}

// cout << "doRequest buffer size:" << buffer.size() << endl;

// static bool flag = true;
// if(flag)
// {
// //method 1:
// vector<int32_t> vtReqid;
// TC_Http2Server::doRequest(current->getRequestBuffer(), vtReqid);
//
// // cout << "doRequest size:" << vtReqid.size() << endl;
//
// TC_Http2Server::Http2Response rsp;
// rsp.status = 200;
// rsp.about = "OK";
// rsp.body = "response helloworld 1";
//
// for(size_t i = 0; i < vtReqid.size(); i++)
// {
// string rbody;
// session->getBody(vtReqid[i], rbody);
//
//// cout << vtReqid[i] << ", " << rbody << endl;
//
// vector<char> data;
// session->doResponse(vtReqid[i], rsp, data);
// buffer.insert(buffer.end(), data.begin(), data.end());
//
//
// }
// }
// else
// {
// //method 2:
// session->doRequest(current->getRequestBuffer(), doRequestFunc, buffer);
// }

// flag = !flag;

Expand All @@ -90,6 +115,7 @@ int Http2Imp::doRequest(TarsCurrentPtr current, vector<char> &buffer)

int Http2Imp::doClose(TarsCurrentPtr current)
{
cout << "doClose" << endl;
delHttp2(current->getUId());

return 0;
Expand Down
6 changes: 3 additions & 3 deletions examples/HttpDemo/Http2Server/Http2Imp.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class Http2Imp : public Servant
*/
int doClose(TarsCurrentPtr current);

static TC_Http2Server *getHttp2(uint32_t uid)
static shared_ptr<TC_Http2Server> getHttp2(uint32_t uid)
{
TC_LockT<TC_SpinLock> lock(_mutex);

Expand All @@ -70,7 +70,7 @@ class Http2Imp : public Servant
return NULL;
}

static void addHttp2(uint32_t uid, TC_Http2Server* ptr)
static void addHttp2(uint32_t uid, const shared_ptr<TC_Http2Server> &ptr)
{
TC_LockT<TC_SpinLock> lock(_mutex);

Expand All @@ -93,7 +93,7 @@ class Http2Imp : public Servant

static TC_SpinLock _mutex;

static unordered_map<int32_t, TC_Http2Server*> _http2;
static unordered_map<int32_t, shared_ptr<TC_Http2Server>> _http2;
};
/////////////////////////////////////////////////////
#endif
19 changes: 14 additions & 5 deletions examples/HttpDemo/Http2Server/HttpServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,27 @@ HttpServer g_app;

TC_NetWorkBuffer::PACKET_TYPE parseHttp2(TC_NetWorkBuffer&in, vector<char> &out)
{
TC_Http2Server*session = (TC_Http2Server*)(in.getContextData());
TC_Http2Server*sessionPtr = (TC_Http2Server*)(in.getContextData());

if(session == NULL)
if(sessionPtr == NULL)
{
session = new TC_Http2Server();
in.setContextData(session, [=]{delete session;});
shared_ptr<TC_Http2Server> session(new TC_Http2Server());
// in.setContextData(session, [=]{delete session;});
in.setContextData(session.get());

session->settings(3000);

TC_EpollServer::Connection *connection = (TC_EpollServer::Connection *)in.getConnection();
Http2Imp::addHttp2(connection->getId(), session);

sessionPtr = session.get();
}

return session->parse(in, out);
TC_NetWorkBuffer::PACKET_TYPE flag = sessionPtr->parse(in, out);

// cout << "parseHttp2:" << session << ", out size:" << out.size() << endl;

return flag;
}


Expand Down
8 changes: 4 additions & 4 deletions examples/HttpDemo/Http2Server/config.conf
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@
#合并回调线程和网络线程(以网络线程个数为准)
mergenetasync = 0
#模块名称
modulename = TestApp.HttpServer
modulename = TestApp.Http2Server
</client>

#定义所有绑定的IP
<server>
closecout = 0
#应用名称
app = TestApp
#服务名称
server = HttpServer
server = Http2Server
#服务的数据目录,可执行文件,配置文件等
basepath = ./
datapath = ./
Expand All @@ -54,7 +54,7 @@
allow =
maxconns = 4096
threads = 1
servant = TestApp.HttpServer.Http2Obj
servant = TestApp.Http2Server.Http2Obj
queuecap = 1000000
protocol = not-tars
</Http2Adapter>
Expand Down
43 changes: 42 additions & 1 deletion util/include/util/tc_http.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ struct TC_HttpRequest_Exception : public TC_Http_Exception
~TC_HttpRequest_Exception() throw(){};
};

class TC_HttpRequest;
class TC_HttpResponse;

/**
* @brief 简单的URL解析类.
*
Expand Down Expand Up @@ -279,6 +282,8 @@ class TC_URL
void specialize();

protected:
friend class TC_HttpRequest;

/**
* @brief 换成URL.
*
Expand Down Expand Up @@ -524,6 +529,12 @@ class TC_Http
const string &getContent() const { return _content; }

/**
* @brief get body
* @return http body
*/
string &getContent() { return _content; }

/**
* @brief 设置http body(默认不修改content-length).
*
* @param content http body内容
Expand Down Expand Up @@ -1156,10 +1167,40 @@ class TC_HttpRequest : public TC_Http
int doRequest(TC_HttpResponse &stHttpRep, int iTimeout = 3000);

/**
* @brief 请求类型.
* @brief get request type
*/
int requestType() const { return _requestType ; }

/**
* @brief set request type
*/
void setRequestType(int requestType) { _requestType = requestType ; }

/**
* set method
* @param
* @return method invalid, throw exception
*/
void setMethod(const char * sMethod);

/**
* set method
* @param
*/
void setPath(const char * sPath);

/**
* set domain
* @param
*/
void setDomain(const char * sDomain);

/**
* set schema
* @param
*/
void setScheme(const char * sScheme);

/**
* @brief 是否是GET请求.
*
Expand Down
Loading

0 comments on commit 0107665

Please sign in to comment.