webcc icon indicating copy to clipboard operation
webcc copied to clipboard

为啥不提供异步接口

Open windflowerly opened this issue 4 years ago • 2 comments

我看了您的部分代码(主要是关于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函数中强行做了一个同步。

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

windflowerly avatar Sep 29 '21 02:09 windflowerly

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

sprinfall avatar Oct 09 '21 01:10 sprinfall

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

sprinfall avatar Dec 03 '23 14:12 sprinfall