From e5a673376feba0a4313cf616b001e717f92a1f58 Mon Sep 17 00:00:00 2001 From: JianBo He Date: Sun, 9 Oct 2022 17:25:48 +0800 Subject: [PATCH] refactor: support the retry option --- .../emqx_auth_http/include/emqx_auth_http.hrl | 19 +++++++++++++++++++ apps/emqx_auth_http/src/emqx_acl_http.erl | 8 +++++--- apps/emqx_auth_http/src/emqx_auth_http.erl | 14 +++++++++----- .../emqx_auth_http/src/emqx_auth_http_cli.erl | 14 +++++++++----- 4 files changed, 42 insertions(+), 13 deletions(-) diff --git a/apps/emqx_auth_http/include/emqx_auth_http.hrl b/apps/emqx_auth_http/include/emqx_auth_http.hrl index 0eaa59daf..4e659293f 100644 --- a/apps/emqx_auth_http/include/emqx_auth_http.hrl +++ b/apps/emqx_auth_http/include/emqx_auth_http.hrl @@ -1 +1,20 @@ +%%-------------------------------------------------------------------- +%% Copyright (c) 2020-2022 EMQ Technologies Co., Ltd. All Rights Reserved. +%% +%% Licensed under the Apache License, Version 2.0 (the "License"); +%% you may not use this file except in compliance with the License. +%% You may obtain a copy of the License at +%% +%% http://www.apache.org/licenses/LICENSE-2.0 +%% +%% Unless required by applicable law or agreed to in writing, software +%% distributed under the License is distributed on an "AS IS" BASIS, +%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +%% See the License for the specific language governing permissions and +%% limitations under the License. +%%-------------------------------------------------------------------- + -define(APP, emqx_auth_http). + +%% equals to the default value of ehttpc +-define(DEFAULT_RETRY_TIMES, 2). diff --git a/apps/emqx_auth_http/src/emqx_acl_http.erl b/apps/emqx_auth_http/src/emqx_acl_http.erl index d4fd96a95..73cf6ce11 100644 --- a/apps/emqx_auth_http/src/emqx_acl_http.erl +++ b/apps/emqx_auth_http/src/emqx_acl_http.erl @@ -24,7 +24,7 @@ -logger_header("[ACL http]"). -import(emqx_auth_http_cli, - [ request/6 + [ request/7 , feedvar/2 ]). @@ -56,13 +56,15 @@ description() -> "ACL with HTTP API". %% Internal functions %%-------------------------------------------------------------------- -check_acl_request(#{pool_name := PoolName, +check_acl_request(Params = + #{pool_name := PoolName, path := Path, method := Method, headers := Headers, params := Params, timeout := Timeout}, ClientInfo) -> - request(PoolName, Method, Path, Headers, feedvar(Params, ClientInfo), Timeout). + Retry = maps:get(retry_times, Params, ?DEFAULT_RETRY_TIMES), + request(PoolName, Method, Path, Headers, feedvar(Params, ClientInfo), Timeout, Retry). access(subscribe) -> 1; access(publish) -> 2. diff --git a/apps/emqx_auth_http/src/emqx_auth_http.erl b/apps/emqx_auth_http/src/emqx_auth_http.erl index 98a897a8c..0c8c46670 100644 --- a/apps/emqx_auth_http/src/emqx_auth_http.erl +++ b/apps/emqx_auth_http/src/emqx_auth_http.erl @@ -25,7 +25,7 @@ -logger_header("[Auth http]"). -import(emqx_auth_http_cli, - [ request/6 + [ request/7 , feedvar/2 ]). @@ -63,24 +63,28 @@ description() -> "Authentication by HTTP API". %% Requests %%-------------------------------------------------------------------- -authenticate(#{pool_name := PoolName, +authenticate(Params = + #{pool_name := PoolName, path := Path, method := Method, headers := Headers, params := Params, timeout := Timeout}, ClientInfo) -> - request(PoolName, Method, Path, Headers, feedvar(Params, ClientInfo), Timeout). + Retry = maps:get(retry_times, Params, ?DEFAULT_RETRY_TIMES), + request(PoolName, Method, Path, Headers, feedvar(Params, ClientInfo), Timeout, Retry). -spec(is_superuser(maybe(map()), emqx_types:client()) -> boolean()). is_superuser(undefined, _ClientInfo) -> false; -is_superuser(#{pool_name := PoolName, +is_superuser(Params = + #{pool_name := PoolName, path := Path, method := Method, headers := Headers, params := Params, timeout := Timeout}, ClientInfo) -> - case request(PoolName, Method, Path, Headers, feedvar(Params, ClientInfo), Timeout) of + Retry = maps:get(retry_times, Params, ?DEFAULT_RETRY_TIMES), + case request(PoolName, Method, Path, Headers, feedvar(Params, ClientInfo), Timeout, Retry) of {ok, 200, _Body} -> true; {ok, _Code, _Body} -> false; {error, Error} -> ?LOG(error, "Request superuser path ~s, error: ~p", [Path, Error]), diff --git a/apps/emqx_auth_http/src/emqx_auth_http_cli.erl b/apps/emqx_auth_http/src/emqx_auth_http_cli.erl index 3c7efd9c9..16c2c8574 100644 --- a/apps/emqx_auth_http/src/emqx_auth_http_cli.erl +++ b/apps/emqx_auth_http/src/emqx_auth_http_cli.erl @@ -19,6 +19,7 @@ -include("emqx_auth_http.hrl"). -export([ request/6 + , request/7 , feedvar/2 , feedvar/3 ]). @@ -27,18 +28,21 @@ %% HTTP Request %%-------------------------------------------------------------------- -request(PoolName, get, Path, Headers, Params, Timeout) -> - NewPath = Path ++ "?" ++ binary_to_list(cow_qs:qs(bin_kw(Params))), - reply(ehttpc:request(PoolName, get, {NewPath, Headers}, Timeout)); +request(PoolName, Method, Path, Headers, Params, Timeout) -> + request(PoolName, Method, Path, Headers, Params, ?DEFAULT_RETRY_TIMES). -request(PoolName, post, Path, Headers, Params, Timeout) -> +request(PoolName, get, Path, Headers, Params, Timeout, Retry) -> + NewPath = Path ++ "?" ++ binary_to_list(cow_qs:qs(bin_kw(Params))), + reply(ehttpc:request(PoolName, get, {NewPath, Headers}, Timeout, Retry)); + +request(PoolName, post, Path, Headers, Params, Timeout, Retry) -> Body = case proplists:get_value(<<"content-type">>, Headers) of "application/x-www-form-urlencoded" -> cow_qs:qs(bin_kw(Params)); "application/json" -> emqx_json:encode(bin_kw(Params)) end, - reply(ehttpc:request(PoolName, post, {Path, Headers, Body}, Timeout)). + reply(ehttpc:request(PoolName, post, {Path, Headers, Body}, Timeout, Retry)). reply({ok, StatusCode, _Headers}) -> {ok, StatusCode, <<>>};