-
Notifications
You must be signed in to change notification settings - Fork 7
/
create_queue.go
59 lines (51 loc) · 1.32 KB
/
create_queue.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
package alimns
import (
"context"
"errors"
"fmt"
"net/http"
"regexp"
)
func checkQueueName(name string) bool {
regex := regexp.MustCompile(`^[0-9a-zA-Z]{1}[0-9a-zA-Z-]{0,255}$`)
return regex.MatchString(name)
}
// CreateQueue 创建一个消息队列
func (c *Client) CreateQueue(name string, setters ...QueueAttributeSetter) (string, error) {
var err error
if !checkQueueName(name) {
return "", errors.New("unqualified queue name")
}
attri := DefaultQueueAttri()
for _, setter := range setters {
err = setter(&attri)
if err != nil {
return "", err
}
}
requestLine := fmt.Sprintf(mnsCreateQueue, name)
req := c.ca.NewRequest().Put().WithPath(requestLine).WithXMLBody(&attri).WithTimeout(apiTimeout)
resp, err := c.ca.Do(context.TODO(), req)
if err != nil {
return "", err
}
switch resp.StatusCode() {
case http.StatusCreated:
return resp.Header().Get(location), nil
default:
var respErr RespErr
if err := resp.DecodeFromXML(&respErr); err != nil {
return "", nil
}
switch respErr.Code {
case createQueueNoContentError.Error():
return "", createQueueNoContentError
case createQueueConflictError.Error():
return "", createQueueConflictError
case queueNumExceededLimitError.Error():
return "", queueNumExceededLimitError
default:
return "", errors.New(respErr.Code)
}
}
}