Skip to content

Commit

Permalink
ensure thread-safe
Browse files Browse the repository at this point in the history
  • Loading branch information
ithewei committed Nov 17, 2023
1 parent df8b2d6 commit bb2fae9
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
2 changes: 1 addition & 1 deletion evpp/EventLoop.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ class EventLoop : public Status {

// Timer interfaces: setTimer, killTimer, resetTimer
TimerID setTimer(int timeout_ms, TimerCallback cb, uint32_t repeat = INFINITE, TimerID timerID = INVALID_TIMER_ID) {
assertInLoopThread();
if (loop_ == NULL) return INVALID_TIMER_ID;
assertInLoopThread();
htimer_t* htimer = htimer_add(loop_, onTimer, timeout_ms, repeat);
assert(htimer != NULL);
if (timerID == INVALID_TIMER_ID) {
Expand Down
13 changes: 13 additions & 0 deletions http/client/AsyncHttpClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@

namespace hv {

int AsyncHttpClient::send(const HttpRequestPtr& req, HttpResponseCallback resp_cb) {
hloop_t* loop = EventLoopThread::hloop();
if (loop == NULL) return -1;
auto task = std::make_shared<HttpClientTask>();
task->req = req;
task->cb = std::move(resp_cb);
task->start_time = hloop_now_hrtime(loop);
if (req->retry_count > 0 && req->retry_delay > 0) {
req->retry_count = MIN(req->retry_count, req->timeout * 1000 / req->retry_delay - 1);
}
return send(task);
}

// createsocket => startConnect =>
// onconnect => sendRequest => startRead =>
// onread => HttpParser => resp_cb
Expand Down
12 changes: 1 addition & 11 deletions http/client/AsyncHttpClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,17 +114,7 @@ class HV_EXPORT AsyncHttpClient : private EventLoopThread {
}

// thread-safe
int send(const HttpRequestPtr& req, HttpResponseCallback resp_cb) {
auto task = std::make_shared<HttpClientTask>();
task->req = req;
task->cb = std::move(resp_cb);
task->start_time = hloop_now_hrtime(EventLoopThread::hloop());
if (req->retry_count > 0 && req->retry_delay > 0) {
req->retry_count = MIN(req->retry_count, req->timeout * 1000 / req->retry_delay - 1);
}
return send(task);
}

int send(const HttpRequestPtr& req, HttpResponseCallback resp_cb);
int send(const HttpClientTaskPtr& task) {
EventLoopThread::loop()->queueInLoop(std::bind(&AsyncHttpClient::sendInLoop, this, task));
return 0;
Expand Down

0 comments on commit bb2fae9

Please sign in to comment.