Skip to content

Commit

Permalink
support statuscode and action in retryfunc
Browse files Browse the repository at this point in the history
  • Loading branch information
danielxiaoran committed Feb 1, 2019
1 parent 3bc92d3 commit 641c3d6
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 7 deletions.
8 changes: 4 additions & 4 deletions tablestore/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func getNextPause(tableStoreClient *TableStoreClient, err error, count uint, end
}
var retry bool
if otsErr, ok := err.(*OtsError); ok {
retry = shouldRetry(tableStoreClient, otsErr.Code, otsErr.Message, action)
retry = shouldRetry(tableStoreClient, otsErr.Code, otsErr.Message, action, otsErr.HttpStatusCode)
} else {
if err == io.EOF || err == io.ErrUnexpectedEOF || //retry on special net error contains EOF or reset
strings.Contains(err.Error(), io.EOF.Error()) ||
Expand All @@ -185,9 +185,9 @@ func getNextPause(tableStoreClient *TableStoreClient, err error, count uint, end
return 0
}

func shouldRetry(tableStoreClient *TableStoreClient, errorCode string, errorMsg string, action string) bool {
func shouldRetry(tableStoreClient *TableStoreClient, errorCode string, errorMsg string, action string, statusCode int) bool {
if tableStoreClient.CustomizedRetryFunc != nil {
if tableStoreClient.CustomizedRetryFunc(errorCode, errorMsg) == true {
if tableStoreClient.CustomizedRetryFunc(errorCode, errorMsg, action, statusCode) == true {
return true
}
}
Expand All @@ -203,7 +203,7 @@ func shouldRetry(tableStoreClient *TableStoreClient, errorCode string, errorMsg
return false
}

type CustomizedRetryNotMatterActions func(errorCode string, errorMsg string) bool
type CustomizedRetryNotMatterActions func(errorCode string, errorMsg string, action string, httpStatus int) bool

func retryNotMatterActions(errorCode string, errorMsg string) bool {
if errorCode == ROW_OPERATION_CONFLICT || errorCode == NOT_ENOUGH_CAPACITY_UNIT ||
Expand Down
2 changes: 2 additions & 0 deletions tablestore/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ type OtsError struct {
Code string
Message string
RequestId string

HttpStatusCode int
}

func (e *OtsError) Error() string {
Expand Down
2 changes: 1 addition & 1 deletion tablestore/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestOtsError_Error(t *testing.T) {

oldErrStr := fmt.Errorf("%s %s %s", *pbErr.Code, *pbErr.Message, reqId)

otsErr := pbErrToOtsError(pbErr, reqId)
otsErr := pbErrToOtsError(500, pbErr, reqId)

if otsErr.Error() != oldErrStr.Error() {
t.Errorf("error string not equal, old %s new %s", oldErrStr.Error(), otsErr.Error())
Expand Down
6 changes: 4 additions & 2 deletions tablestore/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ func (otsClient *TableStoreClient) postReq(req *http.Request, url string) ([]byt
if errUm != nil {
retErr = rawHttpToOtsError(resp.StatusCode, body, reqId)
} else {
retErr = pbErrToOtsError(perr, reqId)
retErr = pbErrToOtsError(resp.StatusCode, perr, reqId)
}
return nil, retErr, reqId
}
Expand All @@ -595,6 +595,7 @@ func rawHttpToOtsError(code int, body []byte, reqId string) *OtsError {
oerr := &OtsError{
Message: string(body),
RequestId: reqId,
HttpStatusCode: code,
}
if code >= 500 && code < 600 {
oerr.Code = SERVER_UNAVAILABLE
Expand All @@ -604,11 +605,12 @@ func rawHttpToOtsError(code int, body []byte, reqId string) *OtsError {
return oerr
}

func pbErrToOtsError(pbErr *otsprotocol.Error, reqId string) *OtsError {
func pbErrToOtsError(statusCode int, pbErr *otsprotocol.Error, reqId string) *OtsError {
return &OtsError{
Code: pbErr.GetCode(),
Message: pbErr.GetMessage(),
RequestId: reqId,
HttpStatusCode : statusCode,
}
}

Expand Down

0 comments on commit 641c3d6

Please sign in to comment.