diff --git a/src/protocols/hpr_protocol_http_roaming.erl b/src/protocols/hpr_protocol_http_roaming.erl index 48f1183..a35c29b 100644 --- a/src/protocols/hpr_protocol_http_roaming.erl +++ b/src/protocols/hpr_protocol_http_roaming.erl @@ -20,6 +20,18 @@ GatewayLocation :: hpr_gateway_location:loc() ) -> ok | {error, any()}. send(PacketUp, Route, Timestamp, GatewayLocation) -> + send(PacketUp, Route, Timestamp, GatewayLocation, 3). + +-spec send( + PacketUp :: hpr_packet_up:packet(), + Route :: hpr_route:route(), + Timestamp :: non_neg_integer(), + GatewayLocation :: hpr_gateway_location:loc(), + Retry :: non_neg_integer() +) -> ok | {error, any()}. +send(_PacketUp, _Route, _Timestamp, _GatewayLocation, 0) -> + {error, {gwmp_sup_err, max_retries}}; +send(PacketUp, Route, Timestamp, GatewayLocation, Retry) -> Protocol = protocol_from(Route), WorkerKey = worker_key_from(PacketUp, Protocol), PubKeyBin = hpr_packet_up:gateway(PacketUp), @@ -29,6 +41,9 @@ send(PacketUp, Route, Timestamp, GatewayLocation) -> key => WorkerKey, protocol => Protocol, net_id => hpr_route:net_id(Route) }) of + {error, already_registered} -> + timer:sleep(2), + send(PacketUp, Route, Timestamp, GatewayLocation, Retry - 1); {error, Reason} = Err -> lager:error( "failed to start http connector for ~s: ~p", diff --git a/src/protocols/http/hpr_http_roaming_worker.erl b/src/protocols/http/hpr_http_roaming_worker.erl index 40574b0..38e31f5 100644 --- a/src/protocols/http/hpr_http_roaming_worker.erl +++ b/src/protocols/http/hpr_http_roaming_worker.erl @@ -87,17 +87,23 @@ init(Args) -> key := Key } = Args, lager:debug("~p init with ~p", [?MODULE, Args]), - true = gproc:add_local_name(Key), - {ok, #state{ - net_id = NetID, - route_id = RouteID, - address = Address, - transaction_id = next_transaction_id(), - send_data_timer = DedupeTimeout, - flow_type = FlowType, - auth_header = Auth, - receiver_nsid = ReceiverNSID - }}. + try gproc:add_local_name(Key) of + true -> + {ok, #state{ + net_id = NetID, + route_id = RouteID, + address = Address, + transaction_id = next_transaction_id(), + send_data_timer = DedupeTimeout, + flow_type = FlowType, + auth_header = Auth, + receiver_nsid = ReceiverNSID + }} + catch + % This will only catch a bad registration + error:badarg -> + {stop, already_registered} + end. handle_call(_Msg, _From, State) -> lager:warning("rcvd unknown call msg: ~p from: ~p", [_Msg, _From]),