Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix post requests sometimes getting context cancelled errors #970

Merged
merged 1 commit into from
Jul 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading