Skip to content

Commit 57c925d

Browse files
Kayne666Houlong66suhaoyuanzhi
authored
Release/2.0.0 (#31)
* refactor(message): 标记 QueueAttribute 中的消息统计字段为废弃 - 在 QueueAttribute 结构体中,将 ActiveMessages、InactiveMessages 和 DelayMessages 字段标记为废弃 - 添加废弃字段的注释,提示用户这些字段即将在后续版本中移除 * fix(message): 修正 QueueAttribute 结构体中的 PollingWaitSeconds 字段名称 将 json 标签中的 "polling_wait_secods" 修改为 "polling_wait_seconds",以纠正拼写错误。 * refactor(client): 将错误处理从 panic 改为返回 error- 修改了多个函数的返回类型,增加了 error 返回值 -移除了 panic 调用,改为返回具体的 error - 更新了相关的示例代码,增加了错误处理逻辑 - 统一了错误日志的输出格式 * fix return err * build: update go module dependencies for aliyun-mns-go-sdk * add unittest * fix(test): access key id is empty * feature(client): support setRegion * fix(client): Compatibility repair Fix compatibility issues caused by deleting_panic&supporting_region * feature(test): config * optimize * fix(client): Modify function name * refactor(client): 去除Deprecated内容 * fix(test): add env * fix(test): delete UID * refactor(example): 删除NewClient Demo * feature(conf): 添加 changelog * doc(client): require explicit region configuration in AliMNSClientConfig Change-Id: Icaeccf2bc8559a08ecfc25628d414adf27504da0 * fix: correct spelling errors in subscription-related types - Fixed 'Subsribe' to 'Subscribe' in MessageSubsribeRequest type - Corrected 'SUBSRIPTION' to 'SUBSCRIPTION' in error constant ERR_MNS_SUBSRIPTION_NAME_LENGTH_ERROR - Fixed 'SubsriptionNameInvalid' to 'SubscriptionNameInvalid' in error mapping Change-Id: I698f62758c1b8e83872ab53452075d65f70e339d * fix(example/queue_example.go): 添加错误处理 * fix(client): 修正错误消息中的拼写错误 --------- Co-authored-by: 浩龙 <[email protected]> Co-authored-by: suhao <[email protected]> Co-authored-by: yuanzhi <[email protected]>
1 parent 68f2cac commit 57c925d

17 files changed

+422
-112
lines changed

CHANGELOG.md

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
# Change log
22

3+
## 2.0.0
4+
This is a major version upgrade that introduces breaking changes. Please read carefully before upgrading.
5+
6+
### ⚠️ Breaking Changes
7+
- **Region Configuration is Now Required**: The `region` field in `AliMNSClientConfig` is now mandatory. Client initialization will fail with error "ali-mns: region is empty" if not provided.
8+
- **Region-Dependent Subscription Endpoints**: Subscription endpoints now use the explicitly configured region instead of attempting to parse from endpoint URL.
9+
10+
### Other Changes
11+
- Fixed [issue#28](https://github.com/aliyun/aliyun-mns-go-sdk/issues/28): Remove panic from client initialization, return errors instead.
12+
- Remove deprecated client creation methods.
13+
- Simplify client configuration with `AliMNSClientConfig`.
14+
- Update examples to use unified `AliMNSClientConfig`.
15+
- Fixed some spelling errors.
16+
317
## 1.0.11
418
- Updated version number to 1.0.11 with no other changes.
519

@@ -30,14 +44,11 @@
3044
- support custom maxConnsPerHost value for the client.
3145

3246
## 1.0.3
33-
3447
- support custom transport configuration
3548

3649
## 1.0.2
37-
3850
- support OpenService API
3951

4052
## 1.0.1
41-
4253
- support setting timeout
43-
- add request id to response
54+
- add request id to response

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

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

client.go

Lines changed: 27 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"net/http"
1010
neturl "net/url"
1111
"os"
12-
"regexp"
1312
"runtime"
1413
"strings"
1514
"sync"
@@ -65,8 +64,8 @@ type MNSClient interface {
6564
Send(method Method, headers map[string]string, message interface{}, resource string) (*fasthttp.Response, error)
6665
SetProxy(url string)
6766
SetTransport(transport fasthttp.RoundTripper)
68-
getAccountId() (accountId string)
69-
getRegion() (region string)
67+
GetAccountId() (accountId string)
68+
GetRegion() (region string)
7069
}
7170

7271
type aliMNSClient struct {
@@ -87,57 +86,38 @@ type AliMNSClientConfig struct {
8786
AccessKeyId string
8887
AccessKeySecret string
8988
Token string
89+
Region string
9090
Credential credentials.Credential
9191
TimeoutSecond int64
9292
MaxConnsPerHost int
9393
}
9494

9595
// NewClient Follow the Alibaba Cloud standards and set the AK (Access Key) and SK (Secret Key) in the environment variables.
9696
// For more details, see: https://help.aliyun.com/zh/sdk/developer-reference/configure-the-alibaba-cloud-accesskey-environment-variable-on-linux-macos-and-windows-systems
97-
func NewClient(endpoint string) MNSClient {
98-
return NewClientWithToken(endpoint, "")
97+
func NewClient(endpoint string, region string) (MNSClient, error) {
98+
return NewClientWithToken(endpoint, "", region)
9999
}
100100

101101
// NewClientWithToken Follow the Alibaba Cloud standards and set the AK (Access Key) and SK (Secret Key) in the environment variables.
102102
// For more details, see: https://help.aliyun.com/zh/sdk/developer-reference/configure-the-alibaba-cloud-accesskey-environment-variable-on-linux-macos-and-windows-systems
103-
func NewClientWithToken(endpoint, token string) MNSClient {
103+
func NewClientWithToken(endpoint string, token string, region string) (MNSClient, error) {
104104
return NewAliMNSClientWithConfig(AliMNSClientConfig{
105105
EndPoint: endpoint,
106106
AccessKeyId: os.Getenv(AliyunAkEnvKey),
107107
AccessKeySecret: os.Getenv(AliyunSkEnvKey),
108108
Token: token,
109109
TimeoutSecond: DefaultTimeout,
110110
MaxConnsPerHost: DefaultMaxConnsPerHost,
111+
Region: region,
111112
})
112113
}
113114

114-
// Deprecated: Use NewClient instead.
115-
func NewAliMNSClient(inputUrl, accessKeyId, accessKeySecret string) MNSClient {
116-
return NewAliMNSClientWithConfig(AliMNSClientConfig{
117-
EndPoint: inputUrl,
118-
AccessKeyId: accessKeyId,
119-
AccessKeySecret: accessKeySecret,
120-
Token: "",
121-
TimeoutSecond: DefaultTimeout,
122-
MaxConnsPerHost: DefaultMaxConnsPerHost,
123-
})
124-
}
125-
126-
// Deprecated: Use NewClientWithToken instead.
127-
func NewAliMNSClientWithToken(inputUrl, accessKeyId, accessKeySecret, token string) MNSClient {
128-
return NewAliMNSClientWithConfig(AliMNSClientConfig{
129-
EndPoint: inputUrl,
130-
AccessKeyId: accessKeyId,
131-
AccessKeySecret: accessKeySecret,
132-
Token: token,
133-
TimeoutSecond: DefaultTimeout,
134-
MaxConnsPerHost: DefaultMaxConnsPerHost,
135-
})
136-
}
137-
138-
func NewAliMNSClientWithConfig(clientConfig AliMNSClientConfig) MNSClient {
115+
func NewAliMNSClientWithConfig(clientConfig AliMNSClientConfig) (MNSClient, error) {
139116
if clientConfig.EndPoint == "" {
140-
panic("ali-mns: message queue url is empty")
117+
return nil, fmt.Errorf("ali-mns: message queue url is empty")
118+
}
119+
if clientConfig.Region == "" {
120+
return nil, fmt.Errorf("ali-mns: region is empty")
141121
}
142122

143123
cli := new(aliMNSClient)
@@ -153,7 +133,7 @@ func NewAliMNSClientWithConfig(clientConfig AliMNSClientConfig) MNSClient {
153133
var err error
154134
cli.credential, err = credentials.NewCredential(config)
155135
if err != nil {
156-
panic(err)
136+
return nil, fmt.Errorf("failed to create sts credential: %w", err)
157137
}
158138
} else {
159139
config := new(credentials.Config).
@@ -163,7 +143,7 @@ func NewAliMNSClientWithConfig(clientConfig AliMNSClientConfig) MNSClient {
163143
var err error
164144
cli.credential, err = credentials.NewCredential(config)
165145
if err != nil {
166-
panic(err)
146+
return nil, fmt.Errorf("failed to create access key credential: %w", err)
167147
}
168148
}
169149

@@ -175,20 +155,20 @@ func NewAliMNSClientWithConfig(clientConfig AliMNSClientConfig) MNSClient {
175155

176156
var err error
177157
if cli.url, err = neturl.Parse(clientConfig.EndPoint); err != nil {
178-
panic("err parse url")
158+
return nil, fmt.Errorf("failed to parse url: %w", err)
179159
}
180160

181-
// 1. parse region and accountId
161+
// 1. parse accountId
182162
pieces := strings.Split(clientConfig.EndPoint, ".")
183163
if len(pieces) != 5 {
184-
panic("ali-mns: message queue url is invalid")
164+
return nil, fmt.Errorf("ali-mns: message queue url is invalid")
185165
}
186166

187167
accountIdSlice := strings.Split(pieces[0], "/")
188168
cli.accountId = accountIdSlice[len(accountIdSlice)-1]
189-
re := regexp.MustCompile("-(internal|control)")
190-
regionSlice := re.Split(pieces[2], -1)
191-
cli.region = regionSlice[0]
169+
170+
cli.region = clientConfig.Region
171+
192172
if globalUrl := os.Getenv(GlobalProxy); globalUrl != "" {
193173
cli.proxyURL = globalUrl
194174
}
@@ -197,14 +177,15 @@ func NewAliMNSClientWithConfig(clientConfig AliMNSClientConfig) MNSClient {
197177
cli.initFastHttpClient()
198178
//change to dial dual stack to support both ipv4 and ipv6
199179
cli.client.DialDualStack = true
200-
return cli
180+
181+
return cli, nil
201182
}
202183

203-
func (p aliMNSClient) getAccountId() (accountId string) {
184+
func (p aliMNSClient) GetAccountId() (accountId string) {
204185
return p.accountId
205186
}
206187

207-
func (p aliMNSClient) getRegion() (region string) {
188+
func (p aliMNSClient) GetRegion() (region string) {
208189
return p.region
209190
}
210191

@@ -325,7 +306,7 @@ func initMNSErrors() {
325306
"InvalidAuthorizationHeader": ERR_MNS_INVALID_AUTHORIZATION_HEADER,
326307
"InvalidDateHeader": ERR_MNS_INVALID_DATE_HEADER,
327308
"InvalidArgument": ERR_MNS_INVALID_ARGUMENT,
328-
"InvalidDegist": ERR_MNS_INVALID_DEGIST,
309+
"InvalidDigest": ERR_MNS_INVALID_DIGEST,
329310
"InvalidRequestURL": ERR_MNS_INVALID_REQUEST_URL,
330311
"InvalidQueryString": ERR_MNS_INVALID_QUERY_STRING,
331312
"MalformedXML": ERR_MNS_MALFORMED_XML,
@@ -347,9 +328,9 @@ func initMNSErrors() {
347328
"TopicAlreadyExist": ERR_MNS_TOPIC_ALREADY_EXIST,
348329
"TopicNameLengthError": ERR_MNS_TOPIC_NAME_LENGTH_ERROR,
349330
"TopicNotExist": ERR_MNS_TOPIC_NOT_EXIST,
350-
"SubscriptionNameLengthError": ERR_MNS_SUBSRIPTION_NAME_LENGTH_ERROR,
331+
"SubscriptionNameLengthError": ERR_MNS_SUBSCRIPTION_NAME_LENGTH_ERROR,
351332
"TopicNameInvalid": ERR_MNS_INVALID_TOPIC_NAME,
352-
"SubsriptionNameInvalid": ERR_MNS_INVALID_SUBSCRIPTION_NAME,
333+
"SubscriptionNameInvalid": ERR_MNS_INVALID_SUBSCRIPTION_NAME,
353334
"SubscriptionAlreadyExist": ERR_MNS_SUBSCRIPTION_ALREADY_EXIST,
354335
"EndpointInvalid": ERR_MNS_INVALID_ENDPOINT,
355336
"SubscriberNotExist": ERR_MNS_SUBSCRIBER_NOT_EXIST,

errors.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ var (
3030
ERR_MNS_INVALID_AUTHORIZATION_HEADER = errors.TN(ALI_MNS_ERR_NS, 103, ali_MNS_ERR_TEMPSTR)
3131
ERR_MNS_INVALID_DATE_HEADER = errors.TN(ALI_MNS_ERR_NS, 104, ali_MNS_ERR_TEMPSTR)
3232
ERR_MNS_INVALID_ARGUMENT = errors.TN(ALI_MNS_ERR_NS, 105, ali_MNS_ERR_TEMPSTR)
33-
ERR_MNS_INVALID_DEGIST = errors.TN(ALI_MNS_ERR_NS, 106, ali_MNS_ERR_TEMPSTR)
33+
ERR_MNS_INVALID_DIGEST = errors.TN(ALI_MNS_ERR_NS, 106, ali_MNS_ERR_TEMPSTR)
3434
ERR_MNS_INVALID_REQUEST_URL = errors.TN(ALI_MNS_ERR_NS, 107, ali_MNS_ERR_TEMPSTR)
3535
ERR_MNS_INVALID_QUERY_STRING = errors.TN(ALI_MNS_ERR_NS, 108, ali_MNS_ERR_TEMPSTR)
3636
ERR_MNS_MALFORMED_XML = errors.TN(ALI_MNS_ERR_NS, 109, ali_MNS_ERR_TEMPSTR)
@@ -53,15 +53,15 @@ var (
5353
ERR_MNS_QPS_LIMIT_EXCEEDED = errors.TN(ALI_MNS_ERR_NS, 134, ali_MNS_ERR_TEMPSTR)
5454
ERR_MNS_UNKNOWN_CODE = errors.TN(ALI_MNS_ERR_NS, 135, ali_MNS_ERR_TEMPSTR)
5555

56-
ERR_MNS_TOPIC_NAME_LENGTH_ERROR = errors.TN(ALI_MNS_ERR_NS, 200, ali_MNS_ERR_TEMPSTR)
57-
ERR_MNS_SUBSRIPTION_NAME_LENGTH_ERROR = errors.TN(ALI_MNS_ERR_NS, 201, ali_MNS_ERR_TEMPSTR)
58-
ERR_MNS_TOPIC_NOT_EXIST = errors.TN(ALI_MNS_ERR_NS, 202, ali_MNS_ERR_TEMPSTR)
59-
ERR_MNS_TOPIC_ALREADY_EXIST = errors.TN(ALI_MNS_ERR_NS, 203, ali_MNS_ERR_TEMPSTR)
60-
ERR_MNS_INVALID_TOPIC_NAME = errors.TN(ALI_MNS_ERR_NS, 204, ali_MNS_ERR_TEMPSTR)
61-
ERR_MNS_INVALID_SUBSCRIPTION_NAME = errors.TN(ALI_MNS_ERR_NS, 205, ali_MNS_ERR_TEMPSTR)
62-
ERR_MNS_SUBSCRIPTION_ALREADY_EXIST = errors.TN(ALI_MNS_ERR_NS, 206, ali_MNS_ERR_TEMPSTR)
63-
ERR_MNS_INVALID_ENDPOINT = errors.TN(ALI_MNS_ERR_NS, 207, ali_MNS_ERR_TEMPSTR)
64-
ERR_MNS_SUBSCRIBER_NOT_EXIST = errors.TN(ALI_MNS_ERR_NS, 211, ali_MNS_ERR_TEMPSTR)
56+
ERR_MNS_TOPIC_NAME_LENGTH_ERROR = errors.TN(ALI_MNS_ERR_NS, 200, ali_MNS_ERR_TEMPSTR)
57+
ERR_MNS_SUBSCRIPTION_NAME_LENGTH_ERROR = errors.TN(ALI_MNS_ERR_NS, 201, ali_MNS_ERR_TEMPSTR)
58+
ERR_MNS_TOPIC_NOT_EXIST = errors.TN(ALI_MNS_ERR_NS, 202, ali_MNS_ERR_TEMPSTR)
59+
ERR_MNS_TOPIC_ALREADY_EXIST = errors.TN(ALI_MNS_ERR_NS, 203, ali_MNS_ERR_TEMPSTR)
60+
ERR_MNS_INVALID_TOPIC_NAME = errors.TN(ALI_MNS_ERR_NS, 204, ali_MNS_ERR_TEMPSTR)
61+
ERR_MNS_INVALID_SUBSCRIPTION_NAME = errors.TN(ALI_MNS_ERR_NS, 205, ali_MNS_ERR_TEMPSTR)
62+
ERR_MNS_SUBSCRIPTION_ALREADY_EXIST = errors.TN(ALI_MNS_ERR_NS, 206, ali_MNS_ERR_TEMPSTR)
63+
ERR_MNS_INVALID_ENDPOINT = errors.TN(ALI_MNS_ERR_NS, 207, ali_MNS_ERR_TEMPSTR)
64+
ERR_MNS_SUBSCRIBER_NOT_EXIST = errors.TN(ALI_MNS_ERR_NS, 211, ali_MNS_ERR_TEMPSTR)
6565

6666
ERR_MNS_TOPIC_NAME_IS_TOO_LONG = errors.TN(ALI_MNS_ERR_NS, 208, "topic name is too long, the max length is 256")
6767
ERR_MNS_TOPIC_ALREADY_EXIST_AND_HAVE_SAME_ATTR = errors.TN(ALI_MNS_ERR_NS, 209, "mns topic already exist, and the attribute is the same, topic name: {{.name}}")

example/create_queue_example.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,25 @@ package main
33
import (
44
"fmt"
55
ali_mns "github.com/aliyun/aliyun-mns-go-sdk"
6+
"os"
67
"time"
78
)
89

910
func main() {
1011
// Replace with your own endpoint.
1112
endpoint := "http://***.mns.cn-hangzhou.aliyuncs.com"
12-
client := ali_mns.NewClient(endpoint)
13+
client, error := ali_mns.NewAliMNSClientWithConfig(ali_mns.AliMNSClientConfig{
14+
EndPoint: endpoint,
15+
AccessKeyId: os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"),
16+
AccessKeySecret: os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"),
17+
Region: "cn-hangzhou",
18+
})
19+
if error != nil {
20+
fmt.Println(error)
21+
return
22+
}
1323
queueManager := ali_mns.NewMNSQueueManager(client)
14-
queueName := "test-queue"
24+
queueName := "go-test-queue"
1525
err := queueManager.CreateQueueWithOptions(queueName,
1626
ali_mns.WithDelaySeconds(5),
1727
ali_mns.WithMaxMessageSize(1024),
@@ -41,4 +51,4 @@ func main() {
4151
} else {
4252
fmt.Println("queue attributes after set:", attributes)
4353
}
44-
}
54+
}

example/open_service_example.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,25 @@ package main
33
import (
44
"fmt"
55
"github.com/aliyun/aliyun-mns-go-sdk"
6+
"os"
67
)
78

89
func main() {
910
// Replace with your own endpoint.
1011
endpoint := "http://xxx.mns.cn-hangzhou.aliyuncs.com"
11-
client := ali_mns.NewClient(endpoint)
12+
client, err := ali_mns.NewAliMNSClientWithConfig(ali_mns.AliMNSClientConfig{
13+
EndPoint: endpoint,
14+
AccessKeyId: os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"),
15+
AccessKeySecret: os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"),
16+
Region: "cn-hangzhou",
17+
})
18+
if err != nil {
19+
fmt.Println(err)
20+
return
21+
}
1222
if resp, err := ali_mns.NewAccountManager(client).OpenService(); err != nil {
1323
fmt.Println(err)
1424
} else {
1525
fmt.Printf("reqId: %s, orderId: %s", resp.RequestId, resp.OrderId)
1626
}
17-
}
27+
}

example/queue_example.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,16 @@ func main() {
2121
// Replace with your own endpoint.
2222
endpoint := "http://***.mns.cn-hangzhou.aliyuncs.com"
2323
isBase64 := os.Getenv("IS_BASE64") == "true"
24-
client := ali_mns.NewClient(endpoint)
24+
client, e := ali_mns.NewAliMNSClientWithConfig(ali_mns.AliMNSClientConfig{
25+
EndPoint: endpoint,
26+
AccessKeyId: os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"),
27+
AccessKeySecret: os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"),
28+
Region: "cn-hangzhou",
29+
})
30+
if e != nil {
31+
fmt.Println(e)
32+
return
33+
}
2534
messageBody := "hello <\"aliyun-mns-go-sdk\">"
2635
if isBase64 {
2736
messageBody = base64.StdEncoding.EncodeToString([]byte(messageBody))
@@ -41,7 +50,12 @@ func main() {
4150
return
4251
}
4352

44-
queue := ali_mns.NewMNSQueue(queueName, client)
53+
queue, e := ali_mns.NewMNSQueue(queueName, client)
54+
if e != nil {
55+
fmt.Println(e)
56+
return
57+
}
58+
4559
for i := 1; i < 10; i++ {
4660
ret, err := queue.SendMessage(msg)
4761
go func() {

0 commit comments

Comments
 (0)