Skip to content

Instantly share code, notes, and snippets.

@schmalz
Created September 13, 2017 11:58
Show Gist options
  • Save schmalz/792f0a056e8d1f74112a2af1f7fd3c40 to your computer and use it in GitHub Desktop.
Save schmalz/792f0a056e8d1f74112a2af1f7fd3c40 to your computer and use it in GitHub Desktop.
Designing for Scalability with Erlang/OTP - Ch 2 - HLR Module - Elixir
defmodule HLR do
@moduledoc"""
Maintain the associations between Mobile Subscriber Integrated Services Digital Network (MSISDN) numbers and process
identifiers (PID).
"""
def new() do
:ets.new(:msisdn_pid, [:named_table])
:ets.new(:pid_msisdn, [:named_table])
:ok
end
@doc"""
Associate an MSISDN number, `ms`, with the current process.
"""
def attach(ms) do
:ets.insert(:msisdn_pid, {ms, self()})
:ets.insert(:pid_msisdn, {self(), ms})
end
@doc"""
Disassociate the current process from its MSISDN number.
"""
def detach() do
case :ets.lookup(:pid_msisdn, self()) do
[{pid, ms}] ->
:ets.delete(:pid_msisdn, pid)
:ets.delete(:msisdn_pid, ms)
[] ->
:ok
end
end
@doc"""
Lookup the PID associated with an MSISDN `ms`.
"""
def lookup_id(ms) do
case :ets.lookup(:msisdn_pid, ms) do
[] ->
{:error, :invalid}
[{^ms, pid}] ->
{:ok, pid}
end
end
@doc"""
Lookup the MSISDN associated with a PID.
"""
def lookup_ms(pid) do
case :ets.lookup(:pid_msisdn, pid) do
[] ->
{:error, :invalid}
[{^pid, ms}] ->
{:ok, ms}
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment