Skip to content

Commit

Permalink
Fix post requests sometimes getting context cancelled errors
Browse files Browse the repository at this point in the history
  • Loading branch information
omerfirmak committed Jul 28, 2023
1 parent c3ad723 commit ef665c9
Showing 1 changed file with 4 additions and 16 deletions.
20 changes: 4 additions & 16 deletions clients/gateway/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,31 +96,19 @@ func NewClient(gatewayURL string, log utils.SimpleLogger) *Client {
}

func (c *Client) AddTransaction(txn json.RawMessage) (json.RawMessage, error) {
endpoint := c.url + "/add_transaction"

body, err := c.post(endpoint, txn)
if err != nil {
return nil, err
}
defer body.Close()

res, readErr := io.ReadAll(body)
if readErr != nil {
return nil, readErr
}

return res, nil
return c.post(c.url+"/add_transaction", txn)
}

// post performs additional utility function over doPost method
func (c *Client) post(url string, data any) (io.ReadCloser, error) {
func (c *Client) post(url string, data any) ([]byte, error) {
ctx, cancel := context.WithTimeout(context.Background(), c.timeout)
defer cancel()

resp, err := c.doPost(ctx, url, data)
if err != nil {
return nil, err
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
var gatewayError Error
Expand All @@ -136,7 +124,7 @@ func (c *Client) post(url string, data any) (io.ReadCloser, error) {
return nil, errors.New(resp.Status)
}

return resp.Body, nil
return io.ReadAll(resp.Body)
}

// doPost performs a "POST" http request with the given URL and a JSON payload derived from the provided data
Expand Down

0 comments on commit ef665c9

Please sign in to comment.