|
@@ -86,17 +86,7 @@ on_start(
|
|
|
connector => ResourceId,
|
|
connector => ResourceId,
|
|
|
config => Config
|
|
config => Config
|
|
|
}),
|
|
}),
|
|
|
- %% emulating the emulator behavior
|
|
|
|
|
- %% https://cloud.google.com/pubsub/docs/emulator
|
|
|
|
|
- {Transport, HostPort} =
|
|
|
|
|
- case os:getenv("PUBSUB_EMULATOR_HOST") of
|
|
|
|
|
- false ->
|
|
|
|
|
- {tls, "pubsub.googleapis.com:443"};
|
|
|
|
|
- HostPort0 ->
|
|
|
|
|
- %% The emulator is plain HTTP...
|
|
|
|
|
- Transport0 = persistent_term:get({?MODULE, transport}, tcp),
|
|
|
|
|
- {Transport0, HostPort0}
|
|
|
|
|
- end,
|
|
|
|
|
|
|
+ {Transport, HostPort} = get_transport(),
|
|
|
#{hostname := Host, port := Port} = emqx_schema:parse_server(HostPort, #{default_port => 443}),
|
|
#{hostname := Host, port := Port} = emqx_schema:parse_server(HostPort, #{default_port => 443}),
|
|
|
PoolType = random,
|
|
PoolType = random,
|
|
|
TransportOpts =
|
|
TransportOpts =
|
|
@@ -302,7 +292,6 @@ get_jwt_authorization_header(JWTConfig) ->
|
|
|
{ok, map()} | {error, {recoverable_error, term()} | term()}.
|
|
{ok, map()} | {error, {recoverable_error, term()} | term()}.
|
|
|
do_send_requests_sync(State, {prepared_request, {Method, Path, Body}}, ResourceId) ->
|
|
do_send_requests_sync(State, {prepared_request, {Method, Path, Body}}, ResourceId) ->
|
|
|
#{
|
|
#{
|
|
|
- jwt_config := JWTConfig,
|
|
|
|
|
pool_name := PoolName,
|
|
pool_name := PoolName,
|
|
|
max_retries := MaxRetries,
|
|
max_retries := MaxRetries,
|
|
|
request_ttl := RequestTTL
|
|
request_ttl := RequestTTL
|
|
@@ -315,12 +304,7 @@ do_send_requests_sync(State, {prepared_request, {Method, Path, Body}}, ResourceI
|
|
|
resource_id => ResourceId
|
|
resource_id => ResourceId
|
|
|
}
|
|
}
|
|
|
),
|
|
),
|
|
|
- Headers = get_jwt_authorization_header(JWTConfig),
|
|
|
|
|
- Request =
|
|
|
|
|
- case {Method, Body} of
|
|
|
|
|
- {get, <<>>} -> {Path, Headers};
|
|
|
|
|
- _ -> {Path, Headers, Body}
|
|
|
|
|
- end,
|
|
|
|
|
|
|
+ Request = to_ehttpc_request(State, Method, Path, Body),
|
|
|
Response = ehttpc:request(
|
|
Response = ehttpc:request(
|
|
|
PoolName,
|
|
PoolName,
|
|
|
Method,
|
|
Method,
|
|
@@ -340,7 +324,6 @@ do_send_requests_async(
|
|
|
State, {prepared_request, {Method, Path, Body}}, ReplyFunAndArgs, ResourceId
|
|
State, {prepared_request, {Method, Path, Body}}, ReplyFunAndArgs, ResourceId
|
|
|
) ->
|
|
) ->
|
|
|
#{
|
|
#{
|
|
|
- jwt_config := JWTConfig,
|
|
|
|
|
pool_name := PoolName,
|
|
pool_name := PoolName,
|
|
|
request_ttl := RequestTTL
|
|
request_ttl := RequestTTL
|
|
|
} = State,
|
|
} = State,
|
|
@@ -352,12 +335,7 @@ do_send_requests_async(
|
|
|
resource_id => ResourceId
|
|
resource_id => ResourceId
|
|
|
}
|
|
}
|
|
|
),
|
|
),
|
|
|
- Headers = get_jwt_authorization_header(JWTConfig),
|
|
|
|
|
- Request =
|
|
|
|
|
- case {Method, Body} of
|
|
|
|
|
- {get, <<>>} -> {Path, Headers};
|
|
|
|
|
- _ -> {Path, Headers, Body}
|
|
|
|
|
- end,
|
|
|
|
|
|
|
+ Request = to_ehttpc_request(State, Method, Path, Body),
|
|
|
Worker = ehttpc_pool:pick_worker(PoolName),
|
|
Worker = ehttpc_pool:pick_worker(PoolName),
|
|
|
ok = ehttpc:request_async(
|
|
ok = ehttpc:request_async(
|
|
|
Worker,
|
|
Worker,
|
|
@@ -368,6 +346,14 @@ do_send_requests_async(
|
|
|
),
|
|
),
|
|
|
{ok, Worker}.
|
|
{ok, Worker}.
|
|
|
|
|
|
|
|
|
|
+to_ehttpc_request(State, Method, Path, Body) ->
|
|
|
|
|
+ #{jwt_config := JWTConfig} = State,
|
|
|
|
|
+ Headers = get_jwt_authorization_header(JWTConfig),
|
|
|
|
|
+ case {Method, Body} of
|
|
|
|
|
+ {get, <<>>} -> {Path, Headers};
|
|
|
|
|
+ _ -> {Path, Headers, Body}
|
|
|
|
|
+ end.
|
|
|
|
|
+
|
|
|
-spec handle_response(term(), resource_id(), sync | async) -> {ok, map()} | {error, term()}.
|
|
-spec handle_response(term(), resource_id(), sync | async) -> {ok, map()} | {error, term()}.
|
|
|
handle_response(Result, ResourceId, QueryMode) ->
|
|
handle_response(Result, ResourceId, QueryMode) ->
|
|
|
case Result of
|
|
case Result of
|
|
@@ -461,3 +447,16 @@ do_get_status(ResourceId, Timeout) ->
|
|
|
exit:timeout ->
|
|
exit:timeout ->
|
|
|
false
|
|
false
|
|
|
end.
|
|
end.
|
|
|
|
|
+
|
|
|
|
|
+-spec get_transport() -> {tls | tcp, string()}.
|
|
|
|
|
+get_transport() ->
|
|
|
|
|
+ %% emulating the emulator behavior
|
|
|
|
|
+ %% https://cloud.google.com/pubsub/docs/emulator
|
|
|
|
|
+ case os:getenv("PUBSUB_EMULATOR_HOST") of
|
|
|
|
|
+ false ->
|
|
|
|
|
+ {tls, "pubsub.googleapis.com:443"};
|
|
|
|
|
+ HostPort0 ->
|
|
|
|
|
+ %% The emulator is plain HTTP...
|
|
|
|
|
+ Transport0 = persistent_term:get({?MODULE, transport}, tcp),
|
|
|
|
|
+ {Transport0, HostPort0}
|
|
|
|
|
+ end.
|