Skip to content

feat: support http client reuse external loop #735

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions http/client/HttpClient.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "HttpClient.h"

#include <memory>
#include <mutex>

#ifdef WITH_CURL
Expand Down Expand Up @@ -731,3 +732,10 @@ int http_client_send_async(HttpRequestPtr req, HttpResponseCallback resp_cb) {

return http_client_exec_async(hv_default_async_http_client(), req, std::move(resp_cb));
}

int http_client_reuse_loop(http_client_t *cli, hv::EventLoopPtr loop) {
if (!cli || !loop) return ERR_NULL_POINTER;
cli->async_client_ = std::make_shared<hv::AsyncHttpClient>(std::move(loop));
return 0;
}

6 changes: 6 additions & 0 deletions http/client/HttpClient.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef HV_HTTP_CLIENT_H_
#define HV_HTTP_CLIENT_H_

#include "EventLoop.h"
#include "hexport.h"
#include "hssl.h"
#include "HttpMessage.h"
Expand Down Expand Up @@ -76,6 +77,7 @@ HV_EXPORT int http_client_send_data(http_client_t* cli, const char* data, int si
HV_EXPORT int http_client_recv_data(http_client_t* cli, char* data, int size);
HV_EXPORT int http_client_recv_response(http_client_t* cli, HttpResponse* resp);
HV_EXPORT int http_client_close(http_client_t* cli);
HV_EXPORT int http_client_reuse_loop(http_client_t *cli, hv::EventLoopPtr loop);

namespace hv {

Expand Down Expand Up @@ -161,6 +163,10 @@ class HttpClient {
int close() {
return http_client_close(_client);
}
//if need, set once, set before sendAsync
int reuseLoop(EventLoopPtr loop) {
return http_client_reuse_loop(_client, std::move(loop));
}

private:
http_client_t* _client;
Expand Down