Skip to content

Commit dbd95f7

Browse files
committed
perf: Removed the check for message body size to allow for larger messages.
1 parent e6276b6 commit dbd95f7

File tree

6 files changed

+35
-28
lines changed

6 files changed

+35
-28
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## 1.0.6
44
- Added an example of HTTP endpoint subscription in `topic_example.go`.
5+
- Added an example of HTTP authorization in `http_authorization.go`.
6+
- Removed the check for message body size to allow for larger messages.
57

68
## 1.0.5
79
- update the minimum Go version declared in go.mod to fix build failures.

README-CN.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Aliyun MNS Go SDK
22

3-
[![Github version](https://badgen.net/badge/color/1.0.5/green?label=version)](https://badgen.net/badge/color/1.0.5/green?label=version)
3+
[![Github version](https://badgen.net/badge/color/1.0.6/green?label=version)](https://badgen.net/badge/color/1.0.6/green?label=version)
44

55
Aliyun MNS Go SDK 是 MNS 在 Go 编译语言的官方 SDK
66

errors.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ var (
7272
ERR_MNS_MAX_MESSAGE_SIZE_RANGE_ERROR = errors.TN(ALI_MNS_ERR_NS, 128, "max message size is not in range of (1024~65536)")
7373
ERR_MNS_MSG_RETENTION_PERIOD_RANGE_ERROR = errors.TN(ALI_MNS_ERR_NS, 129, "message retention period is not in range of (60~129600)")
7474
ERR_MNS_MSG_VISIBILITY_TIMEOUT_RANGE_ERROR = errors.TN(ALI_MNS_ERR_NS, 130, "message visibility timeout is not in range of (1~43200)")
75-
ERR_MNS_MSG_POOLLING_WAIT_SECONDS_RANGE_ERROR = errors.TN(ALI_MNS_ERR_NS, 131, "message poolling wait seconds is not in range of (0~30)")
75+
ERR_MNS_MSG_POOLLING_WAIT_SECONDS_RANGE_ERROR = errors.TN(ALI_MNS_ERR_NS, 131, "message polling wait seconds is not in range of (0~30)")
7676
ERR_MNS_RET_NUMBER_RANGE_ERROR = errors.TN(ALI_MNS_ERR_NS, 132, "list param of ret number is not in range of (1~1000)")
7777
ERR_MNS_QUEUE_ALREADY_EXIST_AND_HAVE_SAME_ATTR = errors.TN(ALI_MNS_ERR_NS, 133, "mns queue already exist, and the attribute is the same, queue name: {{.name}}")
7878
ERR_MNS_BATCH_OP_FAIL = errors.TN(ALI_MNS_ERR_NS, 136, "mns queue batch operation fail")

example/http_authorization.go

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,19 @@ func main() {
3838

3939
method := "POST"
4040
path := "/api/test"
41-
if authenticate(method, path, headers) {
41+
if authenticateWithHeaderMap(method, path, toLowercaseKeys(headers)) {
4242
fmt.Println("Signature verification succeeded")
4343
} else {
4444
fmt.Println("Signature verification failed")
4545
}
4646
}
4747

48-
func authenticate(method, path string, headers map[string]string) bool {
48+
func authenticateWithResponse(method, path string, resp *http.Response) bool {
49+
headersMap := headersToMap(resp)
50+
return authenticateWithHeaderMap(method, path, headersMap)
51+
}
52+
53+
func authenticateWithHeaderMap(method, path string, headers map[string]string) bool {
4954
// Get string to sign
5055
var serviceHeaders []string
5156
for k, v := range headers {
@@ -57,7 +62,7 @@ func authenticate(method, path string, headers map[string]string) bool {
5762
sort.Strings(serviceHeaders)
5863
serviceStr := strings.Join(serviceHeaders, "\n")
5964
var signHeaderList []string
60-
for _, key := range []string{"Content-md5", "Content-Type", "Date"} {
65+
for _, key := range []string{"content-md5", "content-type", "date"} {
6166
if val, ok := headers[key]; ok {
6267
signHeaderList = append(signHeaderList, val)
6368
} else {
@@ -119,7 +124,7 @@ func authenticate(method, path string, headers map[string]string) bool {
119124
}
120125

121126
// 对Authorization字段做Base64解码
122-
signatureBase64, ok := headers["Authorization"]
127+
signatureBase64, ok := headers["authorization"]
123128
if !ok {
124129
fmt.Println("Authorization Header not found")
125130
return false
@@ -143,3 +148,24 @@ func authenticate(method, path string, headers map[string]string) bool {
143148

144149
return true
145150
}
151+
152+
func headersToMap(resp *http.Response) map[string]string {
153+
headersMap := make(map[string]string)
154+
for key, values := range resp.Header {
155+
// 连接多个值为一个字符串,使用逗号分隔
156+
// map 键值全小写
157+
lowercaseKey := strings.ToLower(key)
158+
headersMap[lowercaseKey] = strings.Join(values, ",")
159+
}
160+
return headersMap
161+
}
162+
163+
// HTTP header 不区分大小写,将 map 的 keys 转换为全小写
164+
func toLowercaseKeys(input map[string]string) map[string]string {
165+
output := make(map[string]string)
166+
for k, v := range input {
167+
lowercaseKey := strings.ToLower(k)
168+
output[lowercaseKey] = v
169+
}
170+
return output
171+
}

queue_manager.go

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,6 @@ func checkDelaySeconds(seconds int32) (err error) {
4040
return
4141
}
4242

43-
func checkMaxMessageSize(maxSize int32) (err error) {
44-
if maxSize < 1024 || maxSize > 262144 {
45-
err = ERR_MNS_MAX_MESSAGE_SIZE_RANGE_ERROR.New()
46-
return
47-
}
48-
return
49-
}
50-
5143
func checkMessageRetentionPeriod(retentionPeriod int32) (err error) {
5244
if retentionPeriod < 60 || retentionPeriod > 1296000 {
5345
err = ERR_MNS_MSG_RETENTION_PERIOD_RANGE_ERROR.New()
@@ -79,13 +71,10 @@ func NewMNSQueueManager(client MNSClient) AliQueueManager {
7971
}
8072
}
8173

82-
func checkAttributes(delaySeconds int32, maxMessageSize int32, messageRetentionPeriod int32, visibilityTimeout int32, pollingWaitSeconds int32) (err error) {
74+
func checkAttributes(delaySeconds int32, messageRetentionPeriod int32, visibilityTimeout int32, pollingWaitSeconds int32) (err error) {
8375
if err = checkDelaySeconds(delaySeconds); err != nil {
8476
return
8577
}
86-
if err = checkMaxMessageSize(maxMessageSize); err != nil {
87-
return
88-
}
8978
if err = checkMessageRetentionPeriod(messageRetentionPeriod); err != nil {
9079
return
9180
}
@@ -110,7 +99,6 @@ func (p *MNSQueueManager) CreateQueue(queueName string, delaySeconds int32, maxM
11099
}
111100

112101
if err = checkAttributes(delaySeconds,
113-
maxMessageSize,
114102
messageRetentionPeriod,
115103
visibilityTimeout,
116104
pollingWaitSeconds); err != nil {
@@ -144,7 +132,6 @@ func (p *MNSQueueManager) SetQueueAttributes(queueName string, delaySeconds int3
144132
}
145133

146134
if err = checkAttributes(delaySeconds,
147-
maxMessageSize,
148135
messageRetentionPeriod,
149136
visibilityTimeout,
150137
pollingWaitSeconds); err != nil {

topic_manager.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,6 @@ func (p *MNSTopicManager) CreateTopic(topicName string, maxMessageSize int32, lo
5050
return
5151
}
5252

53-
if err = checkMaxMessageSize(maxMessageSize); err != nil {
54-
return
55-
}
56-
5753
message := CreateTopicRequest{
5854
MaxMessageSize: maxMessageSize,
5955
LoggingEnabled: loggingEnabled,
@@ -77,10 +73,6 @@ func (p *MNSTopicManager) SetTopicAttributes(topicName string, maxMessageSize in
7773
return
7874
}
7975

80-
if err = checkMaxMessageSize(maxMessageSize); err != nil {
81-
return
82-
}
83-
8476
message := CreateTopicRequest{
8577
MaxMessageSize: maxMessageSize,
8678
LoggingEnabled: loggingEnabled,

0 commit comments

Comments
 (0)