Skip to content

Commit 8ad642f

Browse files
committed
Refactor the deleteDeadLetter and support pass with the context
1 parent e5daf5b commit 8ad642f

File tree

2 files changed

+36
-58
lines changed

2 files changed

+36
-58
lines changed

client/client.go

+2-58
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
11
package client
22

33
import (
4-
"bytes"
5-
"encoding/json"
64
"fmt"
7-
"io"
8-
"io/ioutil"
95
"net"
106
"net/http"
117
"net/url"
12-
"path"
13-
"strconv"
14-
"strings"
158
"time"
169
)
1710

@@ -189,56 +182,7 @@ func (c *LmstfyClient) RespawnDeadLetter(queue string, limit, ttlSecond int64) (
189182
return c.respawnDeadLetter(nil, queue, limit, ttlSecond)
190183
}
191184

185+
// DeleteDeadLetter deletes the given queue's dead letter
192186
func (c *LmstfyClient) DeleteDeadLetter(queue string, limit int64) *APIError {
193-
if limit <= 0 {
194-
return &APIError{
195-
Type: RequestErr,
196-
Reason: "limit should be > 0",
197-
}
198-
}
199-
query := url.Values{}
200-
query.Add("limit", strconv.FormatInt(limit, 10))
201-
req, err := c.getReq(http.MethodDelete, path.Join(queue, "deadletter"), query, nil)
202-
if err != nil {
203-
return &APIError{
204-
Type: RequestErr,
205-
Reason: err.Error(),
206-
}
207-
}
208-
resp, err := c.httpCli.Do(req)
209-
if err != nil {
210-
return &APIError{
211-
Type: RequestErr,
212-
Reason: err.Error(),
213-
}
214-
}
215-
defer resp.Body.Close()
216-
if resp.StatusCode != http.StatusNoContent {
217-
return &APIError{
218-
Type: ResponseErr,
219-
Reason: parseResponseError(resp),
220-
RequestID: resp.Header.Get("X-Request-ID"),
221-
}
222-
}
223-
return nil
224-
}
225-
226-
func discardResponseBody(resp io.ReadCloser) {
227-
// discard response body, to make this connection reusable in the http connection pool
228-
ioutil.ReadAll(resp)
229-
}
230-
231-
func parseResponseError(resp *http.Response) string {
232-
respBytes, err := ioutil.ReadAll(resp.Body)
233-
if err != nil {
234-
return fmt.Sprintf("Invalid response: %s", err)
235-
}
236-
var errData struct {
237-
Error string `json:"error"`
238-
}
239-
err = json.Unmarshal(respBytes, &errData)
240-
if err != nil {
241-
return fmt.Sprintf("Invalid JSON: %s", err)
242-
}
243-
return fmt.Sprintf("[%d]%s", resp.StatusCode, errData.Error)
187+
return c.deleteDeadLetter(nil, queue, limit)
244188
}

client/client_impl.go

+34
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,40 @@ func (c *LmstfyClient) respawnDeadLetter(ctx context.Context, queue string, limi
753753
return respData.Count, nil
754754
}
755755

756+
func (c *LmstfyClient) deleteDeadLetter(ctx context.Context, queue string, limit int64) *APIError {
757+
if limit <= 0 {
758+
return &APIError{
759+
Type: RequestErr,
760+
Reason: "limit should be > 0",
761+
}
762+
}
763+
query := url.Values{}
764+
query.Add("limit", strconv.FormatInt(limit, 10))
765+
req, err := c.getReq(ctx, http.MethodDelete, path.Join(queue, "deadletter"), query, nil)
766+
if err != nil {
767+
return &APIError{
768+
Type: RequestErr,
769+
Reason: err.Error(),
770+
}
771+
}
772+
resp, err := c.httpCli.Do(req)
773+
if err != nil {
774+
return &APIError{
775+
Type: RequestErr,
776+
Reason: err.Error(),
777+
}
778+
}
779+
defer resp.Body.Close()
780+
if resp.StatusCode != http.StatusNoContent {
781+
return &APIError{
782+
Type: ResponseErr,
783+
Reason: parseResponseError(resp),
784+
RequestID: resp.Header.Get("X-Request-ID"),
785+
}
786+
}
787+
return nil
788+
}
789+
756790
func discardResponseBody(resp io.ReadCloser) {
757791
// discard response body, to make this connection reusable in the http connection pool
758792
_, _ = ioutil.ReadAll(resp)

0 commit comments

Comments
 (0)