diff options
author | Magnus Ahltorp <map@kth.se> | 2015-02-19 12:32:44 +0100 |
---|---|---|
committer | Magnus Ahltorp <map@kth.se> | 2015-02-19 12:32:44 +0100 |
commit | a287b6c12c4d81b9fd09dd4ae9a85d8f6eef1cb9 (patch) | |
tree | a26109a9e5969fcbba4d7ade43455a3d330bcfea /src/plop.erl | |
parent | 0a8c2486dab278353febb4c50c9722779977227a (diff) |
Use hackney instead of inets:httpc
Diffstat (limited to 'src/plop.erl')
-rw-r--r-- | src/plop.erl | 31 |
1 files changed, 25 insertions, 6 deletions
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 |