-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathqueue_attribute.go
130 lines (115 loc) · 3.48 KB
/
queue_attribute.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package alimns
import (
"encoding/xml"
"errors"
)
const (
defaultDelaySeconds = 0
minDelaySeconds = 0
maxDelaySeconds = 604800 // 7天
)
const (
defaultMessageSize = 65536
minMessageSize = 1024
maxMessageSize = 65536
)
const (
defaultRetentionPeriod = 604800 // 7天
minRetentionPeriod = 60 // 60秒
maxRetentionPeriod = 604800 // 7天
)
const (
defaultVisibilityTimeout = 30
minVisibilityTimeout = 1
maxVisibilityTimeout = 43200
)
const (
defaultPollingWaitSeconds = 30
minPollingWaitSeconds = 0
maxPollingWaitSeconds = 30
)
// ModifiedAttribute 修改消息属性
type ModifiedAttribute struct {
XMLName xml.Name `xml:"Queue"`
DelaySeconds *int `xml:"DelaySeconds,omitempty"`
MaximumMessageSize int `xml:"MaximumMessageSize,omitempty"`
MessageRetentionPeriod int `xml:"MessageRetentionPeriod,omitempty"`
VisibilityTimeout int `xml:"VisibilityTimeout,omitempty"`
PollingWaitSeconds *int `xml:"PollingWaitSeconds,omitempty"`
LoggingEnabled *bool `xml:"LoggingEnabled,omitempty"`
}
// QueueAttributeSetter 消息属性设置函数模板
type QueueAttributeSetter func(attri *ModifiedAttribute) error
// DefaultQueueAttri 返回默认的修改消息隊列的參數
func DefaultQueueAttri() ModifiedAttribute {
var (
delaySeconds = defaultDelaySeconds
pollingWaitSeconds = defaultPollingWaitSeconds
loggingEnabled = true
)
return ModifiedAttribute{
DelaySeconds: &delaySeconds,
MaximumMessageSize: defaultMessageSize,
MessageRetentionPeriod: defaultRetentionPeriod,
VisibilityTimeout: defaultVisibilityTimeout,
PollingWaitSeconds: &pollingWaitSeconds,
LoggingEnabled: &loggingEnabled,
}
}
// WithDelaySeconds 设置延时时间
func WithDelaySeconds(s int) QueueAttributeSetter {
return func(attri *ModifiedAttribute) error {
if s < minDelaySeconds || s > maxDelaySeconds {
return errors.New("delay seconds out of range")
}
attri.DelaySeconds = &s
return nil
}
}
// WithMaximumMessageSize 设置消息体长度
func WithMaximumMessageSize(size int) QueueAttributeSetter {
return func(attri *ModifiedAttribute) error {
if size < minMessageSize || size > maxMessageSize {
return errors.New("maximum message size out of range")
}
attri.MaximumMessageSize = size
return nil
}
}
// WithMessageRetentionPeriod 设置最长存活时间
func WithMessageRetentionPeriod(s int) QueueAttributeSetter {
return func(attri *ModifiedAttribute) error {
if s < minRetentionPeriod || s > maxRetentionPeriod {
return errors.New("retention period out of range")
}
attri.MessageRetentionPeriod = s
return nil
}
}
// WithVisibilityTimeout 设置可见时间
func WithVisibilityTimeout(s int) QueueAttributeSetter {
return func(attri *ModifiedAttribute) error {
if s < minVisibilityTimeout || s > maxVisibilityTimeout {
return visibilityTimeoutError
}
attri.VisibilityTimeout = s
return nil
}
}
// WithPollingWaitSeconds 设置长轮询时间
func WithPollingWaitSeconds(s int) QueueAttributeSetter {
return func(attri *ModifiedAttribute) error {
if s < minPollingWaitSeconds || s > maxPollingWaitSeconds {
return errors.New("polling wait seconds out of range")
}
attri.PollingWaitSeconds = &s
return nil
}
}
// WithLoggingEnabled 设置日志开启
func WithLoggingEnabled(flag bool) QueueAttributeSetter {
return func(attri *ModifiedAttribute) error {
attri.LoggingEnabled = &flag
return nil
}
}