AtomicFlags - mutable runtime configuration on top of atomics

A very short and simple library which implements compile-time enums on top of erlang :atomics module.

Use cases

  1. Configuration, where values change in runtime
  2. Configuration, where values are accessed frequently and accessing them become a bottleneck
  3. Inter-process store for some flags, options or parameters.

Example

defmodule FeatureFlags do
  use AtomicFlags, schema: [
    feature_enabled: [false, true]
  ]
end

iex> require FeatureFlags
iex> flags = FeatureFlags.new()
iex> FeatureFlags.set(flags, :feature_enabled, true)
iex> FeatureFlags.get(flags, :feature_enabled)
true

Or with global: true

defmodule GlobalFeatureFlags do
  use AtomicFlags,
    global: true,
    schema: [
      feature: [:disabled, :for_preview, :enabled]
    ]
end

iex> require GlobalFeatureFlags
iex> GlobalFeatureFlags.new_global()
iex> GlobalFeatureFlags.set(:feature, :for_preview)
iex> GlobalFeatureFlags.get(:feature)
:for_preview

My experience

I am using a version of this library in my pet project and it is working fine, showing really good performance, compared to application env ets

2 Likes