go data_error ai_generated true

rpc 错误:code = AlreadyExists desc = 资源已存在:id=123

rpc error: code = AlreadyExists desc = resource already exists: id=123

ID: go/grpc-already-exists-duplicate

其他格式: JSON · Markdown 中文 · English
80%修复率
88%置信度
0证据数
2024-10-12首次发现

版本兼容性

版本状态引入弃用备注
1.x active — — —

根因分析

客户端尝试创建已存在的资源,通常是由于重复请求或竞态条件。

English

The client attempted to create a resource that already exists, often due to a duplicate request or race condition.

generic

解决方案

  1. 90% 成功率
    // Client sends a unique idempotency key
    // Server checks if key already processed
    if exists, return existing resource
    else create new
  2. 85% 成功率
    resp, err := client.Create(ctx, req)
    if status.Code(err) == codes.AlreadyExists {
        // Fetch existing resource
        getResp, err := client.Get(ctx, &GetRequest{Id: req.Id})
        // use getResp
    }

无效尝试

常见但无效的做法:

  1. 80% 失败

    This may violate business logic if the ID is meaningful; also doesn't address the root cause of duplicate requests.

  2. 70% 失败

    The operation didn't create a new resource, leading to inconsistent state.