Skip to content

Commit 110e633

Browse files
Kayne666zhongxig
andauthored
v1.0.4 (#20)
* perf(version): 修改 useragent 信息,并增加新版本的 readme 说明 * perf(sample): 遵循阿里云规范,通过环境变量设置 ak、sk * revert(dependency): go.mod 版本回退 * ci(gitignore): add pkg ignore 1. add pkg to .gitignore * doc(readme): 修改 README.md * doc(changelog): 修改 CHANGELOG.md,增加 1.0.3 版本更新的说明 * doc(format): md 文件不用引用格式 * feat(client): MNSClient 支持传入 fasthttp 的 maxConnsPerHost * docs(changelog): 补充上次改动的 changelog * docs(changelog): 更新 1.0.4 版本的 changelog --------- Co-authored-by: Asassi <[email protected]>
1 parent c58b551 commit 110e633

File tree

13 files changed

+220
-144
lines changed

13 files changed

+220
-144
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,5 @@ _testmain.go
2323
*.exe
2424
*.test
2525
*.prof
26+
27+
pkg/

CHANGELOG.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Change log
2+
3+
## 1.0.4
4+
- add version and platform information to the user agent
5+
- following Alibaba standards, provide new recommended methods for creating MNS client, and update the example code
6+
- support custom maxConnsPerHost value for the client.
7+
8+
## 1.0.3
9+
10+
- support custom transport configuration
11+
12+
## 1.0.2
13+
14+
- support OpenService API
15+
16+
## 1.0.1
17+
18+
- support setting timeout
19+
- add request id to response

README-CN.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Aliyun MNS Go SDK
2+
3+
[![Github version](https://badgen.net/badge/color/1.0.3/green?label=version)](https://badgen.net/badge/color/1.0.3/green?label=version)
4+
5+
Aliyun MNS Go SDK 是 MNS 在 Go 编译语言的官方 SDK
6+
7+
## [Readme in English](README.md)
8+
9+
## [Change Log](CHANGELOG.md)
10+
11+
## 关于
12+
13+
- 此 Go SDK 基于[阿里云消息服务 MNS](https://www.aliyun.com/product/mns/) 官方 API 构建
14+
- 阿里云消息服务(Message Service,简称 MNS)是一种高效、可靠、安全、便捷、可弹性扩展的分布式消息服务
15+
- MNS 能够帮助应用开发者在他们应用的分布式组件上自由的传递数据、通知消息,构建松耦合系统
16+
- 使用此 SDK,用户可以快速构建高可靠、高并发的一对一消费模型和一对多的发布订阅模型
17+
18+
## 运行环境
19+
20+
- Go 1.18 及以上
21+
22+
## 安装方法
23+
24+
- 执行命令 `go get github.com/aliyun/aliyun-mns-go-sdk` 获取远程代码包
25+
- 在您的代码中通过 `import "github.com/aliyun/aliyun-mns-go-sdk"` 引入 MNS Go SDK
26+
27+
## 快速使用
28+
29+
- 下载最新版 Go SDK,进入 example 目录
30+
- 修改 endpoint 为您自己的接入点,可登录 [MNS 控制台](https://mns.console.aliyun.com/)
31+
查看,具体操作,请参考[获取接入点](https://help.aliyun.com/zh/mns/user-guide/manage-queues-in-the-console?spm=a2c4g.11186623.0.i25#section-yhc-ix5-300)
32+
- 在环境变量中设置您的 `ALIBABA_CLOUD_ACCESS_KEY_ID`
33+
`ALIBABA_CLOUD_ACCESS_KEY_SECRET`,阿里云身份验证信息在 [RAM 控制台](https://ram.console.aliyun.com/)
34+
创建。获取方式请参考[获取 AccessKey](https://help.aliyun.com/document_detail/53045.html?spm=a2c4g.11186623.0.i29#task-354412)
35+
- 根据阿里云规范,您应该将 AK SK
36+
信息设置为环境变量使用,请参考[设置环境变量](https://help.aliyun.com/zh/sdk/developer-reference/configure-the-alibaba-cloud-accesskey-environment-variable-on-linux-macos-and-windows-systems)
37+
38+
## 许可协议
39+
40+
- Apache-2.0 请参阅[许可文件](LICENSE)

README.md

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,50 @@
1-
# MNS GO SDK
2-
1.0.2
3-
- support OpenService API
1+
# Aliyun MNS Go SDK
2+
3+
[![Github version](https://badgen.net/badge/color/1.0.3/green?label=version)](https://badgen.net/badge/color/1.0.3/green?label=version)
4+
5+
The Aliyun MNS Go SDK is the official SDK for MNS in the Go programming language
6+
7+
## [Readme in Simplified Chinese](README-CN.md)
8+
9+
## [Change Log](CHANGELOG.md)
10+
11+
## About
12+
13+
- This Go SDK is built on the official API
14+
of [Alibaba Cloud Message Service (MNS)](https://www.aliyun.com/product/mns/)
15+
- Alibaba Cloud Message Service (MNS) is an efficient, reliable, secure, convenient, and elastically scalable
16+
distributed messaging service
17+
- MNS enables application developers to freely transmit data and notification messages across the distributed
18+
components of their applications, building loosely coupled systems
19+
- Using this SDK, users can quickly build highly reliable and concurrent one-to-one consumption models as well as
20+
one-to-many publish-subscribe models
21+
22+
## Running Environment
23+
24+
- Go 1.18 or above
25+
26+
## Installing
27+
28+
- Run the`go get github.com/aliyun/aliyun-mns-go-sdk` command to get the remote code package.
29+
- Use `import "github.com/aliyun/aliyun-mns-go-sdk"` in your code to introduce MNS Go SDK package
30+
31+
## Getting Start
32+
33+
- Download the latest version of the Go SDK and enter the example directory
34+
- Modify 'endpoint' to your own access point, which can be viewed by logging into
35+
the [MNS console](https://mns.console.aliyun.com/). For more detail,
36+
see [How to get endpoint](https://help.aliyun.com/zh/mns/user-guide/manage-queues-in-the-console?spm=a2c4g.11186623.0.i25#section-yhc-ix5-300)
37+
- Set your `ALIBABA_CLOUD_ACCESS_KEY_ID` and
38+
`ALIBABA_CLOUD_ACCESS_KEY_SECRET` in the environment variables, Alibaba Cloud authentication information can be
39+
created in the [RAM console](https://ram.console.aliyun.com/).
40+
For more detail,
41+
see [How to get AccessKey](https://help.aliyun.com/document_detail/53045.html?spm=a2c4g.11186623.0.i29#task-354412)
42+
- Follow the Alibaba Cloud standards and set the AK (Access Key) and SK (Secret Key) in the environment variables. For
43+
more details,
44+
see [How to set environment variables](https://help.aliyun.com/zh/sdk/developer-reference/configure-the-alibaba-cloud-accesskey-environment-variable-on-linux-macos-and-windows-systems)
45+
46+
## License
47+
48+
- Apache-2.0 see [license file](LICENSE)
49+
450

5-
1.0.1
6-
- support setting timeout
7-
- add request id to response

client.go

Lines changed: 60 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"net/http"
1010
neturl "net/url"
1111
"os"
12+
"runtime"
1213
"strings"
1314
"sync"
1415
"time"
@@ -24,16 +25,21 @@ const (
2425
)
2526

2627
const (
27-
GLOBAL_PROXY = "MNS_GLOBAL_PROXY"
28+
GlobalProxy = "MNS_GLOBAL_PROXY"
2829
)
2930

3031
const (
3132
version = "2015-06-06"
3233
)
3334

3435
const (
35-
ClientName = "mns-go-sdk/1.0.2(fasthttp)"
36-
DefaultTimeout int64 = 35
36+
DefaultTimeout int64 = 35
37+
DefaultMaxConnsPerHost int = 512
38+
)
39+
40+
const (
41+
AliyunAkEnvKey = "ALIBABA_CLOUD_ACCESS_KEY_ID"
42+
AliyunSkEnvKey = "ALIBABA_CLOUD_ACCESS_KEY_SECRET"
3743
)
3844

3945
type Method string
@@ -57,22 +63,21 @@ type MNSClient interface {
5763
Send(method Method, headers map[string]string, message interface{}, resource string) (*fasthttp.Response, error)
5864
SetProxy(url string)
5965
SetTransport(transport fasthttp.RoundTripper)
60-
getAccountID() (accountId string)
66+
getAccountId() (accountId string)
6167
getRegion() (region string)
6268
}
6369

6470
type aliMNSClient struct {
65-
Timeout int64
66-
url *neturl.URL
67-
credential Credential
68-
accessKeyId string
69-
client *fasthttp.Client
70-
proxyURL string
71-
72-
accountId string
73-
region string
74-
75-
clientLocker sync.Mutex
71+
Timeout int64
72+
MaxConnsPerHost int
73+
url *neturl.URL
74+
credential Credential
75+
accessKeyId string
76+
client *fasthttp.Client
77+
proxyURL string
78+
accountId string
79+
region string
80+
clientLocker sync.Mutex
7681
}
7782

7883
type AliMNSClientConfig struct {
@@ -81,25 +86,49 @@ type AliMNSClientConfig struct {
8186
AccessKeySecret string
8287
Token string
8388
TimeoutSecond int64
89+
MaxConnsPerHost int
90+
}
91+
92+
// NewClient Follow the Alibaba Cloud standards and set the AK (Access Key) and SK (Secret Key) in the environment variables.
93+
// 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
94+
func NewClient(endpoint string) MNSClient {
95+
return NewClientWithToken(endpoint, "")
96+
}
97+
98+
// NewClientWithToken Follow the Alibaba Cloud standards and set the AK (Access Key) and SK (Secret Key) in the environment variables.
99+
// 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
100+
func NewClientWithToken(endpoint, token string) MNSClient {
101+
return NewAliMNSClientWithConfig(AliMNSClientConfig{
102+
EndPoint: endpoint,
103+
AccessKeyId: os.Getenv(AliyunAkEnvKey),
104+
AccessKeySecret: os.Getenv(AliyunSkEnvKey),
105+
Token: token,
106+
TimeoutSecond: DefaultTimeout,
107+
MaxConnsPerHost: DefaultMaxConnsPerHost,
108+
})
84109
}
85110

111+
// Deprecated: Use NewClient instead.
86112
func NewAliMNSClient(inputUrl, accessKeyId, accessKeySecret string) MNSClient {
87113
return NewAliMNSClientWithConfig(AliMNSClientConfig{
88114
EndPoint: inputUrl,
89115
AccessKeyId: accessKeyId,
90116
AccessKeySecret: accessKeySecret,
91117
Token: "",
92118
TimeoutSecond: DefaultTimeout,
119+
MaxConnsPerHost: DefaultMaxConnsPerHost,
93120
})
94121
}
95122

123+
// Deprecated: Use NewClientWithToken instead.
96124
func NewAliMNSClientWithToken(inputUrl, accessKeyId, accessKeySecret, token string) MNSClient {
97125
return NewAliMNSClientWithConfig(AliMNSClientConfig{
98126
EndPoint: inputUrl,
99127
AccessKeyId: accessKeyId,
100128
AccessKeySecret: accessKeySecret,
101129
Token: token,
102130
TimeoutSecond: DefaultTimeout,
131+
MaxConnsPerHost: DefaultMaxConnsPerHost,
103132
})
104133
}
105134

@@ -109,42 +138,43 @@ func NewAliMNSClientWithConfig(clientConfig AliMNSClientConfig) MNSClient {
109138
}
110139

111140
credential := NewAliMNSCredential(clientConfig.AccessKeySecret, clientConfig.Token)
112-
113141
cli := new(aliMNSClient)
114142
cli.credential = credential
115143
cli.accessKeyId = clientConfig.AccessKeyId
116144
cli.Timeout = clientConfig.TimeoutSecond
145+
if clientConfig.MaxConnsPerHost != 0 {
146+
cli.MaxConnsPerHost = clientConfig.MaxConnsPerHost
147+
} else {
148+
cli.MaxConnsPerHost = DefaultMaxConnsPerHost
149+
}
117150

118151
var err error
119152
if cli.url, err = neturl.Parse(clientConfig.EndPoint); err != nil {
120153
panic("err parse url")
121154
}
122155

123-
// 1. parse region and accountid
156+
// 1. parse region and accountId
124157
pieces := strings.Split(clientConfig.EndPoint, ".")
125158
if len(pieces) != 5 {
126159
panic("ali-mns: message queue url is invalid")
127160
}
128161

129162
accountIdSlice := strings.Split(pieces[0], "/")
130163
cli.accountId = accountIdSlice[len(accountIdSlice)-1]
131-
132164
regionSlice := strings.Split(pieces[2], "-internal")
133165
cli.region = regionSlice[0]
134-
135-
if globalurl := os.Getenv(GLOBAL_PROXY); globalurl != "" {
136-
cli.proxyURL = globalurl
166+
if globalUrl := os.Getenv(GlobalProxy); globalUrl != "" {
167+
cli.proxyURL = globalUrl
137168
}
138169

139170
// 2. now init http client
140171
cli.initFastHttpClient()
141172
//change to dial dual stack to support both ipv4 and ipv6
142173
cli.client.DialDualStack = true
143-
144174
return cli
145175
}
146176

147-
func (p aliMNSClient) getAccountID() (accountId string) {
177+
func (p aliMNSClient) getAccountId() (accountId string) {
148178
return p.accountId
149179
}
150180

@@ -163,16 +193,13 @@ func (p *aliMNSClient) SetProxy(url string) {
163193
func (p *aliMNSClient) initFastHttpClient() {
164194
p.clientLocker.Lock()
165195
defer p.clientLocker.Unlock()
166-
167196
timeoutInt := DefaultTimeout
168-
169197
if p.Timeout > 0 {
170198
timeoutInt = p.Timeout
171199
}
172200

173201
timeout := time.Second * time.Duration(timeoutInt)
174-
175-
p.client = &fasthttp.Client{ReadTimeout: timeout, WriteTimeout: timeout, Name: ClientName}
202+
p.client = &fasthttp.Client{ReadTimeout: timeout, WriteTimeout: timeout, MaxConnsPerHost: p.MaxConnsPerHost, Name: getDefaultUserAgent()}
176203
}
177204

178205
func (p *aliMNSClient) SetTransport(transport fasthttp.RoundTripper) {
@@ -182,7 +209,7 @@ func (p *aliMNSClient) SetTransport(transport fasthttp.RoundTripper) {
182209
}
183210
}
184211

185-
func (p *aliMNSClient) proxy(req *http.Request) (*neturl.URL, error) {
212+
func (p *aliMNSClient) proxy() (*neturl.URL, error) {
186213
if p.proxyURL != "" {
187214
return neturl.Parse(p.proxyURL)
188215
}
@@ -309,6 +336,11 @@ func initMNSErrors() {
309336
}
310337
}
311338

339+
func getDefaultUserAgent() string {
340+
goVersion := strings.TrimPrefix(runtime.Version(), "go")
341+
return fmt.Sprintf("%s/%s(%s/%s/%s;%s)", SdkName, Version, runtime.GOOS, "-", runtime.GOARCH, goVersion)
342+
}
343+
312344
func ParseError(resp ErrorResponse, resource string) (err error) {
313345
if errCodeTemplate, exist := errMapping[resp.Code]; exist {
314346
err = errCodeTemplate.New(errors.Params{"resp": resp, "resource": resource})

example/.gitignore

Lines changed: 0 additions & 2 deletions
This file was deleted.

example/app.conf.example

Lines changed: 0 additions & 5 deletions
This file was deleted.

example/open_service_example.go

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,17 @@
11
package main
22

33
import (
4-
"encoding/json"
54
"fmt"
6-
"io/ioutil"
7-
8-
ali_mns "github.com/aliyun/aliyun-mns-go-sdk"
5+
"github.com/aliyun/aliyun-mns-go-sdk"
96
)
107

11-
type appConf struct {
12-
Url string `json:"url"`
13-
AccessKeyId string `json:"access_key_id"`
14-
AccessKeySecret string `json:"access_key_secret"`
15-
}
16-
178
func main() {
18-
conf := appConf{}
19-
20-
if bFile, e := ioutil.ReadFile("app.conf"); e != nil {
21-
panic(e)
22-
} else {
23-
if e := json.Unmarshal(bFile, &conf); e != nil {
24-
panic(e)
25-
}
26-
}
27-
28-
client := ali_mns.NewAliMNSClient(conf.Url,
29-
conf.AccessKeyId,
30-
conf.AccessKeySecret)
31-
9+
// Replace with your own endpoint.
10+
endpoint := "http://xxx.mns.cn-hangzhou.aliyuncs.com"
11+
client := ali_mns.NewClient(endpoint)
3212
if resp, err := ali_mns.NewAccountManager(client).OpenService(); err != nil {
3313
fmt.Println(err)
3414
} else {
35-
fmt.Printf("reqId:%s, orderId:%s", resp.RequestId, resp.OrderId)
15+
fmt.Printf("reqId: %s, orderId: %s", resp.RequestId, resp.OrderId)
3616
}
3717
}

0 commit comments

Comments
 (0)