Skip to content

Commit 99dbf04

Browse files
author
hellertang
authored
Feat/add timeout env (#826)
* add timeout env * remove debug info * add panic
1 parent e761815 commit 99dbf04

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

tencentcloud/common.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"os"
1010
"os/user"
1111
"reflect"
12+
"strconv"
1213
"strings"
1314
"sync/atomic"
1415
"time"
@@ -27,14 +28,23 @@ type contextLogId string
2728

2829
const logIdKey = contextLogId("logId")
2930

31+
const (
32+
PROVIDER_READ_RETRY_TIMEOUT = "TENCENTCLOUD_READ_RETRY_TIMEOUT"
33+
PROVIDER_WRITE_RETRY_TIMEOUT = "TENCENTCLOUD_WRITE_RETRY_TIMEOUT"
34+
)
35+
3036
var logFirstTime = ""
3137
var logAtomicId int64 = 0
3238

3339
// readRetryTimeout is read retry timeout
34-
const readRetryTimeout = 3 * time.Minute
40+
//const readRetryTimeout = 3 * time.Minute
41+
var readRetry = getEnvDefault(PROVIDER_READ_RETRY_TIMEOUT, 3)
42+
var readRetryTimeout = time.Duration(readRetry) * time.Minute
3543

3644
// writeRetryTimeout is write retry timeout
37-
const writeRetryTimeout = 5 * time.Minute
45+
//const writeRetryTimeout = 5 * time.Minute
46+
var writeRetry = getEnvDefault(PROVIDER_WRITE_RETRY_TIMEOUT, 5)
47+
var writeRetryTimeout = time.Duration(writeRetry) * time.Minute
3848

3949
// InternalError common internalError, do not add in retryableErrorCode,
4050
// because when some product return this error, retry won't fix anything.
@@ -59,6 +69,18 @@ func init() {
5969
logFirstTime = fmt.Sprintf("%d", time.Now().UnixNano()/int64(time.Millisecond))
6070
}
6171

72+
func getEnvDefault(key string, defVal int) int {
73+
val, ex := os.LookupEnv(key)
74+
if !ex {
75+
return defVal
76+
}
77+
int, err := strconv.Atoi(val)
78+
if err != nil {
79+
panic("TENCENTCLOUD_READ_RETRY_TIMEOUT or TENCENTCLOUD_WRITE_RETRY_TIMEOUT must be int.")
80+
}
81+
return int
82+
}
83+
6284
// getLogId get logId for trace, return a new logId if ctx is nil
6385
func getLogId(ctx context.Context) string {
6486
if ctx != nil {

0 commit comments

Comments
 (0)