elixir-expert

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Elixir Expert

Elixir 专家

Purpose

用途

Provides expertise in Elixir development, Phoenix Framework, and OTP patterns. Covers concurrent programming, real-time features with LiveView, and building fault-tolerant distributed systems on the BEAM VM.
提供Elixir开发、Phoenix Framework及OTP模式相关专业知识。涵盖并发编程、基于LiveView的实时功能,以及在BEAM虚拟机上构建容错分布式系统的内容。

When to Use

使用场景

  • Building Elixir applications
  • Developing Phoenix web applications
  • Implementing real-time features with LiveView
  • Using OTP patterns (GenServer, Supervisor)
  • Building distributed systems on BEAM
  • Designing fault-tolerant architectures
  • Working with Ecto for database access
  • 构建Elixir应用
  • 开发Phoenix Web应用
  • 使用LiveView实现实时功能
  • 使用OTP模式(GenServer、Supervisor)
  • 在BEAM上构建分布式系统
  • 设计容错架构
  • 使用Ecto进行数据库访问

Quick Start

快速开始

Invoke this skill when:
  • Building Elixir applications
  • Developing Phoenix web applications
  • Implementing real-time features with LiveView
  • Using OTP patterns
  • Designing fault-tolerant systems
Do NOT invoke when:
  • Building Ruby on Rails apps (use rails-expert)
  • Building Node.js backends (use javascript-pro)
  • Building Python backends (use python-pro)
  • Infrastructure automation (use terraform-engineer)
调用此技能的场景:
  • 构建Elixir应用
  • 开发Phoenix Web应用
  • 使用LiveView实现实时功能
  • 使用OTP模式
  • 设计容错系统
请勿调用的场景:
  • 构建Ruby on Rails应用(请使用rails-expert)
  • 构建Node.js后端(请使用javascript-pro)
  • 构建Python后端(请使用python-pro)
  • 基础设施自动化(请使用terraform-engineer)

Decision Framework

决策框架

Concurrency Pattern:
├── Stateful process → GenServer
├── Async work → Task
├── Background job → Oban or Task.Supervisor
├── Event streaming → GenStage / Broadway
├── Real-time UI → Phoenix LiveView
└── External service → Retry with exponential backoff

Supervision Strategy:
├── Process can crash independently → one_for_one
├── Processes depend on each other → one_for_all
├── Ordered restart needed → rest_for_one
└── Dynamic children → DynamicSupervisor
并发模式:
├── 有状态进程 → GenServer
├── 异步工作 → Task
├── 后台任务 → Oban 或 Task.Supervisor
├── 事件流 → GenStage / Broadway
├── 实时UI → Phoenix LiveView
└── 外部服务 → 指数退避重试

监督策略:
├── 进程可独立崩溃 → one_for_one
├── 进程相互依赖 → one_for_all
├── 需要有序重启 → rest_for_one
└── 动态子进程 → DynamicSupervisor

Core Workflows

核心工作流

1. Phoenix Application Setup

1. Phoenix应用搭建

  1. Generate Phoenix project
  2. Configure database with Ecto
  3. Define schemas and migrations
  4. Create contexts for business logic
  5. Build controllers or LiveViews
  6. Add authentication
  7. Deploy with releases
  1. 生成Phoenix项目
  2. 使用Ecto配置数据库
  3. 定义模式(schema)和迁移(migration)
  4. 为业务逻辑创建上下文(context)
  5. 构建控制器或LiveView
  6. 添加认证功能
  7. 通过发布版本部署

2. OTP Application Design

2. OTP应用设计

  1. Identify stateful components
  2. Design supervision tree
  3. Implement GenServers for state
  4. Add proper error handling
  5. Implement graceful shutdown
  6. Test supervision strategies
  1. 识别有状态组件
  2. 设计监督树
  3. 实现用于状态管理的GenServer
  4. 添加完善的错误处理
  5. 实现优雅关闭
  6. 测试监督策略

3. Real-Time with LiveView

3. 基于LiveView的实时功能

  1. Generate LiveView module
  2. Define assigns and state
  3. Implement handle_event callbacks
  4. Use pubsub for broadcasts
  5. Optimize with temporary_assigns
  6. Add JS hooks if needed
  1. 生成LiveView模块
  2. 定义assigns和状态
  3. 实现handle_event回调
  4. 使用pubsub进行广播
  5. 使用temporary_assigns优化性能
  6. 按需添加JS钩子

Best Practices

最佳实践

  • Let it crash - design for failure recovery
  • Use supervision trees for fault tolerance
  • Keep GenServer state minimal
  • Use contexts to organize business logic
  • Prefer immutable data transformations
  • Test concurrent code with async: true
  • 允许崩溃 - 针对故障恢复进行设计
  • 使用监督树实现容错
  • 保持GenServer的状态最小化
  • 使用上下文组织业务逻辑
  • 优先选择不可变数据转换
  • 使用async: true测试并发代码

Anti-Patterns

反模式

Anti-PatternProblemCorrect Approach
Large GenServer stateMemory and serializationExternal storage, ETS
Defensive codingHides bugsLet it crash, supervise
Blocking GenServerProcess bottleneckAsync tasks for I/O
No supervisionUnrecoverable crashesProper supervision tree
Mutable mindsetBugs and race conditionsEmbrace immutability
反模式问题正确做法
庞大的GenServer状态内存与序列化问题使用外部存储、ETS
防御式编码隐藏bug允许崩溃,使用监督机制
阻塞式GenServer进程瓶颈为I/O操作使用异步任务
无监督机制崩溃后无法恢复构建合理的监督树
可变数据思维引发bug和竞态条件拥抱不可变性