forked from weikaishio/ali_mns
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtopic_manager.go
177 lines (135 loc) · 4.15 KB
/
topic_manager.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package ali_mns
import (
"fmt"
"net/http"
"strconv"
"strings"
"github.com/gogap/errors"
)
type AliTopicManager interface {
CreateSimpleTopic(topicName string) (err error)
CreateTopic(topicName string, maxMessageSize int32, loggingEnabled bool) (err error)
SetTopicAttributes(topicName string, maxMessageSize int32, loggingEnabled bool) (err error)
GetTopicAttributes(topicName string) (attr TopicAttribute, err error)
DeleteTopic(topicName string) (err error)
ListTopic(nextMarker string, retNumber int32, prefix string) (topics Topics, err error)
ListTopicDetail(nextMarker string, retNumber int32, prefix string) (topicDetails TopicDetails, err error)
}
type MNSTopicManager struct {
cli MNSClient
decoder MNSDecoder
}
func checkTopicName(topicName string) (err error) {
if len(topicName) > 256 {
err = ERR_MNS_TOPIC_NAME_IS_TOO_LONG.New()
return
}
return
}
func NewMNSTopicManager(client MNSClient) AliTopicManager {
return &MNSTopicManager{
cli: client,
decoder: NewAliMNSDecoder(),
}
}
func (p *MNSTopicManager) CreateSimpleTopic(topicName string) (err error) {
return p.CreateTopic(topicName, 65536, false)
}
func (p *MNSTopicManager) CreateTopic(topicName string, maxMessageSize int32, loggingEnabled bool) (err error) {
topicName = strings.TrimSpace(topicName)
if err = checkTopicName(topicName); err != nil {
return
}
if err = checkMaxMessageSize(maxMessageSize); err != nil {
return
}
message := CreateTopicRequest{
MaxMessageSize: maxMessageSize,
LoggingEnabled: loggingEnabled,
}
var code int
code, err = send(p.cli, p.decoder, PUT, nil, &message, "topics/"+topicName, nil)
if code == http.StatusNoContent {
err = ERR_MNS_TOPIC_ALREADY_EXIST_AND_HAVE_SAME_ATTR.New(errors.Params{"name": topicName})
return
}
return
}
func (p *MNSTopicManager) SetTopicAttributes(topicName string, maxMessageSize int32, loggingEnabled bool) (err error) {
topicName = strings.TrimSpace(topicName)
if err = checkTopicName(topicName); err != nil {
return
}
if err = checkMaxMessageSize(maxMessageSize); err != nil {
return
}
message := CreateTopicRequest{
MaxMessageSize: maxMessageSize,
LoggingEnabled: loggingEnabled,
}
_, err = send(p.cli, p.decoder, PUT, nil, &message, fmt.Sprintf("topics/%s?metaoverride=true", topicName), nil)
return
}
func (p *MNSTopicManager) GetTopicAttributes(topicName string) (attr TopicAttribute, err error) {
topicName = strings.TrimSpace(topicName)
if err = checkTopicName(topicName); err != nil {
return
}
_, err = send(p.cli, p.decoder, GET, nil, nil, "topics/"+topicName, &attr)
return
}
func (p *MNSTopicManager) DeleteTopic(topicName string) (err error) {
topicName = strings.TrimSpace(topicName)
if err = checkTopicName(topicName); err != nil {
return
}
_, err = send(p.cli, p.decoder, DELETE, nil, nil, "topics/"+topicName, nil)
return
}
func (p *MNSTopicManager) ListTopic(nextMarker string, retNumber int32, prefix string) (topics Topics, err error) {
header := map[string]string{}
marker := strings.TrimSpace(nextMarker)
if len(marker) > 0 {
if marker != "" {
header["x-mns-marker"] = marker
}
}
if retNumber > 0 {
if retNumber >= 1 && retNumber <= 1000 {
header["x-mns-ret-number"] = strconv.Itoa(int(retNumber))
} else {
err = ERR_MNS_RET_NUMBER_RANGE_ERROR.New()
return
}
}
prefix = strings.TrimSpace(prefix)
if prefix != "" {
header["x-mns-prefix"] = prefix
}
_, err = send(p.cli, p.decoder, GET, header, nil, "topics", &topics)
return
}
func (p *MNSTopicManager) ListTopicDetail(nextMarker string, retNumber int32, prefix string) (topicDetails TopicDetails, err error) {
header := map[string]string{}
marker := strings.TrimSpace(nextMarker)
if len(marker) > 0 {
if marker != "" {
header["x-mns-marker"] = marker
}
}
if retNumber > 0 {
if retNumber >= 1 && retNumber <= 1000 {
header["x-mns-ret-number"] = strconv.Itoa(int(retNumber))
} else {
err = ERR_MNS_RET_NUMBER_RANGE_ERROR.New()
return
}
}
prefix = strings.TrimSpace(prefix)
if prefix != "" {
header["x-mns-prefix"] = prefix
}
header["x-mns-with-meta"] = "true"
_, err = send(p.cli, p.decoder, GET, header, nil, "topics", &topicDetails)
return
}