Packages

A secure and unique ID system for distributed workloads.

Current section

Files

Jump to
aphora lib aphora config.ex
Raw

lib/aphora/config.ex

# Copyright 2019 Schicksal
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
defmodule Aphora.Config do
@moduledoc """
This module provides a simple interface to the `config.exs` file, by providing a `default` variable fallback.
"""
@default [
datacenter: 0,
worker: 0,
epoch: 1_546_300_800_000
]
@typedoc """
This can be any non-negative integer within the span of `0..31` and represents the `t:datacenter/0` in which the `t:worker/0` resides in.
"""
@typedoc since: "0.1.0"
@type datacenter() :: non_neg_integer()
@doc """
Returns the set unique `t:datacenter/0` identifier from the config, or otherwise the `default` value `0`.
## Examples
iex> Aphora.Config.datacenter()
0
"""
@doc since: "0.1.0"
@spec get_datacenter() :: datacenter()
def get_datacenter, do: Application.get_env(:aphora, :datacenter) || @default[:datacenter]
@typedoc """
This can be any non-negative integer within the span of `0..1_023` and represents the unique `t:worker/0` within a `t:datacenter/0`.
*A `t:worker/0` should **never** be assigned twice with the same ID within the same `t:datacenter/0`.*
"""
@typedoc since: "0.1.0"
@type worker() :: non_neg_integer()
@doc """
Returns the set unique `t:worker/0` identifier from the config, or otherwise the `default` value `0`.
## Examples
iex> Aphora.Config.worker()
0
"""
@doc since: "0.1.0"
@spec get_worker() :: worker()
def get_worker, do: Application.get_env(:aphora, :worker) || @default[:worker]
@typedoc """
This can be any non-negative integer and represents the `t:epoch/0` with which the `t:Aphora.Worker.timestamp/0` is generated.
*`t:epoch/0` should **never** be changed after deploying IDs to prevent duplicates.*
"""
@typedoc since: "0.1.0"
@type epoch() :: non_neg_integer()
@doc """
Returns the set unique `t:epoch/0` identifier from the config, or otherwise the `default` value `1_546_300_800_000`.
## Examples
iex> Aphora.Config.epoch()
1546300800000000
"""
@doc since: "0.1.0"
@spec get_epoch() :: epoch()
def get_epoch, do: Application.get_env(:aphora, :epoch) || @default[:epoch]
end