日本熟妇hd丰满老熟妇,中文字幕一区二区三区在线不卡 ,亚洲成片在线观看,免费女同在线一区二区

文檔

錯誤處理

更新時間:

表格存儲Go SDK目前采用“異常”的方式處理錯誤。本文介紹了表格存儲錯誤處理方式、異常處理信息和出錯時的重試策略。

方式

表格存儲Go SDK目前采用“異常”的方式處理錯誤,如果調用接口沒有拋出異常,則說明操作成功,否則失敗。

說明

批量相關接口,例如BatchGetRow和BatchWriteRow不僅需要判斷是否有異常,還需要檢查每行的狀態是否成功,只有全部成功后才能保證整個接口調用是成功的。

異常

在使用表格存儲Go SDK時,異常通常作為方法返回值的第二個參數返回。因此,在獲取返回數據前,您需要檢查err參數是否有值。

如果是表格存儲服務端報錯,err中會包含requestId,它是一個用于唯一標識該次請求的UUID。如果您無法解決問題,請記錄此requestId提交工單或者加入釘釘群36165029092(表格存儲技術交流群-3)進行咨詢

異常處理的代碼示例如下:

client := tablestore.NewClient(endpoint, instanceName, accessKeyId, accessKeySecret)
listTables, err := client.ListTable()
if err != nil {
    // 異常處理。
    fmt.Println(err.Error())
} else {
    // 無異常。
    for _, table := range listTables.TableNames {
        fmt.Println("TableName: ", table)
    }
}

重試

Go SDK提供了默認重試策略,您也可以根據需要自定義重試邏輯。

默認重試策略

當發生流控類錯誤或者是讀操作相關的服務端內部錯誤時,Go SDK會進行退避重試,默認的重試最大次數為10次,默認的重試總時長為5秒。您可以通過修改tablestore.TableStoreConfig來設置默認重試的參數。具體參數說明請參見下表。

參數

說明

默認值

RetryTimes

最大的重試次數。

10

MaxRetryTime

重試最大總時長。

5s

DefaultRetryInterval

指數退避重試策略的抖動值,以避免多個故障客戶端在同一時間點發起重試請求。

10 ms

MaxRetryInterval

兩次重試之間的最大時間間隔。

320 ms

Transport

管理HTTP客戶端的底層傳輸屬性,默認為nil。

如果設置了該參數,則HTTPTimeout.ConnectionTimeout、MaxIdleConnections、IdleConnTimeout參數不生效。

nil

HTTPTimeout.ConnectionTimeout

HTTP建立新的網絡連接時的超時時長。

15s

HTTPTimeout.RequestTimeout

HTTP客戶端發起請求并等待服務器響應的最長時間。

30s

MaxIdleConnections

HTTP host的最大空閑連接數。

2000

IdleConnTimeout

HTTP host空閑連接在連接池中保持打開狀態但未被復用的最大時長。

25s

用戶自定義重試邏輯

如果您希望在默認重試邏輯的基礎上進行一些改造或者是完全自定義重試邏輯,您可以通過設置TableStoreClient的以下參數實現。

參數

說明

默認值

CustomizedRetryFunc

如果設置了CustomizedRetryFunc,則會優先判斷CustomizedRetryFunc的方法是否進行重試。

  • 如果未設置CustomizedRetryFunc,即CustomizedRetryFunc為nil,則SDK會執行默認重試邏輯。

  • 如果CustomizedRetryFunc的取值不為nil,且判定值為重試,則SDK會根據設置的自定義重試邏輯進行重試。

  • 如果CustomizedRetryFunc的取值不為nil,且判定值為不重試,則需要根據KeepDefaultRetryStrategyWhileUsingCustomizedRetryFunc的值做進一步判斷。

    • 如果KeepDefaultRetryStrategyWhileUsingCustomizedRetryFunc的取值為false,則不進行重試。

    • 如果KeepDefaultRetryStrategyWhileUsingCustomizedRetryFunc的取值為true,則繼續校驗默認重試邏輯判斷是否進行重試。

nil

KeepDefaultRetryStrategyWhileUsingCustomizedRetryFunc

true

自定義重試邏輯的示例如下:

針對所有錯誤都進行重試

以下示例用于對所有錯誤都進行重試。

func alwaysRetry(errorCode string, errorMsg string, action string, httpStatus int) bool {
	return true
}
func main() {
    client := tablestore.NewClient(endpoint, instanceName, accessKeyId, accessKeySecret)
  	client.CustomizedRetryFunc = alwaysRetry
    // do something
}

針對所有錯誤都不進行重試

以下示例用于對所有錯誤都不進行重試。

func alwaysNotRetry(errorCode string, errorMsg string, action string, httpStatus int) bool {
	return false
}
func main() {
    client := tablestore.NewClient(endpoint, instanceName, accessKeyId, accessKeySecret)
  	client.CustomizedRetryFunc = alwaysNotRetry
    client.KeepDefaultRetryStrategyWhileUsingCustomizedRetryFunc = false
    // do something
}

重試時進行回調

如果需要在SDK進行重試時進行一些預定義的操作,您可以通過設置TableStoreClient的以下參數實現。

參數

說明

默認值

RetryNotify

SDK重試時會觸發的回調方法。

nil

以下示例展示了如何針對每一次請求設置一個業務側的traceID,在發生重試時打印此traceID。

func userRetryNotify(traceId, requestId string, err error, action string, backoffDuration time.Duration) {
    // 用戶自定義邏輯,在重試時會觸發調用。
    fmt.Println("Retry for traceId: " + traceId + ", timestamp: " + strconv.FormatInt(time.Now().UnixNano(), 10))
}

func alwaysRetry(errorCode string, errorMsg string, action string, httpStatus int) bool {
    return true
}

func main() {
    client := tablestore.NewClient(endpoint, instanceName, accessKeyId, accessKeySecret)
    client.CustomizedRetryFunc = alwaysRetry
    client.RetryNotify = userRetryNotify

    request := &tablestore.DescribeTableRequest{TableName: "tableNotExist"}
    // 設置請求的業務側traceID。
    request.ExtraRequestInfo.SetTraceID("test_TraceId_" + strconv.FormatInt(time.Now().UnixNano(), 10))
    // do something
    res, err := client.DescribeTable(request)

    if err != nil {
        fmt.Println(err.Error())
    } else {
        fmt.Println(res.ResponseInfo.RequestId)
    }
}

返回結果示例如下:

Retry for traceId: test_TraceId_1711097752255675000, timestamp: 1711097752655394000
Retry for traceId: test_TraceId_1711097752255675000, timestamp: 1711097752683437000
Retry for traceId: test_TraceId_1711097752255675000, timestamp: 1711097752708603000
Retry for traceId: test_TraceId_1711097752255675000, timestamp: 1711097752760519000
Retry for traceId: test_TraceId_1711097752255675000, timestamp: 1711097752814590000
Retry for traceId: test_TraceId_1711097752255675000, timestamp: 1711097752916539000
Retry for traceId: test_TraceId_1711097752255675000, timestamp: 1711097753110943000
Retry for traceId: test_TraceId_1711097752255675000, timestamp: 1711097753454311000
Retry for traceId: test_TraceId_1711097752255675000, timestamp: 1711097753798531000
Retry for traceId: test_TraceId_1711097752255675000, timestamp: 1711097754165411000
OTSObjectNotExist Requested table does not exist. 0006143b-fdd6-5050-10ef-700b045590fc