|
| 1 | +package httpsqs |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "net/http" |
| 9 | + "net/url" |
| 10 | + "strconv" |
| 11 | + "strings" |
| 12 | + "time" |
| 13 | + |
| 14 | + "github.com/save95/xerror" |
| 15 | +) |
| 16 | + |
| 17 | +type client struct { |
| 18 | + config *Config |
| 19 | +} |
| 20 | + |
| 21 | +func NewClient(config *Config) IClient { |
| 22 | + return &client{config: config} |
| 23 | +} |
| 24 | + |
| 25 | +func (c *client) Put(ctx context.Context, name, data string) (int64, error) { |
| 26 | + values := url.Values{} |
| 27 | + values.Set("name", name) |
| 28 | + values.Set("opt", "put") |
| 29 | + values.Set("data", data) |
| 30 | + |
| 31 | + body, headers, err := c.httpGet(ctx, values, []string{"pos"}) |
| 32 | + if err != nil { |
| 33 | + return 0, xerror.Wrap(err, "httpsqs put failed") |
| 34 | + } |
| 35 | + |
| 36 | + switch body { |
| 37 | + case "HTTPSQS_PUT_OK": |
| 38 | + pos, _ := strconv.Atoi(headers["pos"]) |
| 39 | + return int64(pos), nil |
| 40 | + case "HTTPSQS_PUT_ERROR": |
| 41 | + return 0, xerror.New("httpsqs put err") |
| 42 | + case "HTTPSQS_PUT_END": |
| 43 | + return -1, xerror.New("httpsqs is full") |
| 44 | + default: |
| 45 | + return 0, xerror.Errorf("httpsqs put failed: %s", body) |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +func (c *client) Get(ctx context.Context, name string) (string, int64, error) { |
| 50 | + values := url.Values{} |
| 51 | + values.Set("name", name) |
| 52 | + values.Set("opt", "get") |
| 53 | + |
| 54 | + body, headers, err := c.httpGet(ctx, values, []string{"pos"}) |
| 55 | + if err != nil { |
| 56 | + return "", 0, xerror.Wrap(err, "httpsqs get failed") |
| 57 | + } |
| 58 | + |
| 59 | + switch body { |
| 60 | + case "HTTPSQS_GET_END": // 没有未取出的消息队列 |
| 61 | + return "", -1, nil |
| 62 | + default: |
| 63 | + pos, _ := strconv.Atoi(headers["pos"]) |
| 64 | + return body, int64(pos), nil |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +func (c *client) Status(ctx context.Context, name string) (*Status, error) { |
| 69 | + values := url.Values{} |
| 70 | + values.Set("name", name) |
| 71 | + values.Set("opt", "status_json") |
| 72 | + |
| 73 | + body, _, err := c.httpGet(ctx, values, nil) |
| 74 | + if err != nil { |
| 75 | + return nil, xerror.Wrap(err, "httpsqs status failed") |
| 76 | + } |
| 77 | + |
| 78 | + var res Status |
| 79 | + if err := json.Unmarshal([]byte(body), &res); nil != err { |
| 80 | + return nil, xerror.Wrapf(err, "httpsqs status failed: response not json: %s", body) |
| 81 | + } |
| 82 | + |
| 83 | + return &res, nil |
| 84 | +} |
| 85 | + |
| 86 | +func (c *client) View(ctx context.Context, name string, pos int64) (string, error) { |
| 87 | + if pos <= 0 || pos > 1000000000 { |
| 88 | + return "", xerror.New("input pos error") |
| 89 | + } |
| 90 | + |
| 91 | + values := url.Values{} |
| 92 | + values.Set("name", name) |
| 93 | + values.Set("opt", "view") |
| 94 | + values.Set("pos", strconv.Itoa(int(pos))) |
| 95 | + |
| 96 | + body, _, err := c.httpGet(ctx, values, nil) |
| 97 | + if err != nil { |
| 98 | + return "", xerror.Wrap(err, "httpsqs view failed") |
| 99 | + } |
| 100 | + |
| 101 | + return body, nil |
| 102 | +} |
| 103 | + |
| 104 | +func (c *client) Reset(ctx context.Context, name string) error { |
| 105 | + values := url.Values{} |
| 106 | + values.Set("name", name) |
| 107 | + values.Set("opt", "reset") |
| 108 | + |
| 109 | + body, _, err := c.httpGet(ctx, values, nil) |
| 110 | + if err != nil { |
| 111 | + return xerror.Wrap(err, "httpsqs reset failed") |
| 112 | + } |
| 113 | + |
| 114 | + switch body { |
| 115 | + case "HTTPSQS_RESET_OK": |
| 116 | + return nil |
| 117 | + default: |
| 118 | + return xerror.Errorf("httpsqs reset failed: %s", body) |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +func (c *client) SetMaxQueue(ctx context.Context, name string, max int) error { |
| 123 | + values := url.Values{} |
| 124 | + values.Set("name", name) |
| 125 | + values.Set("opt", "maxqueue") |
| 126 | + values.Set("num", strconv.Itoa(max)) |
| 127 | + |
| 128 | + body, _, err := c.httpGet(ctx, values, nil) |
| 129 | + if err != nil { |
| 130 | + return xerror.Wrap(err, "httpsqs set max_queue failed") |
| 131 | + } |
| 132 | + |
| 133 | + switch body { |
| 134 | + case "HTTPSQS_MAXQUEUE_OK": |
| 135 | + return nil |
| 136 | + case "HTTPSQS_MAXQUEUE_CANCEL": |
| 137 | + return xerror.New("httpsqs option cancel") |
| 138 | + default: |
| 139 | + return xerror.Errorf("httpsqs set max_queue failed: %s", body) |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +func (c *client) SetSyncTime(ctx context.Context, name string, duration time.Duration) error { |
| 144 | + num := int(duration.Seconds()) |
| 145 | + if num < 1 || num > 1000000000 { |
| 146 | + return xerror.New("input num error") |
| 147 | + } |
| 148 | + |
| 149 | + values := url.Values{} |
| 150 | + values.Set("name", name) |
| 151 | + values.Set("opt", "synctime") |
| 152 | + values.Set("num", strconv.Itoa(num)) |
| 153 | + |
| 154 | + body, _, err := c.httpGet(ctx, values, nil) |
| 155 | + if err != nil { |
| 156 | + return xerror.Wrap(err, "httpsqs set sync_time failed") |
| 157 | + } |
| 158 | + |
| 159 | + switch body { |
| 160 | + case "HTTPSQS_SYNCTIME_OK": |
| 161 | + return nil |
| 162 | + case "HTTPSQS_SYNCTIME_CANCEL": |
| 163 | + return xerror.New("httpsqs option cancel") |
| 164 | + default: |
| 165 | + return xerror.Errorf("httpsqs set synctime failed: %s", body) |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +func (c *client) httpGet(ctx context.Context, values url.Values, parseHeaders []string) (string, map[string]string, error) { |
| 170 | + furl := fmt.Sprintf("http://%s/", c.config.Addr) |
| 171 | + req, err := http.NewRequestWithContext(ctx, http.MethodGet, furl, nil) |
| 172 | + if nil != err { |
| 173 | + return "", nil, err |
| 174 | + } |
| 175 | + |
| 176 | + if len(c.config.Password) > 0 { |
| 177 | + values.Set("auth", c.config.Password) |
| 178 | + } |
| 179 | + req.URL.RawQuery = values.Encode() |
| 180 | + |
| 181 | + httpClient := http.DefaultClient |
| 182 | + if c.config.Timeout > 0 { |
| 183 | + httpClient.Timeout = c.config.Timeout |
| 184 | + } else { |
| 185 | + httpClient.Timeout = 5 * time.Second |
| 186 | + } |
| 187 | + resp, err := httpClient.Do(req) |
| 188 | + if nil != err { |
| 189 | + return "", nil, err |
| 190 | + } |
| 191 | + |
| 192 | + defer func() { |
| 193 | + _ = resp.Body.Close() |
| 194 | + }() |
| 195 | + |
| 196 | + bodyBytes, err := io.ReadAll(resp.Body) |
| 197 | + if err != nil { |
| 198 | + return "", nil, err |
| 199 | + } |
| 200 | + |
| 201 | + body := strings.TrimSpace(string(bodyBytes)) |
| 202 | + switch body { |
| 203 | + case "HTTPSQS_ERROR": // 全局错误 |
| 204 | + return "", nil, xerror.New("httpsqs global error") |
| 205 | + case "HTTPSQS_AUTH_FAILED": // 密码校验失败 |
| 206 | + return "", nil, xerror.New("httpsqs auth error") |
| 207 | + default: |
| 208 | + headerResult := make(map[string]string) |
| 209 | + for _, header := range parseHeaders { |
| 210 | + headerResult[header] = strings.TrimSpace(resp.Header.Get(header)) |
| 211 | + } |
| 212 | + return body, headerResult, nil |
| 213 | + } |
| 214 | +} |
0 commit comments