Last active
March 19, 2019 12:24
-
-
Save sneako/8d3f92e5e8c508a2212a338045746698 to your computer and use it in GitHub Desktop.
Kinesis GenServer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule KinesisServer.Worker do | |
use GenServer | |
alias KinesisServer.Writer | |
def start_link(opts \\ []) do | |
GenServer.start_link(__MODULE__, Writer.new(opts)) | |
end | |
@impl true | |
def init(init_arg) do | |
Process.flag(:trap_exit, true) | |
{:ok, init_arg} | |
end | |
def write(record) do | |
:poolboy.transaction(:kinesis_writer_pool, fn pid -> | |
GenServer.cast(pid, {:write, record}) | |
end) | |
end | |
@impl true | |
def handle_cast({:write, record}, state) do | |
{:noreply, Writer.write(state, record)} | |
end | |
@impl true | |
def terminate(reason, state) do | |
Writer.flush(state) | |
:shutdown | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment