fix httpc

This commit is contained in:
anlicheng 2025-04-29 23:13:51 +08:00
parent 8b9774132b
commit b30a8a91df
4 changed files with 32 additions and 42 deletions

View File

@ -82,23 +82,28 @@ handle_call(_Request, _From, State = #state{}) ->
{noreply, NewState :: #state{}, timeout() | hibernate} | {noreply, NewState :: #state{}, timeout() | hibernate} |
{stop, Reason :: term(), NewState :: #state{}}). {stop, Reason :: term(), NewState :: #state{}}).
handle_cast({download, ReceiverPid, Ref, Url, TargetDir}, State = #state{}) -> handle_cast({download, ReceiverPid, Ref, Url, TargetDir}, State = #state{}) ->
SSLOpts = {ssl_options, [ SslOpts = [
% {ssl, [
{verify, verify_none} %
]}, {verify, verify_none}
]}
],
TargetFile = get_filename_from_url(Url), TargetFile = get_filename_from_url(Url),
FullFilename = TargetDir ++ TargetFile,
StartTs = os:timestamp(), StartTs = os:timestamp(),
case hackney:request(get, Url, [], <<>>, [async, {stream_to, self()}, {pool, false}, SSLOpts]) of case httpc:request(get, {Url, []}, SslOpts, [{sync, false}, {stream, self}]) of
{ok, ClientRef} -> {ok, RequestId} ->
case receive_data(ClientRef, TargetDir, TargetFile) of case receive_data(RequestId, FullFilename) of
ok -> ok ->
EndTs = os:timestamp(), EndTs = os:timestamp(),
%% %%
CostMs = timer:now_diff(EndTs, StartTs) div 1000, CostMs = timer:now_diff(EndTs, StartTs) div 1000,
efka_logger:debug("download cost: ~p", [CostMs]),
ReceiverPid ! {download_response, Ref, {ok, CostMs}}; ReceiverPid ! {download_response, Ref, {ok, CostMs}};
{error, Reason} -> {error, Reason} ->
%%
file:delete(FullFilename),
ReceiverPid ! {download_response, Ref, {error, Reason}} ReceiverPid ! {download_response, Ref, {error, Reason}}
end; end;
{error, Reason} -> {error, Reason} ->
@ -137,37 +142,25 @@ code_change(_OldVsn, State = #state{}, _Extra) ->
%%% Internal functions %%% Internal functions
%%%=================================================================== %%%===================================================================
%% ResponseLine
receive_data(ClientRef, TargetDir, DefaultFile) when is_list(TargetDir), is_list(DefaultFile) ->
receive
{hackney_response, ClientRef, {status, 200, _Reason}} ->
receive_data0(ClientRef, TargetDir, DefaultFile);
{hackney_response, ClientRef, {status, StatusCode, _Reason}} ->
{error, {http_status, StatusCode}}
end.
%% , %% ,
receive_data0(ClientRef, TargetDir, DefaultFile) -> receive_data(RequestId, FullFilename) ->
receive receive
{hackney_response, ClientRef, {headers, Headers}} -> {http, {RequestId, stream_start, _Headers}} ->
TargetFilename = extra_filename(Headers, DefaultFile),
FullFilename = TargetDir ++ TargetFilename,
efka_logger:debug("full name: ~p", [FullFilename]),
{ok, File} = file:open(FullFilename, [write, binary]), {ok, File} = file:open(FullFilename, [write, binary]),
receive_data1(ClientRef, File) receive_data1(RequestId, File)
end. end.
%% %%
receive_data1(ClientRef, File) -> receive_data1(RequestId, File) ->
receive receive
{hackney_response, ClientRef, {error, Reason}} -> {http, {RequestId, {error, Reason}}} ->
ok = file:close(File), ok = file:close(File),
{error, Reason}; {error, Reason};
{hackney_response, ClientRef, done} -> {http, {RequestId, stream_end, _Headers}} ->
ok = file:close(File), ok = file:close(File),
hackney:close(ClientRef),
ok; ok;
{hackney_response, ClientRef, Data} -> {http, {RequestId, stream, Data}} ->
file:write(File, Data), file:write(File, Data),
receive_data1(ClientRef, File) receive_data1(RequestId, File)
end. end.
-spec extra_filename(Headers :: list(), Default :: string()) -> Filename :: string(). -spec extra_filename(Headers :: list(), Default :: string()) -> Filename :: string().
@ -192,4 +185,3 @@ get_filename_from_url(Url) when is_list(Url) ->
URIMap = uri_string:parse(Url), URIMap = uri_string:parse(Url),
Path = maps:get(path, URIMap), Path = maps:get(path, URIMap),
filename:basename(Path). filename:basename(Path).

View File

@ -13,7 +13,7 @@
-export([debug/2, notice/2]). -export([debug/2, notice/2]).
debug(Format, Args) -> debug(Format, Args) ->
io:format(Format, Args). io:format(Format ++ "~n", Args).
notice(Format, Args) -> notice(Format, Args) ->
io:format(Format, Args). io:format(Format ++ "~n", Args).

View File

@ -28,14 +28,14 @@ start_link() ->
init([]) -> init([]) ->
SupFlags = #{strategy => one_for_one, intensity => 1000, period => 3600}, SupFlags = #{strategy => one_for_one, intensity => 1000, period => 3600},
ChildSpecs = [ ChildSpecs = [
#{ %#{
id => 'efka_agent', % id => 'efka_agent',
start => {'efka_agent', start_link, []}, % start => {'efka_agent', start_link, []},
restart => permanent, % restart => permanent,
shutdown => 2000, % shutdown => 2000,
type => worker, % type => worker,
modules => ['efka_agent'] % modules => ['efka_agent']
}, %},
#{ #{
id => 'efka_server_sup', id => 'efka_server_sup',

View File

@ -35,8 +35,6 @@
] ]
}]}]}. }]}]}.
{erl_opts, [{parse_transform,lager_transform}]}.
{project_plugins, [ {project_plugins, [
%% 或从 Git 仓库拉取最新版本 %% 或从 Git 仓库拉取最新版本
{pc, {git, "https://github.com/blt/port_compiler.git", {tag, "v1.15.0"}}} {pc, {git, "https://github.com/blt/port_compiler.git", {tag, "v1.15.0"}}}