|
| 1 | +defmodule Inflex.Database.PoolWorker do |
| 2 | + @moduledoc """ |
| 3 | + single worker responsible for sending stats to the configured database |
| 4 | + """ |
| 5 | + |
| 6 | + use GenServer |
| 7 | + |
| 8 | + require Logger |
| 9 | + |
| 10 | + alias Inflex.Conn.UDP |
| 11 | + alias Inflex.LineProtocol |
| 12 | + |
| 13 | + def child_spec(opts) do |
| 14 | + :poolboy.child_spec(:worker, poolboy_config(opts), opts) |
| 15 | + end |
| 16 | + |
| 17 | + defp poolboy_config(opts) do |
| 18 | + [ |
| 19 | + {:name, opts |> Map.fetch!(:database) |> via_tuple()}, |
| 20 | + {:worker_module, __MODULE__}, |
| 21 | + {:size, Map.get(opts, :pool_size)}, |
| 22 | + {:max_overflow, Map.get(opts, :pool_overflow)} |
| 23 | + ] |
| 24 | + end |
| 25 | + |
| 26 | + @doc false |
| 27 | + def start_link(opts) do |
| 28 | + GenServer.start_link(__MODULE__, opts) |
| 29 | + end |
| 30 | + |
| 31 | + @doc """ |
| 32 | + single pool worker initialization with deferred UDP socket setup |
| 33 | + """ |
| 34 | + def init(opts) do |
| 35 | + Process.send(self(), {:init, opts}, []) |
| 36 | + {:ok, %{}} |
| 37 | + end |
| 38 | + |
| 39 | + @spec worker_pid(database :: String.t()) :: pid() | :full |
| 40 | + @doc """ |
| 41 | + Perform a non-blocking checkout of an available worker for sending data. If no |
| 42 | + worker is available, `:poolboy.checkout/2` will return `:full` |
| 43 | + """ |
| 44 | + def worker_pid(database) do |
| 45 | + :poolboy.checkout( |
| 46 | + via_tuple(database), |
| 47 | + false |
| 48 | + ) |
| 49 | + end |
| 50 | + |
| 51 | + @spec send_points(pid(), points :: [map() | Inflex.Point.t()]) :: :ok |
| 52 | + @doc """ |
| 53 | + Given an identified worker via `worker_pid/1` and points, asynchronously ship |
| 54 | + the points to influx. |
| 55 | + """ |
| 56 | + def send_points(pid, points), do: GenServer.cast(pid, {:send, points}) |
| 57 | + |
| 58 | + @doc false |
| 59 | + def handle_cast({:send, points}, state) do |
| 60 | + send_batch(points, state) |
| 61 | + {:noreply, state} |
| 62 | + end |
| 63 | + |
| 64 | + defp send_batch(points, state) do |
| 65 | + payload = |
| 66 | + points |
| 67 | + |> Stream.map(&LineProtocol.encode/1) |
| 68 | + |> Enum.join("\n") |
| 69 | + |
| 70 | + UDP.write( |
| 71 | + state.udp_socket, |
| 72 | + state.config.host, |
| 73 | + state.config.udp_port, |
| 74 | + payload |
| 75 | + ) |
| 76 | + rescue |
| 77 | + e -> |
| 78 | + Logger.debug(fn -> |
| 79 | + { |
| 80 | + Exception.format(:error, e, System.stacktrace()), |
| 81 | + [points: inspect(points)] |
| 82 | + } |
| 83 | + end) |
| 84 | + after |
| 85 | + # regardless of formatting errors, invalid types, etc, check the worker |
| 86 | + # back into the pool |
| 87 | + :poolboy.checkin(via_tuple(state.config.database), self()) |
| 88 | + end |
| 89 | + |
| 90 | + def handle_info({:init, opts}, _state) do |
| 91 | + udp_opts = |
| 92 | + Map.get( |
| 93 | + opts, |
| 94 | + :udp_conn_opts |
| 95 | + ) |
| 96 | + |
| 97 | + socket = UDP.open(0, udp_opts) |
| 98 | + |
| 99 | + config = |
| 100 | + opts |
| 101 | + |> Map.new() |
| 102 | + |> prepare_host() |
| 103 | + |
| 104 | + {:noreply, %{config: config, udp_socket: socket}} |
| 105 | + end |
| 106 | + |
| 107 | + defp prepare_host(state), |
| 108 | + do: Map.update(state, :host, "", &String.to_charlist/1) |
| 109 | + |
| 110 | + @doc false |
| 111 | + def via_tuple(database) do |
| 112 | + {:via, Registry, {Inflex.Registry, database <> "_pool"}} |
| 113 | + end |
| 114 | + |
| 115 | + def create_point(input) do |
| 116 | + %{ |
| 117 | + measurement: "measure", |
| 118 | + fields: %{ |
| 119 | + input: input, |
| 120 | + value: :rand.uniform(10) |
| 121 | + }, |
| 122 | + tags: %{ |
| 123 | + tag0: |
| 124 | + if :rand.uniform() > 0.5 do |
| 125 | + "val0" |
| 126 | + else |
| 127 | + "val1" |
| 128 | + end |
| 129 | + }, |
| 130 | + timestamp: System.os_time(:nanosecond) |
| 131 | + } |
| 132 | + end |
| 133 | +end |
0 commit comments