Commit a9ba5395 authored by octal's avatar octal

Some ongoing work on the http client

parent 26938285
......@@ -61,23 +61,4 @@ private:
int main() {
Header::Registry::registerHeader<XProtocolVersion>();
Experimental::Client client("http://supnetwork.org:9080");
auto ops = Experimental::Client::options()
.threads(1)
.maxConnections(64);
client.init(ops);
auto resp = client.get("/ping").header<XProtocolVersion>(1, 0).send();
resp.then([](Response response) {
std::cout << "Response code = " << response.code() << std::endl;
auto body = response.body();
if (!body.empty())
std::cout << "Response body = " << body << std::endl;
}, Async::NoExcept);
std::this_thread::sleep_for(std::chrono::seconds(1));
client.shutdown();
}
......@@ -12,19 +12,14 @@ using namespace Net;
using namespace Net::Http;
int main(int argc, char *argv[]) {
if (argc < 2) {
std::cerr << "Usage: http_client host [page]" << std::endl;
if (argc < 1) {
std::cerr << "Usage: http_client page" << std::endl;
return 1;
}
std::string host = argv[1];
std::string page;
if (argc == 3)
page = argv[2];
else
page = "/";
std::string page = argv[1];
Experimental::Client client(host);
Experimental::Client client;
auto opts = Http::Experimental::Client::options()
.threads(1)
......
......@@ -10,6 +10,7 @@
#include "http.h"
#include "io.h"
#include "timer_pool.h"
#include "view.h"
#include <atomic>
#include <sys/types.h>
#include <sys/socket.h>
......@@ -24,6 +25,8 @@ namespace Experimental {
class ConnectionPool;
class Transport;
std::pair<StringView, StringView> splitUrl(const std::string& url);
struct Connection : public std::enable_shared_from_this<Connection> {
friend class ConnectionPool;
......@@ -44,13 +47,11 @@ struct Connection : public std::enable_shared_from_this<Connection> {
RequestData(
Async::Resolver resolve, Async::Rejection reject,
const Http::Request& request,
std::string host,
std::chrono::milliseconds timeout,
OnDone onDone)
: resolve(std::move(resolve))
, reject(std::move(reject))
, request(request)
, host(std::move(host))
, timeout(timeout)
, onDone(std::move(onDone))
{ }
......@@ -58,7 +59,6 @@ struct Connection : public std::enable_shared_from_this<Connection> {
Async::Rejection reject;
Http::Request request;
std::string host;
std::chrono::milliseconds timeout;
OnDone onDone;
};
......@@ -76,19 +76,18 @@ struct Connection : public std::enable_shared_from_this<Connection> {
void connect(Net::Address addr);
void close();
bool isIdle() const;
bool isConnected() const;
bool hasTransport() const;
void associateTransport(const std::shared_ptr<Transport>& transport);
Async::Promise<Response> perform(
const Http::Request& request,
std::string host,
std::chrono::milliseconds timeout,
OnDone onDone);
void performImpl(
const Http::Request& request,
std::string host,
std::chrono::milliseconds timeout,
Async::Resolver resolve,
Async::Rejection reject,
......@@ -138,16 +137,23 @@ private:
};
struct ConnectionPool {
void init(size_t max = 1);
std::shared_ptr<Connection> pickConnection();
std::shared_ptr<Connection> pickConnection(const std::string& domain);
void releaseConnection(const std::shared_ptr<Connection>& connection);
size_t availableCount() const;
size_t usedConnections(const std::string& domain) const;
size_t idleConnections(const std::string& domain) const;
size_t availableConnections(const std::string& domain) const;
void closeIdleConnections(const std::string& domain);
private:
std::atomic<uint32_t> usedCount;
std::vector<std::shared_ptr<Connection>> connections;
typedef std::vector<std::shared_ptr<Connection>> Connections;
typedef std::mutex Lock;
typedef std::lock_guard<Lock> Guard;
mutable Lock connsLock;
std::unordered_map<std::string, Connections> conns;
};
class Transport : public Io::Handler {
......@@ -298,13 +304,16 @@ public:
Options& threads(int val);
Options& maxConnections(int val);
Options& keepAlive(bool val);
Options& maxConnectionsPerHost(int val);
private:
int threads_;
int maxConnections_;
int maxConnectionsPerHost_;
bool keepAlive_;
};
Client(const std::string &base);
Client();
static Options options();
void init(const Options& options);
......@@ -319,9 +328,6 @@ public:
private:
Io::ServiceGroup io_;
std::string url_;
std::string host_;
Net::Address addr_;
ConnectionPool pool;
std::shared_ptr<Transport> transport_;
......
......@@ -129,6 +129,10 @@ struct View<std::string> : public ViewBase<char> {
typedef ViewBase<char> Base;
explicit View(std::nullptr_t)
: Base(nullptr)
{ }
explicit View(const char* begin, size_t end)
: Base(begin, end)
{ }
......
This diff is collapsed.
......@@ -25,3 +25,7 @@ add_test( cookie_test run_cookie_test )
add_executable( run_view_test view_test.cc )
target_link_libraries(run_view_test gtest gtest_main net)
add_test( view_test run_view_test )
add_executable( run_client_test http_client_test.cc )
target_link_libraries(run_client_test gtest gtest_main net)
add_test( client_test run_client_test )
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment