Elixir otp pro

From findscripter/everything-skills · View interactive version

Elixir otp pro is an AI agent skill from the GitHub repository findscripter/everything-skills. 当用 Elixir/OTP 构建高并发容错或分布式系统、设计监督树或开发 Phoenix LiveView 实时应用时使用;产出 OTP 应用(GenServer/Supervisor)、Phoenix 上下文与 ExUnit 测试;不适用于其他语言运行时、仅需基础语法或无法改动 mix 工程;触发词:GenServer、监督树、Phoenix LiveView、let it crash、BEAM

How to use this skill

Skill file

何时使用

适用:

不该用(负边界):

步骤

  1. 对齐前提:确认 Elixir/OTP 版本、运行时与部署约束(release、容器、集群拓扑)。
  2. 进程设计:按隔离边界划分进程,画出监督树;选 GenServer 还是 Task/Agent,定 restart 策略(:one_for_one/:rest_for_one/:one_for_all)。
  3. 实现:用模式匹配 + 卫语句替代条件分支;状态走不可变数据;副作用进程化。
  4. 测试:ExUnit 配 doctest,能 async: true 就并发跑;用 StreamData 做属性测试。
  5. 优化:先测后调——用 :observer/:recon 定位瓶颈,再调进程数、邮箱与 ETS。

指令

示例

最小 GenServer 骨架(状态 + 调用,配 doctest 与 spec):

defmodule Counter do
  use GenServer

  # Client API
  @spec start_link(integer()) :: GenServer.on_start()
  def start_link(initial), do: GenServer.start_link(__MODULE__, initial, name: __MODULE__)

  @spec bump() :: integer()
  def bump, do: GenServer.call(__MODULE__, :bump)

  # Server callbacks
  @impl true
  def init(initial), do: {:ok, initial}

  @impl true
  def handle_call(:bump, _from, count), do: {:reply, count + 1, count + 1}
end

监督树(:one_for_one,子进程崩溃只重启自己):

children = [{Counter, 0}]
Supervisor.start_link(children, strategy: :one_for_one)

典型请求:

注意事项

互见


采编自 sickn33/antigravity-awesome-skills(MIT)。

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.