Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert back to no worker #283

Merged
merged 4 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions config/ct.config
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
host => "localhost",
port => 8085
}},
{iot_location_service, #{
transport => http,
host => "localhost",
port => 8085
}},
{downlink_service, #{
transport => http,
host => "localhost",
Expand Down
5 changes: 5 additions & 0 deletions config/sys.config
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
host => "localhost",
port => 50051
}},
{iot_location_service, #{
transport => http,
host => "localhost",
port => 50051
}},
{downlink_service, #{
transport => http,
host => "localhost",
Expand Down
5 changes: 5 additions & 0 deletions config/sys.config.src
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
host => "${HPR_IOT_CONFIG_HOST}",
port => "${HPR_IOT_CONFIG_PORT}"
}},
{iot_location_service, #{
transport => "${HPR_IOT_LOCATION_TRANSPORT}",
host => "${HPR_IOT_LOCATION_HOST}",
port => "${HPR_IOT_LOCATION_PORT}"
}},
{downlink_service, #{
transport => "${HPR_DOWNLINK_SERVICE_TRANSPORT}",
host => "${HPR_DOWNLINK_SERVICE_HOST}",
Expand Down
82 changes: 18 additions & 64 deletions src/grpc/iot_config/hpr_gateway_location.erl
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,18 @@
%%%-------------------------------------------------------------------
-module(hpr_gateway_location).

-behaviour(gen_server).

-include("hpr.hrl").
-include("../autogen/iot_config_pb.hrl").

%% ------------------------------------------------------------------
%% API Function Exports
%% ------------------------------------------------------------------
-export([
start_link/1,
init/0,
get/1,
update_location/1,
expire_locations/0
]).

%% ------------------------------------------------------------------
%% gen_server Function Exports
%% ------------------------------------------------------------------
%% This is exported for "just in case"
-export([
init/1,
handle_call/3,
handle_cast/2,
handle_info/2,
terminate/2
get_location_from_ics/1
]).

-define(SERVER, ?MODULE).
Expand All @@ -42,8 +29,6 @@
-define(NOT_FOUND, not_found).
-define(REQUESTED, requested).

-record(state, {}).

-record(location, {
status :: ok | ?NOT_FOUND | error | ?REQUESTED,
gateway :: libp2p_crypto:pubkey_bin(),
Expand All @@ -53,19 +38,10 @@
long = undefined :: float() | undefined
}).

-type state() :: #state{}.
-type loc() :: {h3:h3index(), float(), float()} | undefined.

-export_type([loc/0]).

%% ------------------------------------------------------------------
%%% API Function Definitions
%% ------------------------------------------------------------------

-spec start_link(map()) -> any().
start_link(Args) ->
gen_server:start_link({local, ?SERVER}, ?SERVER, Args, []).

-spec init() -> ok.
init() ->
?ETS = ets:new(?ETS, [
Expand All @@ -92,25 +68,25 @@ get(PubKeyBin) ->
LastHour = Now - ?ERROR_CACHE_TIME,
case ets:lookup(?ETS, PubKeyBin) of
[] ->
ok = ?MODULE:update_location(PubKeyBin),
ok = update_location(PubKeyBin),
{error, ?NOT_FOUND};
[#location{status = ok, timestamp = T, h3_index = H3Index, lat = Lat, long = Long}] when
T < Yesterday
->
ok = ?MODULE:update_location(PubKeyBin),
ok = update_location(PubKeyBin),
{ok, H3Index, Lat, Long};
[#location{status = _, timestamp = T}] when T < Yesterday ->
ok = ?MODULE:update_location(PubKeyBin),
ok = update_location(PubKeyBin),
{error, ?NOT_FOUND};
[#location{status = error, timestamp = T}] when T < LastHour ->
ok = ?MODULE:update_location(PubKeyBin),
ok = update_location(PubKeyBin),
{error, undefined};
[#location{status = error}] ->
{error, undefined};
[#location{status = requested, timestamp = T, gateway = PubKeyBin}] when T < LastHour ->
GatewayName = hpr_utils:gateway_name(PubKeyBin),
lager:warning("got an old request for ~p ~s", [PubKeyBin, GatewayName]),
ok = ?MODULE:update_location(PubKeyBin),
ok = update_location(PubKeyBin),
{error, ?REQUESTED};
[#location{status = requested}] ->
{error, ?REQUESTED};
Expand All @@ -120,16 +96,6 @@ get(PubKeyBin) ->
{ok, H3Index, Lat, Long}
end.

-spec update_location(libp2p_crypto:pubkey_bin()) -> ok.
update_location(PubKeyBin) ->
NewLoc = #location{
status = ?REQUESTED,
gateway = PubKeyBin,
timestamp = erlang:system_time(millisecond)
},
true = ets:insert(?ETS, NewLoc),
gen_server:cast(?SERVER, {update_location, NewLoc}).

-spec expire_locations() -> ok.
expire_locations() ->
Time = erlang:system_time(millisecond) - ?CACHE_TIME,
Expand All @@ -138,18 +104,18 @@ expire_locations() ->
]),
lager:info("expiring ~w dets keys", [DETSDeleted]).

% ------------------------------------------------------------------
%%% gen_server Function Definitions
%% ------------------------------------------------------------------
-spec init(map()) -> {ok, state()}.
init(_Args) ->
{ok, #state{}}.

handle_call(_Msg, _From, State) ->
{reply, ok, State}.
%% Internal Function Definitions
%% ------------------------------------------------------------------

handle_cast({update_location, Loc}, State) ->
PubKeyBin = Loc#location.gateway,
-spec update_location(libp2p_crypto:pubkey_bin()) -> ok.
update_location(PubKeyBin) ->
Loc = #location{
status = ?REQUESTED,
gateway = PubKeyBin,
timestamp = erlang:system_time(millisecond)
},
true = ets:insert(?ETS, Loc),
Start = erlang:system_time(millisecond),
case get_location_from_ics(PubKeyBin) of
{error, ?NOT_FOUND} ->
Expand Down Expand Up @@ -178,20 +144,8 @@ handle_cast({update_location, Loc}, State) ->
lat = Lat,
long = Long
})
end,
{noreply, State};
handle_cast(_Msg, State) ->
{noreply, State}.

handle_info(_Info, State) ->
{noreply, State}.

terminate(_Reason, _state) ->
ok.
end.

%% ------------------------------------------------------------------
%% Internal Function Definitions
%% ------------------------------------------------------------------
-spec insert(Loc :: #location{}) -> ok.
insert(Loc) ->
true = ets:insert(?ETS, Loc),
Expand Down
5 changes: 2 additions & 3 deletions src/hpr_sup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,14 @@ init([]) ->

PacketReporterConfig = application:get_env(?APP, packet_reporter, #{}),
ConfigServiceConfig = application:get_env(?APP, iot_config_service, #{}),
LocationServiceConfig = application:get_env(?APP, iot_location_service, #{}),
DownlinkServiceConfig = application:get_env(?APP, downlink_service, #{}),
MultiBuyServiceConfig = application:get_env(?APP, multi_buy_service, #{}),

%% Starting config service client channel here because of the way we get
%% .env vars into the app.
_ = maybe_start_channel(ConfigServiceConfig, ?IOT_CONFIG_CHANNEL),
_ = maybe_start_channel(ConfigServiceConfig, ?LOCATION_CHANNEL),
_ = maybe_start_channel(LocationServiceConfig, ?LOCATION_CHANNEL),
_ = maybe_start_channel(DownlinkServiceConfig, ?DOWNLINK_CHANNEL),
_ = maybe_start_channel(MultiBuyServiceConfig, ?MULTI_BUY_CHANNEL),

Expand All @@ -85,8 +86,6 @@ init([]) ->

?WORKER(hpr_packet_reporter, [PacketReporterConfig]),

?WORKER(hpr_gateway_location, [#{}]),

?WORKER(hpr_route_stream_worker, [#{}]),

?WORKER(hpr_protocol_router, [#{}]),
Expand Down
1 change: 0 additions & 1 deletion src/hpr_utils.erl
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,6 @@ get_device_traces(Data) ->
format_coord(Coord) ->
erlang:float_to_binary(Coord, [{decimals, 15}]).


%% ------------------------------------------------------------------
%% EUNIT Tests
%% ------------------------------------------------------------------
Expand Down
Loading