dbos-golang
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseDBOS Go Best Practices
DBOS Go 最佳实践
Guide for building reliable, fault-tolerant Go applications with DBOS durable workflows.
本指南介绍如何使用 DBOS 持久化工作流构建可靠、容错的 Go 应用。
When to Use
使用场景
Reference these guidelines when:
- Adding DBOS to existing Go code
- Creating workflows and steps
- Using queues for concurrency control
- Implementing workflow communication (events, messages, streams)
- Configuring and launching DBOS applications
- Using the DBOS Client from external applications
- Testing DBOS applications
在以下场景中可参考本指南:
- 为现有 Go 代码添加 DBOS 集成
- 创建工作流与步骤
- 使用队列进行并发控制
- 实现工作流通信(事件、消息、流)
- 配置与启动 DBOS 应用
- 从外部应用使用 DBOS 客户端
- 测试 DBOS 应用
Rule Categories by Priority
按优先级划分的规则类别
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Lifecycle | CRITICAL | |
| 2 | Workflow | CRITICAL | |
| 3 | Step | HIGH | |
| 4 | Queue | HIGH | |
| 5 | Communication | MEDIUM | |
| 6 | Pattern | MEDIUM | |
| 7 | Testing | LOW-MEDIUM | |
| 8 | Client | MEDIUM | |
| 9 | Advanced | LOW | |
| 优先级 | 类别 | 影响程度 | 前缀 |
|---|---|---|---|
| 1 | 生命周期 | 关键 | |
| 2 | 工作流 | 关键 | |
| 3 | 步骤 | 高 | |
| 4 | 队列 | 高 | |
| 5 | 通信 | 中 | |
| 6 | 模式 | 中 | |
| 7 | 测试 | 低-中 | |
| 8 | 客户端 | 中 | |
| 9 | 高级 | 低 | |
Critical Rules
关键规则
Installation
安装
Install the DBOS Go module:
bash
go get github.com/dbos-inc/dbos-transact-golang/dbos@latest安装 DBOS Go 模块:
bash
go get github.com/dbos-inc/dbos-transact-golang/dbos@latestDBOS Configuration and Launch
DBOS 配置与启动
A DBOS application MUST create a context, register workflows, and launch before running any workflows:
go
package main
import (
"context"
"log"
"os"
"time"
"github.com/dbos-inc/dbos-transact-golang/dbos"
)
func main() {
ctx, err := dbos.NewDBOSContext(context.Background(), dbos.Config{
AppName: "my-app",
DatabaseURL: os.Getenv("DBOS_SYSTEM_DATABASE_URL"),
})
if err != nil {
log.Fatal(err)
}
defer dbos.Shutdown(ctx, 30*time.Second)
dbos.RegisterWorkflow(ctx, myWorkflow)
if err := dbos.Launch(ctx); err != nil {
log.Fatal(err)
}
}DBOS 应用必须先创建上下文、注册工作流,然后启动,之后才能运行任何工作流:
go
package main
import (
"context"
"log"
"os"
"time"
"github.com/dbos-inc/dbos-transact-golang/dbos"
)
func main() {
ctx, err := dbos.NewDBOSContext(context.Background(), dbos.Config{
AppName: "my-app",
DatabaseURL: os.Getenv("DBOS_SYSTEM_DATABASE_URL"),
})
if err != nil {
log.Fatal(err)
}
defer dbos.Shutdown(ctx, 30*time.Second)
dbos.RegisterWorkflow(ctx, myWorkflow)
if err := dbos.Launch(ctx); err != nil {
log.Fatal(err)
}
}Workflow and Step Structure
工作流与步骤结构
Workflows are comprised of steps. Any function performing complex operations or accessing external services must be run as a step using :
dbos.RunAsStepgo
func fetchData(ctx context.Context) (string, error) {
resp, err := http.Get("https://api.example.com/data")
if err != nil {
return "", err
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
return string(body), nil
}
func myWorkflow(ctx dbos.DBOSContext, input string) (string, error) {
result, err := dbos.RunAsStep(ctx, fetchData, dbos.WithStepName("fetchData"))
if err != nil {
return "", err
}
return result, nil
}工作流由步骤组成。任何执行复杂操作或访问外部服务的函数都必须通过 以步骤形式运行:
dbos.RunAsStepgo
func fetchData(ctx context.Context) (string, error) {
resp, err := http.Get("https://api.example.com/data")
if err != nil {
return "", err
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
return string(body), nil
}
func myWorkflow(ctx dbos.DBOSContext, input string) (string, error) {
result, err := dbos.RunAsStep(ctx, fetchData, dbos.WithStepName("fetchData"))
if err != nil {
return "", err
}
return result, nil
}Key Constraints
关键约束
- Do NOT start or enqueue workflows from within steps
- Do NOT use uncontrolled goroutines to start workflows - use with queues or
dbos.RunWorkflow/dbos.Gofor concurrent stepsdbos.Select - Workflows MUST be deterministic - non-deterministic operations go in steps
- Do NOT modify global variables from workflows or steps
- All workflows and queues MUST be registered before calling
Launch()
- 请勿在步骤内部启动或入队工作流
- 请勿使用不受控的 goroutine 启动工作流 - 应使用带队列的 或
dbos.RunWorkflow/dbos.Go处理并发步骤dbos.Select - 工作流必须是确定性的 - 非确定性操作应放在步骤中
- 请勿从工作流或步骤中修改全局变量
- 所有工作流与队列必须在调用 之前完成注册
Launch()
How to Use
使用方法
Read individual rule files for detailed explanations and examples:
references/lifecycle-config.md
references/workflow-determinism.md
references/queue-concurrency.md阅读单个规则文件获取详细说明与示例:
references/lifecycle-config.md
references/workflow-determinism.md
references/queue-concurrency.md