From a287b6c12c4d81b9fd09dd4ae9a85d8f6eef1cb9 Mon Sep 17 00:00:00 2001 From: Magnus Ahltorp Date: Thu, 19 Feb 2015 12:32:44 +0100 Subject: Use hackney instead of inets:httpc --- Emakefile | 1 + src/plop.erl | 31 +++++++++++++++++++++++++------ src/plop_app.erl | 1 + 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/Emakefile b/Emakefile index fe4b31d..dc13e9c 100644 --- a/Emakefile +++ b/Emakefile @@ -2,6 +2,7 @@ {["src/*", "test/*"], [debug_info, {i, "include/"}, + {i, "../"}, {outdir, "ebin/"}, {parse_transform, lager_transform}]}. {["test/src/*"], diff --git a/src/plop.erl b/src/plop.erl index 114695d..f90d287 100644 --- a/src/plop.erl +++ b/src/plop.erl @@ -39,6 +39,7 @@ %%-include("db.hrl"). -include_lib("public_key/include/public_key.hrl"). -include_lib("eunit/include/eunit.hrl"). +-include_lib("hackney/include/hackney_lib.hrl"). %%%%% moved from plop.hrl, maybe remove -define(PLOPVERSION, 0). @@ -193,6 +194,28 @@ storage_nodes_quorum() -> {ok, Value} = application:get_env(plop, storage_nodes_quorum), Value. +send_http_request(TreeLeafHash, URL, Headers, RequestBody) -> + ParentPid = self(), + RequestId = make_ref(), + CACertFile = application:get_env(catlfish, https_cacertfile, none), + spawn(fun () -> + Starttime = os:timestamp(), + ParsedURL = hackney_url:parse_url(URL), + #hackney_url{path = Path} = ParsedURL, + lager:debug("leafhash ~s: sending http request to ~p", [mochihex:to_hex(TreeLeafHash), URL]), + {ok, ConnRef} = hackney:connect(ParsedURL, [{ssl_options, [{cacertfile, CACertFile}]}]), + lager:debug("leafhash ~s: connected to ~p", [mochihex:to_hex(TreeLeafHash), URL]), + {ok, StatusCode, RespHeaders, ClientRef} = hackney:send_request(ConnRef, {post, Path, Headers, RequestBody}), + lager:debug("leafhash ~s: received headers for ~p", [mochihex:to_hex(TreeLeafHash), URL]), + {ok, Body} = hackney:body(ClientRef), + Stoptime = os:timestamp(), + hackney:close(ClientRef), + lager:debug("leafhash ~s: received body for ~p: time ~p", [mochihex:to_hex(TreeLeafHash), URL, timer:now_diff(Stoptime, Starttime)]), + StatusLine = {none, StatusCode, none}, + ParentPid ! {http, {RequestId, {StatusLine, RespHeaders, Body}}} + end), + RequestId. + send_storage_sendentry(URLBase, LogEntry, TreeLeafHash) -> Request = mochijson2:encode( {[{plop_version, 1}, @@ -201,9 +224,7 @@ send_storage_sendentry(URLBase, LogEntry, TreeLeafHash) -> ]}), lager:debug("leafhash ~s: send sendentry to storage node ~p", [mochihex:to_hex(TreeLeafHash), URLBase]), - {ok, RequestId} = httpc:request(post, {URLBase ++ "sendentry", [], - "text/json", list_to_binary(Request)}, - [], [{sync, false}]), + RequestId = send_http_request(TreeLeafHash, URLBase ++ "sendentry", [{"Content-Type", "text/json"}], list_to_binary(Request)), {RequestId, URLBase}. send_storage_entrycommitted(URLBase, EntryHash, TreeLeafHash) -> @@ -212,9 +233,7 @@ send_storage_entrycommitted(URLBase, EntryHash, TreeLeafHash) -> {entryhash, base64:encode(EntryHash)}, {treeleafhash, base64:encode(TreeLeafHash)} ]}), - httpc:request(post, {URLBase ++ "entrycommitted", [], - "text/json", list_to_binary(Request)}, - [], [{sync, false}]). + send_http_request(TreeLeafHash, URLBase ++ "entrycommitted", [{"Content-Type", "text/json"}], list_to_binary(Request)). store_loop(TreeLeafHash, Requests, RepliesUntilQuorum) -> receive diff --git a/src/plop_app.erl b/src/plop_app.erl index 767bf06..9cb5558 100644 --- a/src/plop_app.erl +++ b/src/plop_app.erl @@ -6,6 +6,7 @@ -export([start/2, stop/1]). start(normal, Args) -> + hackney:start(), plop_sup:start_link(Args). stop(_State) -> -- cgit v1.1