{"id":32168064,"url":"https://github.com/nyo16/counterex","last_synced_at":"2026-02-18T21:02:40.991Z","repository":{"id":57485925,"uuid":"242605245","full_name":"nyo16/CounterEx","owner":"nyo16","description":"High-performance counter library with pluggable backends and namespace support.","archived":false,"fork":false,"pushed_at":"2025-11-16T21:46:19.000Z","size":58,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2026-02-15T20:51:07.414Z","etag":null,"topics":["counters","elixir"],"latest_commit_sha":null,"homepage":"","language":"Elixir","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nyo16.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-02-23T23:03:08.000Z","updated_at":"2025-12-01T11:58:27.000Z","dependencies_parsed_at":"2022-09-10T20:01:27.815Z","dependency_job_id":null,"html_url":"https://github.com/nyo16/CounterEx","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/nyo16/CounterEx","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nyo16%2FCounterEx","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nyo16%2FCounterEx/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nyo16%2FCounterEx/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nyo16%2FCounterEx/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nyo16","download_url":"https://codeload.github.com/nyo16/CounterEx/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nyo16%2FCounterEx/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29596125,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-18T20:59:56.587Z","status":"ssl_error","status_checked_at":"2026-02-18T20:58:41.434Z","response_time":162,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["counters","elixir"],"created_at":"2025-10-21T15:44:00.952Z","updated_at":"2026-02-18T21:02:40.986Z","avatar_url":"https://github.com/nyo16.png","language":"Elixir","funding_links":[],"categories":[],"sub_categories":[],"readme":"# CounterEx\n\n**High-performance counter library with pluggable backends and namespace support.**\n\n[![Build Status](https://github.com/nyo16/CounterEx/workflows/CI/badge.svg)](https://github.com/nyo16/CounterEx/actions)\n[![Hex.pm](https://img.shields.io/hexpm/v/counter_ex.svg)](https://hex.pm/packages/counter_ex)\n[![License](https://img.shields.io/hexpm/l/counter_ex.svg)](https://github.com/nyo16/CounterEx/blob/master/LICENSE.md)\n\nCounterEx provides atomic counter operations with three backend options:\n- **ETS** (default): Dynamic counters, unlimited capacity\n- **Atomics**: Fixed capacity, ultra-fast operations\n- **Counters**: Fixed capacity, write-optimized\n\n## Features\n\n- ✨ **Pluggable Backends**: Choose between ETS, Erlang Atomics, or Counters\n- 🏷️ **Namespace Support**: Organize counters into logical groups\n- ⚡ **High Performance**: Up to 291K operations/second (4 parallel workers)\n- 🔒 **Atomic Operations**: Thread-safe increment, set, compare-and-swap\n- 📊 **Observability**: Built-in Telemetry integration\n- 🎯 **Simple API**: Clean, intuitive function interface\n- 🧪 **Well Tested**: Comprehensive test suite with \u003e90% coverage\n\n## Installation\n\nAdd `counter_ex` to your list of dependencies in `mix.exs`:\n\n```elixir\ndef deps do\n  [\n    {:counter_ex, \"~\u003e 0.2.0\"}\n  ]\nend\n```\n\n## Quick Start\n\n```elixir\n# Add to your supervision tree\ndef start(_type, _args) do\n  children = [\n    {CounterEx, []}  # Uses ETS backend by default\n  ]\n\n  Supervisor.start_link(children, strategy: :one_for_one)\nend\n\n# Basic operations\n{:ok, 1} = CounterEx.inc(:my_counter)\n{:ok, 2} = CounterEx.inc(:my_counter)\n{:ok, 2} = CounterEx.get(:my_counter)\n\n# Use namespaces to organize counters\n{:ok, 1} = CounterEx.inc(:http, :requests)\n{:ok, 1} = CounterEx.inc(:http, :errors)\n{:ok, 1} = CounterEx.inc(:db, :queries)\n\n# Get all counters in a namespace\n{:ok, counters} = CounterEx.all(:http)\n# =\u003e %{requests: 1, errors: 1}\n```\n\n## Backend Selection\n\n### ETS (Default) - Best for Dynamic Counters\n\n```elixir\n{CounterEx, backend: CounterEx.Backend.ETS}\n```\n\n**Use when:**\n- You don't know how many counters you'll need\n- Counter names/keys are dynamic\n- You want the simplest solution\n\n**Performance:** ~256K inc/s, ~270K get/s (4 workers on M4 Max)\n\n### Atomics - Best for Maximum Performance\n\n```elixir\n{CounterEx,\n  backend: CounterEx.Backend.Atomics,\n  backend_opts: [capacity: 10_000]\n}\n```\n\n**Use when:**\n- You have a known, bounded set of counters\n- You need maximum performance\n- You can estimate capacity upfront\n\n**Performance:** ~253K inc/s, ~291K get/s (4 workers on M4 Max)\n\n### Counters - Best for Write-Heavy Workloads\n\n```elixir\n{CounterEx,\n  backend: CounterEx.Backend.Counters,\n  backend_opts: [capacity: 5_000]\n}\n```\n\n**Use when:**\n- You have write-heavy workloads\n- Many concurrent processes incrementing counters\n- Write performance is more critical than read performance\n\n**Performance:** ~248K inc/s, ~269K get/s (4 workers on M4 Max)\n\nSee the [Backend Comparison Guide](guides/backends.md) for detailed comparison.\n\n## API Overview\n\n### Basic Operations\n\n```elixir\n# Increment\n{:ok, 1} = CounterEx.inc(:counter)\n{:ok, 11} = CounterEx.inc(:counter, 10)\n{:ok, 105} = CounterEx.inc(:counter, 5, 100)  # with default value\n\n# Get\n{:ok, 105} = CounterEx.get(:counter)\n{:ok, nil} = CounterEx.get(:nonexistent)\n\n# Set\n{:ok, 42} = CounterEx.set(:counter, 42)\n\n# Reset\n{:ok, 0} = CounterEx.reset(:counter)\n{:ok, 10} = CounterEx.reset(:counter, 10)\n\n# Delete\n:ok = CounterEx.delete(:counter)\n```\n\n### Namespace Operations\n\n```elixir\n# Increment in namespace\n{:ok, 1} = CounterEx.inc(:metrics, :requests, 1, 0)\n\n# Get from namespace\n{:ok, 1} = CounterEx.get(:metrics, :requests)\n\n# Get all counters in namespace\n{:ok, %{requests: 1, errors: 0}} = CounterEx.all(:metrics)\n\n# Delete entire namespace\n:ok = CounterEx.delete_namespace(:metrics)\n```\n\n### Advanced Operations\n\n```elixir\n# Compare-and-swap\n{:ok, 10} = CounterEx.set(:counter, 10)\n{:ok, 20} = CounterEx.compare_and_swap(:counter, 10, 20)  # succeeds\n{:error, :mismatch, 20} = CounterEx.compare_and_swap(:counter, 10, 30)  # fails\n\n# Get backend info\n{:ok, info} = CounterEx.info()\n# %{\n#   type: :ets,\n#   counters_count: 42,\n#   namespaces: [:default, :http, :db],\n#   ...\n# }\n```\n\n## Use Cases\n\n### HTTP Request Tracking\n\n```elixir\ndefmodule MyApp.Metrics do\n  def track_request(status) do\n    CounterEx.inc(:http, :total_requests)\n\n    case status do\n      s when s in 200..299 -\u003e CounterEx.inc(:http, :success)\n      s when s in 400..499 -\u003e CounterEx.inc(:http, :client_errors)\n      s when s in 500..599 -\u003e CounterEx.inc(:http, :server_errors)\n    end\n  end\n\n  def get_metrics do\n    CounterEx.all(:http)\n  end\nend\n```\n\n### Rate Limiting\n\n```elixir\ndefmodule MyApp.RateLimiter do\n  @max_requests 100\n\n  def check_rate_limit(user_id) do\n    key = :\"user_#{user_id}\"\n\n    case CounterEx.get(:rate_limits, key) do\n      {:ok, count} when count \u003e= @max_requests -\u003e\n        {:error, :rate_limit_exceeded}\n\n      {:ok, _count} -\u003e\n        {:ok, _} = CounterEx.inc(:rate_limits, key)\n        :ok\n\n      {:ok, nil} -\u003e\n        {:ok, _} = CounterEx.inc(:rate_limits, key, 1, 0)\n        :ok\n    end\n  end\nend\n```\n\n## Configuration\n\n### Periodic Sweep\n\nClear all counters periodically:\n\n```elixir\n{CounterEx, interval: 60_000}  # Clear every 60 seconds\n```\n\n### Multiple Counter Instances\n\nRun multiple instances with different backends:\n\n```elixir\nchildren = [\n  # Fast metrics with Atomics\n  {CounterEx, name: :metrics, backend: CounterEx.Backend.Atomics, backend_opts: [capacity: 1000]},\n\n  # Dynamic user counters with ETS\n  {CounterEx, name: :users, backend: CounterEx.Backend.ETS}\n]\n```\n\n## Performance\n\nBenchmarks on Apple M4 Max (16 cores, 64GB RAM) with 4 parallel workers:\n\n| Backend | Operation | Throughput | Notes |\n|---------|-----------|------------|-------|\n| **Atomics** | get() | **291K ops/s** | Fastest reads |\n| **Atomics** | inc() | **253K ops/s** | Best overall |\n| **Atomics** | set() | **257K ops/s** | |\n| **ETS** | get() | **270K ops/s** | |\n| **ETS** | inc() | **256K ops/s** | Most flexible |\n| **ETS** | set() | **262K ops/s** | |\n| **Counters** | get() | **269K ops/s** | |\n| **Counters** | inc() | **248K ops/s** | Write-optimized |\n| **Counters** | set() | **256K ops/s** | |\n\n**System Configuration:**\n- **CPU**: Apple M4 Max (12 performance + 4 efficiency cores)\n- **Memory**: 64 GB\n- **OS**: macOS 26.1\n- **Elixir**: 1.18.4\n- **Erlang/OTP**: 28.0.1 (JIT enabled)\n\nRun your own benchmarks:\n\n```elixir\nCounterEx.benchmark(parallel: 4)\n```\n\n## Telemetry\n\nCounterEx emits telemetry events for observability:\n\n```elixir\n:telemetry.attach(\n  \"counter-handler\",\n  [:counter_ex, :backend, :ets, :inc],\n  \u0026handle_event/4,\n  nil\n)\n\ndef handle_event([:counter_ex, :backend, :ets, :inc], measurements, metadata, _config) do\n  # measurements: %{count: 1}\n  # metadata: %{namespace: :http, key: :requests, step: 1}\nend\n```\n\nEvents emitted:\n- `[:counter_ex, :backend, :ets, :inc]`\n- `[:counter_ex, :backend, :ets, :set]`\n- `[:counter_ex, :backend, :ets, :delete]`\n- `[:counter_ex, :backend, :ets, :cas]`\n- ... (similar for atomics and counters backends)\n\n## Migration from 0.1.x\n\nSee the [Migration Guide](guides/migration.md) for upgrading from version 0.1.x.\n\n**Key Changes:**\n- Return values now use `{:ok, value}` tuples\n- `all/0` now returns a map instead of list\n- New namespace support\n- Pluggable backend architecture\n\n## Documentation\n\n- [API Documentation](https://hexdocs.pm/counter_ex)\n- [Backend Comparison Guide](guides/backends.md)\n- [Migration Guide](guides/migration.md)\n\n## Contributing\n\nContributions are welcome! Please feel free to submit a Pull Request.\n\n## License\n\nCopyright 2025 © nyo16\n\nLicensed under the Apache License, Version 2.0. See [LICENSE.md](LICENSE.md) for details.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnyo16%2Fcounterex","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnyo16%2Fcounterex","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnyo16%2Fcounterex/lists"}