elixir-expert
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseElixir 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
└── 动态子进程 → DynamicSupervisorCore Workflows
核心工作流
1. Phoenix Application Setup
1. Phoenix应用搭建
- Generate Phoenix project
- Configure database with Ecto
- Define schemas and migrations
- Create contexts for business logic
- Build controllers or LiveViews
- Add authentication
- Deploy with releases
- 生成Phoenix项目
- 使用Ecto配置数据库
- 定义模式(schema)和迁移(migration)
- 为业务逻辑创建上下文(context)
- 构建控制器或LiveView
- 添加认证功能
- 通过发布版本部署
2. OTP Application Design
2. OTP应用设计
- Identify stateful components
- Design supervision tree
- Implement GenServers for state
- Add proper error handling
- Implement graceful shutdown
- Test supervision strategies
- 识别有状态组件
- 设计监督树
- 实现用于状态管理的GenServer
- 添加完善的错误处理
- 实现优雅关闭
- 测试监督策略
3. Real-Time with LiveView
3. 基于LiveView的实时功能
- Generate LiveView module
- Define assigns and state
- Implement handle_event callbacks
- Use pubsub for broadcasts
- Optimize with temporary_assigns
- Add JS hooks if needed
- 生成LiveView模块
- 定义assigns和状态
- 实现handle_event回调
- 使用pubsub进行广播
- 使用temporary_assigns优化性能
- 按需添加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-Pattern | Problem | Correct Approach |
|---|---|---|
| Large GenServer state | Memory and serialization | External storage, ETS |
| Defensive coding | Hides bugs | Let it crash, supervise |
| Blocking GenServer | Process bottleneck | Async tasks for I/O |
| No supervision | Unrecoverable crashes | Proper supervision tree |
| Mutable mindset | Bugs and race conditions | Embrace immutability |
| 反模式 | 问题 | 正确做法 |
|---|---|---|
| 庞大的GenServer状态 | 内存与序列化问题 | 使用外部存储、ETS |
| 防御式编码 | 隐藏bug | 允许崩溃,使用监督机制 |
| 阻塞式GenServer | 进程瓶颈 | 为I/O操作使用异步任务 |
| 无监督机制 | 崩溃后无法恢复 | 构建合理的监督树 |
| 可变数据思维 | 引发bug和竞态条件 | 拥抱不可变性 |