Don't crash on tcp send errors

This commit is contained in:
Сергей Прохоров 2018-10-20 16:42:04 +02:00
parent e0d586ef3e
commit 8d9c64b58b
No known key found for this signature in database
GPG key ID: 1C570244E4EF3337
2 changed files with 27 additions and 4 deletions

View file

@ -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}}.

View file

@ -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]}}
].