Skip to content

Commit 9b4c409

Browse files
author
jojoliang
committed
crypto add kms request retry
1 parent 13ccff3 commit 9b4c409

File tree

1 file changed

+62
-14
lines changed

1 file changed

+62
-14
lines changed

crypto/master_kms_cipher.go

Lines changed: 62 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile"
1010
kms "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/kms/v20190118"
1111
"github.com/tencentyun/cos-go-sdk-v5"
12+
"time"
1213
)
1314

1415
var (
@@ -19,6 +20,11 @@ type MasterKMSCipher struct {
1920
Client *kms.Client
2021
KmsId string
2122
MatDesc string
23+
Conf MasterKMSCipherConf
24+
}
25+
type MasterKMSCipherConf struct {
26+
Retry int
27+
RetryInternal int64 // ms
2228
}
2329

2430
type KMSClientOptions = func(*profile.HttpProfile)
@@ -29,6 +35,20 @@ func KMSEndpoint(endpoint string) KMSClientOptions {
2935
}
3036
}
3137

38+
type MasterKMSCipherOptions = func(*MasterKMSCipher)
39+
40+
func KMSRetry(retry int) MasterKMSCipherOptions {
41+
return func(c *MasterKMSCipher) {
42+
c.Conf.Retry = retry
43+
}
44+
}
45+
46+
func KMSRetryInternal(internal int64) MasterKMSCipherOptions {
47+
return func(c *MasterKMSCipher) {
48+
c.Conf.RetryInternal = internal
49+
}
50+
}
51+
3252
func NewKMSClient(cred *cos.Credential, region string, opt ...KMSClientOptions) (*kms.Client, error) {
3353
if cred == nil {
3454
fmt.Errorf("credential is nil")
@@ -48,11 +68,10 @@ func NewKMSClient(cred *cos.Credential, region string, opt ...KMSClientOptions)
4868
return client, err
4969
}
5070

51-
func CreateMasterKMS(client *kms.Client, kmsId string, desc map[string]string) (MasterCipher, error) {
71+
func CreateMasterKMS(client *kms.Client, kmsId string, desc map[string]string, opts ...MasterKMSCipherOptions) (MasterCipher, error) {
5272
if kmsId == "" || client == nil {
5373
return nil, fmt.Errorf("KMS ID is empty or kms client is nil")
5474
}
55-
var kmsCipher MasterKMSCipher
5675
var jdesc string
5776
if len(desc) > 0 {
5877
bs, err := json.Marshal(desc)
@@ -61,33 +80,62 @@ func CreateMasterKMS(client *kms.Client, kmsId string, desc map[string]string) (
6180
}
6281
jdesc = string(bs)
6382
}
64-
kmsCipher.Client = client
65-
kmsCipher.KmsId = kmsId
66-
kmsCipher.MatDesc = jdesc
67-
return &kmsCipher, nil
83+
kmsCipher := &MasterKMSCipher{
84+
Client: client,
85+
KmsId: kmsId,
86+
MatDesc: jdesc,
87+
Conf: MasterKMSCipherConf{
88+
Retry: 3,
89+
RetryInternal: 100,
90+
},
91+
}
92+
for _, fn := range opts {
93+
fn(kmsCipher)
94+
}
95+
return kmsCipher, nil
6896
}
6997

7098
func (kc *MasterKMSCipher) Encrypt(plaintext []byte) ([]byte, error) {
7199
request := kms.NewEncryptRequest()
72100
request.KeyId = common.StringPtr(kc.KmsId)
73101
request.EncryptionContext = common.StringPtr(kc.MatDesc)
74102
request.Plaintext = common.StringPtr(base64.StdEncoding.EncodeToString(plaintext))
75-
resp, err := kc.Client.Encrypt(request)
76-
if err != nil {
77-
return nil, err
103+
104+
var retry int
105+
for {
106+
resp, err := kc.Client.Encrypt(request)
107+
if err != nil {
108+
retry++
109+
if retry < kc.Conf.Retry {
110+
time.Sleep(time.Duration(kc.Conf.RetryInternal) * time.Millisecond)
111+
continue
112+
}
113+
return nil, err
114+
}
115+
return []byte(*resp.Response.CiphertextBlob), nil
78116
}
79-
return []byte(*resp.Response.CiphertextBlob), nil
117+
return nil, nil
80118
}
81119

82120
func (kc *MasterKMSCipher) Decrypt(ciphertext []byte) ([]byte, error) {
83121
request := kms.NewDecryptRequest()
84122
request.CiphertextBlob = common.StringPtr(string(ciphertext))
85123
request.EncryptionContext = common.StringPtr(kc.MatDesc)
86-
resp, err := kc.Client.Decrypt(request)
87-
if err != nil {
88-
return nil, err
124+
125+
var retry int
126+
for {
127+
resp, err := kc.Client.Decrypt(request)
128+
if err != nil {
129+
retry++
130+
if retry < kc.Conf.Retry {
131+
time.Sleep(time.Duration(kc.Conf.RetryInternal) * time.Millisecond)
132+
continue
133+
}
134+
return nil, err
135+
}
136+
return base64.StdEncoding.DecodeString(*resp.Response.Plaintext)
89137
}
90-
return base64.StdEncoding.DecodeString(*resp.Response.Plaintext)
138+
return nil, nil
91139
}
92140

93141
func (kc *MasterKMSCipher) GetWrapAlgorithm() string {

0 commit comments

Comments
 (0)