Packages
gnat
1.9.0
1.16.0
1.15.2
1.15.1
1.15.0
1.14.1
1.14.0
1.14.0-rc1
1.14.0-rc0
1.13.1
1.13.0
1.12.1
1.12.0
1.11.1
1.11.0
1.10.2
1.10.2-rc1
1.10.2-rc0
1.10.1
1.10.0
1.9.1
1.9.0
1.8.5
1.8.4
1.8.3
1.8.2
1.8.1
1.7.1
1.7.0
1.6.0
1.5.2
1.5.1
1.5.0
1.4.0
1.3.0
1.3.0-rc0
1.2.1
1.2.0
1.1.3
1.1.2
1.1.1
1.1.0
1.0.1
1.0.0
0.7.0
0.6.1
0.6.0
0.5.5
0.5.4
0.5.3
0.5.2
0.5.1
0.5.0
0.4.2
0.4.1
0.4.0
0.3.4
0.3.3
0.3.2
0.3.1
0.3.0
0.2.0
0.1.3
0.1.2
0.1.1
0.1.0
A nats client in pure elixir. Resilience, Performance, Ease-of-Use.
Current section
Files
Jump to
Current section
Files
lib/gnat/services/service_responder.ex
defmodule Gnat.Services.ServiceResponder do
@moduledoc false
require Logger
alias Gnat.Services.Service
@op_ping "PING"
@op_stats "STATS"
@op_info "INFO"
def maybe_respond(%{topic: topic} = message, service) do
case String.split(topic, ".") do
["$SRV", @op_ping | rest] -> handle_ping(rest, service, message)
["$SRV", @op_info | rest] -> handle_info(rest, service, message)
["$SRV", @op_stats | rest] -> handle_stats(rest, service, message)
_other ->
Logger.error("ServiceResponder received unexpected message #{topic}")
end
end
defp handle_ping(tail, service, %{reply_to: rt, gnat: gnat}) do
if should_respond?(tail, service.name, service.instance_id) do
body = Service.ping(service) |> Jason.encode!()
Gnat.pub(gnat, rt, body)
end
end
defp handle_info(tail, service, %{reply_to: rt, gnat: gnat}) do
if should_respond?(tail, service.name, service.instance_id) do
body = Service.info(service) |> Jason.encode!()
Gnat.pub(gnat, rt, body)
end
end
defp handle_stats(tail, service, %{reply_to: rt, gnat: gnat}) do
if should_respond?(tail, service.name, service.instance_id) do
body = Service.stats(service) |> Jason.encode!()
Gnat.pub(gnat, rt, body)
end
end
@spec should_respond?(list, String.t, String.t) :: boolean()
defp should_respond?(tail, service_name, instance_id) do
case tail do
[] -> true
[^service_name] -> true
[^service_name, ^instance_id] -> true
_ -> false
end
end
end