diff --git a/.formatter.exs b/.formatter.exs index d2cda26..7f59b1f 100644 --- a/.formatter.exs +++ b/.formatter.exs @@ -1,4 +1,4 @@ # Used by "mix format" [ - inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] + inputs: ["{mix,.formatter}.exs", "{lib,test}/**/*.{ex,exs}"] ] diff --git a/CHANGELOG.md b/CHANGELOG.md index 6c4ff9d..59ee71d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Unreleased +- make Hammer startup explicit + ## 6.2.1 - 2024-02-23 - Fix issue in OTP 26 and Elixir 1.15 by not using to_existing_atom in configuration diff --git a/config/config.exs b/config/config.exs deleted file mode 100644 index 86d7cb7..0000000 --- a/config/config.exs +++ /dev/null @@ -1,12 +0,0 @@ -# This file is responsible for configuring your application -# and its dependencies with the aid of the Mix.Config module. -import Config - -config :hammer, - backend: - {Hammer.Backend.ETS, - [ - ets_table_name: :hammer_backend_ets_buckets, - expiry_ms: 60_000 * 60 * 2, - cleanup_interval_ms: 60_000 * 2 - ]} diff --git a/lib/hammer/application.ex b/lib/hammer/application.ex deleted file mode 100644 index d406d96..0000000 --- a/lib/hammer/application.ex +++ /dev/null @@ -1,68 +0,0 @@ -defmodule Hammer.Application do - @moduledoc """ - Hammer application, responsible for starting the backend worker pools. - - Configured with the `:hammer` environment key: - - - `:backend`, Either a tuple of `{module, config}`, or a keyword-list - of separate, named backends. Examples: - `{Hammer.Backend.ETS, []}`, `[ets: {Hammer.Backend.ETS, []}, ...]` - - `:suppress_logs`, if set to `true`, stops all log messages from Hammer - - ### General Backend Options - - Different backends take different options, but all will accept the following - options, and with the same effect: - - - `:expiry_ms` (int): expiry time in milliseconds, after which a bucket will - be deleted. The exact mechanism for cleanup will vary by backend. This configuration - option is mandatory - - `:pool_size` (int): size of the backend worker pool (default=2) - - `:pool_max_overflow` int(): number of extra workers the pool is permitted - to spawn when under pressure. The worker pool (managed by the poolboy library) - will automatically create and destroy workers up to the max-overflow limit - (default=0) - - Example of a single backend: - - config :hammer, - backend: {Hammer.Backend.ETS, [expiry_ms: 60_000 * 60 * 2]} - - Example of config for multiple-backends: - - config :hammer, - backend: [ - ets: { - Hammer.Backend.ETS, - [ - ets_table_name: :hammer_backend_ets_buckets, - expiry_ms: 60_000 * 60 * 2, - cleanup_interval_ms: 60_000 * 2, - ] - }, - redis: { - Hammer.Backend.Redis, - [ - expiry_ms: 60_000 * 60 * 2, - redix_config: [host: "localhost", port: 6379], - pool_size: 4, - ] - } - ] - - """ - - use Application - require Logger - - def start(_type, _args) do - config = - Application.get_env( - :hammer, - :backend, - {Hammer.Backend.ETS, []} - ) - - Hammer.Supervisor.start_link(config, name: Hammer.Supervisor) - end -end diff --git a/lib/hammer/supervisor.ex b/lib/hammer/supervisor.ex index 3b37fc9..d3c5441 100644 --- a/lib/hammer/supervisor.ex +++ b/lib/hammer/supervisor.ex @@ -4,13 +4,65 @@ defmodule Hammer.Supervisor do Starts a set of poolboy pools based on provided configuration, which are latter called to by the `Hammer` module. - See the Application module for configuration examples. + + Configured with the `:hammer` environment key: + + - `:backend`, Either a tuple of `{module, config}`, or a keyword-list + of separate, named backends. Examples: + `{Hammer.Backend.ETS, []}`, `[ets: {Hammer.Backend.ETS, []}, ...]` + - `:suppress_logs`, if set to `true`, stops all log messages from Hammer + + ### General Backend Options + + Different backends take different options, but all will accept the following + options, and with the same effect: + + - `:expiry_ms` (int): expiry time in milliseconds, after which a bucket will + be deleted. The exact mechanism for cleanup will vary by backend. This configuration + option is mandatory + - `:pool_size` (int): size of the backend worker pool (default=2) + - `:pool_max_overflow` int(): number of extra workers the pool is permitted + to spawn when under pressure. The worker pool (managed by the poolboy library) + will automatically create and destroy workers up to the max-overflow limit + (default=0) + + Example of a single backend: + + children = [ + {Hammer.Supervisor, backend: {Hammer.Backend.ETS, [expiry_ms: 60_000 * 60 * 2]}} + ] + + Example of config for multiple-backends: + + children = [ + {Hammer.Supervisor, + backend: [ + ets: { + Hammer.Backend.ETS, + [ + ets_table_name: :hammer_backend_ets_buckets, + expiry_ms: 60_000 * 60 * 2, + cleanup_interval_ms: 60_000 * 2 + ] + }, + redis: { + Hammer.Backend.Redis, + [ + expiry_ms: 60_000 * 60 * 2, + redix_config: [host: "localhost", port: 6379], + pool_size: 4 + ] + } + ]} + ] + """ use Supervisor - def start_link(config, opts) do - Supervisor.start_link(__MODULE__, config, opts) + def start_link(config) do + backend = Keyword.fetch!(config, :backend) + Supervisor.start_link(__MODULE__, backend, name: Hammer.Supervisor) end # Single backend diff --git a/mix.exs b/mix.exs index b37b064..565e93b 100644 --- a/mix.exs +++ b/mix.exs @@ -20,8 +20,7 @@ defmodule Hammer.Mixfile do end def application do - # Specify extra applications you'll use from Erlang/Elixir - [mod: {Hammer.Application, []}, extra_applications: [:logger, :runtime_tools]] + [extra_applications: []] end defp deps do diff --git a/test/hammer_test.exs b/test/hammer_test.exs index d07177f..d0d2feb 100644 --- a/test/hammer_test.exs +++ b/test/hammer_test.exs @@ -1,5 +1,5 @@ defmodule HammerTest do - use ExUnit.Case, async: false + use ExUnit.Case setup _context do pool = Hammer.Utils.pool_name() diff --git a/test/hammer_util_test.exs b/test/hammer_util_test.exs index e7939b4..dc14faa 100644 --- a/test/hammer_util_test.exs +++ b/test/hammer_util_test.exs @@ -1,6 +1,21 @@ defmodule UtilsTest do use ExUnit.Case + setup do + start_supervised!( + {Hammer.Supervisor, + backend: [ + ets: + {Hammer.Backend.ETS, + ets_table_name: :utils_test, + expiry_ms: 60_000 * 60 * 2, + cleanup_interval_ms: 60_000 * 2} + ]} + ) + + :ok + end + test "timestamp" do assert is_integer(Hammer.Utils.timestamp()) end