Debian Codegraph @

mq/client.go

raw symbols SCIP
1 package mq
2
3 import (
4 "encoding/json"
5 "errors"
6 "fmt"
7 "strconv"
8 )
9
10 type Client struct {
11 AccessKey string
12 SecretKey string
13 Endpoint string
14 Topic string
15 ProducerId string
16 ConsumerId string
17 Key string
18 Tag string
19 }
20
21 type MessageResponse struct {
22 Body string `json:"body"`
23 BornTime int64 `json:"bornTime"` // UTC time in Unix
24 }
25
26 func NewClient(ak string, sk string, endpoint string, topic string,
27 producerId string, consumerId string, key string, tag string) (client *Client) {
28 client = &Client{
29 AccessKey: ak,
30 SecretKey: sk,
31 Endpoint: endpoint,
32 Topic: topic,
33 ProducerId: producerId,
34 ConsumerId: consumerId,
35 Key: key,
36 Tag: tag,
37 }
38 return client
39 }
40
41 func getSendUrl(endpoint string, topic string, time int64, tag string, key string) string {
42 return endpoint + "/message/?topic=" + topic + "&time=" +
43 strconv.FormatInt(time, 10) + "&tag=" + tag + "&key=" + key
44 }
45
46 func getReceiveUrl(endpoint, topic string, time int64, tag string, num int) string {
47 return endpoint + "/message/?topic=" + topic + "&time=" +
48 strconv.FormatInt(time, 10) + "&tag=" + tag + "&num=" + strconv.Itoa(num)
49 }
50
51 func getSendSign(topic string, producerId string, messageBody []byte, time int64, sk string) (sign string) {
52 signStr := topic + newline + producerId + newline + Md5(messageBody) + newline + strconv.FormatInt(time, 10)
53 sign = HamSha1(signStr, []byte(sk))
54 return sign
55 }
56
57 func getReceiveSign(topic string, consumerId string, time int64, sk string) string {
58 // [topic+ā€\nā€+ cid+ā€\nā€+time]
59 signStr := topic + newline + consumerId + newline + strconv.FormatInt(time, 10)
60 return HamSha1(signStr, []byte(sk))
61 }
62
63 func getReceiveHeader(ak, sign, consumerId string) (map[string]string, error) {
64 if consumerId == "" {
65 return nil, fmt.Errorf("consumer id is not provided")
66 }
67 header := make(map[string]string)
68 header["AccessKey"] = ak
69 header["Signature"] = sign
70 header["ConsumerId"] = consumerId
71 return header, nil
72 }
73
74 func getSendHeader(ak string, sign string, producerId string) (header map[string]string, err error) {
75 if producerId == "" {
76 return nil, fmt.Errorf("producer id is not provided")
77 }
78 header = make(map[string]string, 0)
79 header["AccessKey"] = ak
80 header["Signature"] = sign
81 header["ProducerId"] = producerId
82 return header, nil
83 }
84
85 func (client *Client) Send(time int64, message []byte) (msgId string, err error) {
86 url := getSendUrl(client.Endpoint, client.Topic, time, client.Tag, client.Key)
87 sign := getSendSign(client.Topic, client.ProducerId, message, time, client.SecretKey)
88 header, err := getSendHeader(client.AccessKey, sign, client.ProducerId)
89 if err != nil {
90 return "", err
91 }
92 response, status, err := httpPost(url, header, message)
93 if err != nil {
94 return "", err
95 }
96
97 fmt.Printf("receive message: %s %d", response, status)
98 statusMessage := getStatusCodeMessage(status)
99 if statusMessage != "" {
100 return "", errors.New(statusMessage)
101 }
102
103 var rs interface{}
104 err = json.Unmarshal(response, &rs)
105 if err != nil {
106 return "", err
107 }
108
109 result := rs.(map[string]interface{})
110
111 sendStatus := result["sendStatus"].(string)
112 if sendStatus != "SEND_OK" {
113 return "", errors.New(sendStatus)
114 }
115
116 return result["msgId"].(string), nil
117 }
118
119 func (client *Client) ReceiveMessage(messageChan chan string, errChan chan error) {
120 // only listen for the latest message
121 time := GetCurrentUnixMicro()
122 url := getReceiveUrl(client.Endpoint, client.Topic, time, client.Tag, 1)
123 sign := getReceiveSign(client.Topic, client.ConsumerId, time, client.SecretKey)
124 header, err := getReceiveHeader(client.AccessKey, sign, client.ConsumerId)
125 if err != nil {
126 errChan <- err
127 return
128 }
129 response, status, err := HttpGet(url, header)
130 if err != nil {
131 errChan <- err
132 return
133 }
134
135 fmt.Printf("receive message: %s %d", response, status)
136 statusMessage := getStatusCodeMessage(status)
137 if statusMessage != "" {
138 errChan <- errors.New(statusMessage)
139 return
140 }
141
142 messages := make([]MessageResponse, 0)
143 json.Unmarshal(response, &messages)
144
145 if len(messages) > 0 {
146 fmt.Printf("size of messages is %d", len(messages))
147 message := messages[0]
148 messageChan <- message.Body
149 } else {
150 errChan <- fmt.Errorf("no message available")
151 return
152 }
153 }