Skip to content

Commit 7a75e9d

Browse files
committed
Merge branch feature/support_logEnable into release/1.0.8
Title: feat: support configuring the logEnable parameter for queues during creation and attribute setting. #### 需求实现 实现了在 Go SDK 中支持队列(Queue)的 `LoggingEnabled` 参数配置,以满足客户的请求。 #### 代码实现 通过添加 `LoggingEnabled` 字段到相关结构体,并引入选项模式来设置队列属性,从而支持创建和更新队列时配置日志记录功能。 Link: https://code.alibaba-inc.com/messaging/aliyun-mns-go-sdk/codereview/20337410
2 parents 1fd3413 + 9b2963d commit 7a75e9d

File tree

7 files changed

+206
-3
lines changed

7 files changed

+206
-3
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Change log
22

3+
## 1.0.8
4+
- Support configuring the logEnable parameter for queues during creation and attribute setting.
5+
36
## 1.0.7
47
- Add an example of base64 encoding and decoding to queue_example.go.
58
- Add support for dynamic credentials.

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.7/green?label=version)](https://badgen.net/badge/color/1.0.7/green?label=version)
3+
[![Github version](https://badgen.net/badge/color/1.0.8/green?label=version)](https://badgen.net/badge/color/1.0.8/green?label=version)
44

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

README.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.7/green?label=version)](https://badgen.net/badge/color/1.0.7/green?label=version)
3+
[![Github version](https://badgen.net/badge/color/1.0.8/green?label=version)](https://badgen.net/badge/color/1.0.8/green?label=version)
44

55
The Aliyun MNS Go SDK is the official SDK for MNS in the Go programming language
66

example/create_queue_example.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
ali_mns "github.com/aliyun/aliyun-mns-go-sdk"
6+
"time"
7+
)
8+
9+
func main() {
10+
// Replace with your own endpoint.
11+
endpoint := "http://***.mns.cn-hangzhou.aliyuncs.com"
12+
client := ali_mns.NewClient(endpoint)
13+
queueManager := ali_mns.NewMNSQueueManager(client)
14+
queueName := "test-queue"
15+
err := queueManager.CreateQueueWithOptions(queueName,
16+
ali_mns.WithDelaySeconds(5),
17+
ali_mns.WithMaxMessageSize(1024),
18+
ali_mns.WithMessageRetentionPeriod(86400))
19+
time.Sleep(time.Duration(2) * time.Second)
20+
if err != nil && !ali_mns.ERR_MNS_QUEUE_ALREADY_EXIST_AND_HAVE_SAME_ATTR.IsEqual(err) {
21+
fmt.Println("create queue failed: ", err)
22+
return
23+
}
24+
25+
attributes, err := queueManager.GetQueueAttributes(queueName)
26+
if err != nil {
27+
fmt.Println("get queue attributes failed: ", err)
28+
return
29+
} else {
30+
fmt.Println("queue attributes:", attributes)
31+
}
32+
33+
err = queueManager.SetQueueAttributesWithOptions(queueName,
34+
ali_mns.WithLoggingEnabled(true),
35+
ali_mns.WithMessageRetentionPeriod(7200),
36+
ali_mns.WithPollingWaitSeconds(10))
37+
attributes, err = queueManager.GetQueueAttributes(queueName)
38+
if err != nil {
39+
fmt.Println("get queue attributes failed: ", err)
40+
return
41+
} else {
42+
fmt.Println("queue attributes after set:", attributes)
43+
}
44+
}

message.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ type CreateQueueRequest struct {
170170
MessageRetentionPeriod int32 `xml:"MessageRetentionPeriod,omitempty" json:"message_retention_period,omitempty"`
171171
VisibilityTimeout int32 `xml:"VisibilityTimeout,omitempty" json:"visibility_timeout,omitempty"`
172172
PollingWaitSeconds int32 `xml:"PollingWaitSeconds" json:"polling_wait_secods"`
173+
LoggingEnabled bool `xml:"LoggingEnabled" json:"logging_enabled"`
173174
}
174175

175176
type CreateTopicRequest struct {
@@ -217,6 +218,7 @@ type QueueAttribute struct {
217218
DelayMessages int64 `xml:"DelayMessages,omitempty" json:"delay_messages,omitempty"`
218219
CreateTime int64 `xml:"CreateTime,omitempty" json:"create_time,omitempty"`
219220
LastModifyTime int64 `xml:"LastModifyTime,omitempty" json:"last_modify_time,omitempty"`
221+
LoggingEnabled bool `xml:"LoggingEnabled" json:"logging_enabled"`
220222
}
221223

222224
type TopicAttribute struct {

queue_manager.go

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ import (
1212
type AliQueueManager interface {
1313
CreateSimpleQueue(queueName string) (err error)
1414
CreateQueue(queueName string, delaySeconds int32, maxMessageSize int32, messageRetentionPeriod int32, visibilityTimeout int32, pollingWaitSeconds int32, slices int32) (err error)
15+
CreateQueueWithOptions(queueName string, options ...QueueOption) (err error)
1516
SetQueueAttributes(queueName string, delaySeconds int32, maxMessageSize int32, messageRetentionPeriod int32, visibilityTimeout int32, pollingWaitSeconds int32, slices int32) (err error)
17+
SetQueueAttributesWithOptions(queueName string, options ...QueueOption) (err error)
1618
GetQueueAttributes(queueName string) (attr QueueAttribute, err error)
1719
DeleteQueue(queueName string) (err error)
1820
ListQueue(nextMarker string, retNumber int32, prefix string) (queues Queues, err error)
@@ -24,6 +26,59 @@ type MNSQueueManager struct {
2426
decoder MNSDecoder
2527
}
2628

29+
type QueueOptions struct {
30+
delaySeconds int32
31+
maxMessageSize int32
32+
messageRetentionPeriod int32
33+
visibilityTimeout int32
34+
pollingWaitSeconds int32
35+
loggingEnabled bool
36+
}
37+
38+
type QueueOption func(*QueueOptions, map[string]bool)
39+
40+
func WithDelaySeconds(delay int32) QueueOption {
41+
return func(o *QueueOptions, tracker map[string]bool) {
42+
o.delaySeconds = delay
43+
tracker["delaySeconds"] = true
44+
}
45+
}
46+
47+
func WithMaxMessageSize(size int32) QueueOption {
48+
return func(o *QueueOptions, tracker map[string]bool) {
49+
o.maxMessageSize = size
50+
tracker["maxMessageSize"] = true
51+
}
52+
}
53+
54+
func WithMessageRetentionPeriod(period int32) QueueOption {
55+
return func(o *QueueOptions, tracker map[string]bool) {
56+
o.messageRetentionPeriod = period
57+
tracker["messageRetentionPeriod"] = true
58+
}
59+
}
60+
61+
func WithVisibilityTimeout(timeout int32) QueueOption {
62+
return func(o *QueueOptions, tracker map[string]bool) {
63+
o.visibilityTimeout = timeout
64+
tracker["visibilityTimeout"] = true
65+
}
66+
}
67+
68+
func WithPollingWaitSeconds(seconds int32) QueueOption {
69+
return func(o *QueueOptions, tracker map[string]bool) {
70+
o.pollingWaitSeconds = seconds
71+
tracker["pollingWaitSeconds"] = true
72+
}
73+
}
74+
75+
func WithLoggingEnabled(enabled bool) QueueOption {
76+
return func(o *QueueOptions, tracker map[string]bool) {
77+
o.loggingEnabled = enabled
78+
tracker["loggingEnabled"] = true
79+
}
80+
}
81+
2782
func checkQueueName(queueName string) (err error) {
2883
if len(queueName) > 256 {
2984
err = ERR_MNS_QUEUE_NAME_IS_TOO_LONG.New()
@@ -111,6 +166,7 @@ func (p *MNSQueueManager) CreateQueue(queueName string, delaySeconds int32, maxM
111166
MessageRetentionPeriod: messageRetentionPeriod,
112167
VisibilityTimeout: visibilityTimeout,
113168
PollingWaitSeconds: pollingWaitSeconds,
169+
LoggingEnabled: false,
114170
}
115171

116172
var code int
@@ -124,6 +180,41 @@ func (p *MNSQueueManager) CreateQueue(queueName string, delaySeconds int32, maxM
124180
return
125181
}
126182

183+
func (p *MNSQueueManager) CreateQueueWithOptions(queueName string, options ...QueueOption) (err error) {
184+
queueName = strings.TrimSpace(queueName)
185+
if err = checkQueueName(queueName); err != nil {
186+
return
187+
}
188+
opts := defaultQueueOptions()
189+
tracker := make(map[string]bool)
190+
for _, opt := range options {
191+
opt(&opts, tracker)
192+
}
193+
194+
if err = checkAttributes(opts.delaySeconds, opts.messageRetentionPeriod,
195+
opts.visibilityTimeout, opts.pollingWaitSeconds); err != nil {
196+
return
197+
}
198+
199+
message := CreateQueueRequest{
200+
DelaySeconds: opts.delaySeconds,
201+
MaxMessageSize: opts.maxMessageSize,
202+
MessageRetentionPeriod: opts.messageRetentionPeriod,
203+
VisibilityTimeout: opts.visibilityTimeout,
204+
PollingWaitSeconds: opts.pollingWaitSeconds,
205+
LoggingEnabled: opts.loggingEnabled,
206+
}
207+
208+
var code int
209+
code, err = send(p.cli, p.decoder, PUT, nil, &message, "queues/"+queueName, nil)
210+
if code == http.StatusNoContent {
211+
err = ERR_MNS_QUEUE_ALREADY_EXIST_AND_HAVE_SAME_ATTR.New(errors.Params{"name": queueName})
212+
return
213+
}
214+
215+
return
216+
}
217+
127218
func (p *MNSQueueManager) SetQueueAttributes(queueName string, delaySeconds int32, maxMessageSize int32, messageRetentionPeriod int32, visibilityTimeout int32, pollingWaitSeconds int32, slices int32) (err error) {
128219
queueName = strings.TrimSpace(queueName)
129220

@@ -150,6 +241,58 @@ func (p *MNSQueueManager) SetQueueAttributes(queueName string, delaySeconds int3
150241
return
151242
}
152243

244+
func (p *MNSQueueManager) SetQueueAttributesWithOptions(queueName string, options ...QueueOption) (err error) {
245+
queueName = strings.TrimSpace(queueName)
246+
if err = checkQueueName(queueName); err != nil {
247+
return
248+
}
249+
opts := QueueOptions{}
250+
tracker := make(map[string]bool)
251+
for _, opt := range options {
252+
opt(&opts, tracker)
253+
}
254+
255+
message := CreateQueueRequest{}
256+
if tracker["delaySeconds"] {
257+
if err = checkDelaySeconds(opts.delaySeconds); err != nil {
258+
return
259+
}
260+
message.DelaySeconds = opts.delaySeconds
261+
}
262+
263+
if tracker["maxMessageSize"] {
264+
message.MaxMessageSize = opts.maxMessageSize
265+
}
266+
267+
if tracker["messageRetentionPeriod"] {
268+
if err = checkMessageRetentionPeriod(opts.messageRetentionPeriod); err != nil {
269+
return
270+
}
271+
message.MessageRetentionPeriod = opts.messageRetentionPeriod
272+
}
273+
274+
if tracker["visibilityTimeout"] {
275+
if err = checkVisibilityTimeout(opts.visibilityTimeout); err != nil {
276+
return
277+
}
278+
message.VisibilityTimeout = opts.visibilityTimeout
279+
}
280+
281+
if tracker["pollingWaitSeconds"] {
282+
if err = checkPollingWaitSeconds(opts.pollingWaitSeconds); err != nil {
283+
return
284+
}
285+
message.PollingWaitSeconds = opts.pollingWaitSeconds
286+
}
287+
288+
if tracker["loggingEnabled"] {
289+
message.LoggingEnabled = opts.loggingEnabled
290+
}
291+
292+
_, err = send(p.cli, p.decoder, PUT, nil, &message, fmt.Sprintf("queues/%s?metaoverride=true", queueName), nil)
293+
return
294+
}
295+
153296
func (p *MNSQueueManager) GetQueueAttributes(queueName string) (attr QueueAttribute, err error) {
154297
queueName = strings.TrimSpace(queueName)
155298

@@ -235,3 +378,14 @@ func (p *MNSQueueManager) ListQueueDetail(nextMarker string, retNumber int32, pr
235378

236379
return
237380
}
381+
382+
func defaultQueueOptions() QueueOptions {
383+
return QueueOptions{
384+
delaySeconds: 0,
385+
maxMessageSize: 65536,
386+
messageRetentionPeriod: 345600,
387+
visibilityTimeout: 30,
388+
pollingWaitSeconds: 0,
389+
loggingEnabled: false,
390+
}
391+
}

version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ package ali_mns
22

33
const (
44
SdkName = "aliyun-sdk-go"
5-
Version = "1.0.7"
5+
Version = "1.0.8"
66
)

0 commit comments

Comments
 (0)