# rpc错误：代码=先决条件失败 描述=资源版本冲突：期望3，得到5

- **ID:** `go/grpc-failed-precondition-state-mismatch`
- **领域:** go
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

请求违反了先决条件，例如在乐观并发控制场景中资源版本过时。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 1.0 | active | — | — |
| 1.1 | active | — | — |

## 解决方案

1. **Fetch the latest resource version and retry the update.** (90% 成功率)
   ```
   currentResource, err := client.GetResource(ctx, &pb.GetRequest{Id: id})
if err != nil { return err }
req.Version = currentResource.Version
resp, err := client.UpdateResource(ctx, req)
   ```
2. **Implement a retry loop with backoff to handle concurrent updates.** (85% 成功率)
   ```
   for i := 0; i < 3; i++ {
    resp, err := client.UpdateResource(ctx, req)
    if err == nil { return resp }
    if status.Code(err) != codes.FailedPrecondition { return err }
    time.Sleep(100 * time.Millisecond)
    // Refresh resource
}
   ```

## 无效尝试

- **Retry the same request without updating the version.** — The version mismatch will persist, causing the same error. (100% 失败率)
- **Ignore the version and force the update.** — This can lead to data corruption or lost updates. (80% 失败率)
