logclient: buffer JSON request body

This permits us to detect JSON marshalling errors, and makes it easy to
retry the request.

Request bodies are short so this should have negligible performance impact.
This commit is contained in:
Andrew Ayer 2022-05-31 15:37:47 -04:00
parent c59eecfdec
commit f7f79f2600
1 changed files with 5 additions and 7 deletions

View File

@ -11,7 +11,6 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"io"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"net/url" "net/url"
@ -124,12 +123,11 @@ func (c *LogClient) fetchAndParse(uri string, respBody interface{}) error {
} }
func (c *LogClient) postAndParse(uri string, body interface{}, respBody interface{}) error { func (c *LogClient) postAndParse(uri string, body interface{}, respBody interface{}) error {
bodyReader, bodyWriter := io.Pipe() bodyBytes, err := json.Marshal(body)
go func() { if err != nil {
json.NewEncoder(bodyWriter).Encode(body) return err
bodyWriter.Close() }
}() req, err := http.NewRequest("POST", uri, bytes.NewReader(bodyBytes))
req, err := http.NewRequest("POST", uri, bodyReader)
if err != nil { if err != nil {
return fmt.Errorf("POST %s: Sending request failed: %s", uri, err) return fmt.Errorf("POST %s: Sending request failed: %s", uri, err)
} }