Skip to content
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

为啥不提供异步接口 #26

Open
windflowerly opened this issue Sep 29, 2021 · 2 comments
Open

为啥不提供异步接口 #26

windflowerly opened this issue Sep 29, 2021 · 2 comments

Comments

@windflowerly
Copy link

我看了您的部分代码(主要是关于http client部分的),发现其本身就是支持异步调用的。


void ClientBase::AsyncResolve(string_view default_port) {
std::string port = request_->port();
if (port.empty()) {
port = ToString(default_port);
}

LOG_VERB("Resolve host (%s)", request_->host().c_str());

// The protocol depends on the host, both V4 and V6 are supported.
resolver_.async_resolve(request_->host(), port,
std::bind(&ClientBase::OnResolve, this, _1, _2));
}

void ClientBase::OnResolve(boost::system::error_code ec,
tcp::resolver::results_type endpoints) {
if (ec) {
LOG_ERRO("Host resolve error (%s)", ec.message().c_str());
error_.Set(Error::kResolveError, "Host resolve error");
FinishRequest();
return;
}

LOG_VERB("Connect socket");

AsyncWaitDeadlineTimer(connect_timeout_);

socket_->AsyncConnect(request_->host(), endpoints,
std::bind(&ClientBase::OnConnect, this, _1, _2));
}

以上代码就是异步调用的。只是在ClientBase::FinishRequest函数中强行做了一个同步。

我比较好奇的是,为啥不开放异步调用接口。对整个框架其实也没有额外的负担。

@sprinfall
Copy link
Owner

sprinfall commented Oct 9, 2021

暂时还没想好接口应该怎么设计(回调?future?),
Keep-Alive(持久连接)和对象生命周期都不好管理,
异步 client 接口用起来也不方便。
最初是参考 Python requests 程序库来做的,requests (v2) 也没有提供异步的客户端接口。

@sprinfall
Copy link
Owner

之前通过条件变量强行做的同步被去掉了,但是一个 request 调用下来仍然是”同步“的。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants