Don't crash on tcp send errors

parent e0d586ef
......@@ -307,7 +307,16 @@ up_send(Packet, #state{stage = tunnel,
{Encoded, UpCodec1} = mtp_layer:encode_packet(Packet, UpCodec),
mtp_metric:rt([?APP, upstream_send_duration, seconds],
fun() ->
ok = Transport:send(Sock, Encoded)
case Transport:send(Sock, Encoded) of
ok -> ok;
{error, Reason} ->
is_atom(Reason) andalso
mtp_metric:count_inc(
[?APP, upstream_send_error, total], 1,
#{labels => [Reason]}),
lager:warning("Upstream send error: ~p", [Reason]),
throw({stop, normal, S})
end
end),
{ok, S#state{up_codec = UpCodec1}}.
......@@ -316,7 +325,16 @@ down_send(Packet, #state{down_sock = Sock,
{Encoded, DownCodec1} = mtp_layer:encode_packet(Packet, DownCodec),
mtp_metric:rt([?APP, downstream_send_duration, seconds],
fun() ->
ok = gen_tcp:send(Sock, Encoded)
case gen_tcp:send(Sock, Encoded) of
ok -> ok;
{error, Reason} ->
is_atom(Reason) andalso
mtp_metric:count_inc(
[?APP, downstream_send_error, total], 1,
#{labels => [Reason]}),
lager:warning("Downstream send error: ~p", [Reason]),
throw({stop, normal, S})
end
end),
{ok, S#state{down_codec = DownCodec1}}.
......
......@@ -146,6 +146,11 @@ active_metrics() ->
#{duration_unit => seconds,
%% buckets => ?MS_BUCKETS
labels => [listener]
}}
}},
{count, [?APP, upstream_send_error, total],
"Count of tcp send errors to upstream",
#{labels => [listener, reason]}},
{count, [?APP, downstream_send_error, total],
"Count of tcp send errors to downstream",
#{labels => [listener, reason]}}
].
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment