Make Client thread-safe & add docs #495

Merged
6543 merged 6 commits from :doc-concurrency into master 2021-03-04 20:19:56 +00:00
Showing only changes of commit 308a9e73ca - Show all commits

View File

@ -161,7 +161,8 @@ func SetDebugMode() func(client *Client) {
func (c *Client) getWebResponse(method, path string, body io.Reader) ([]byte, *Response, error) { func (c *Client) getWebResponse(method, path string, body io.Reader) ([]byte, *Response, error) {
c.mutex.RLock() c.mutex.RLock()
if c.debug { debug := c.debug
if debug {
fmt.Printf("%s: %s\nBody: %v\n", method, c.url+path, body) fmt.Printf("%s: %s\nBody: %v\n", method, c.url+path, body)
} }
req, err := http.NewRequestWithContext(c.ctx, method, c.url+path, body) req, err := http.NewRequestWithContext(c.ctx, method, c.url+path, body)
@ -180,7 +181,7 @@ func (c *Client) getWebResponse(method, path string, body io.Reader) ([]byte, *R
defer resp.Body.Close() defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body) data, err := ioutil.ReadAll(resp.Body)
if c.debug { if debug {
fmt.Printf("Response: %v\n\n", resp) fmt.Printf("Response: %v\n\n", resp)
} }
return data, &Response{resp}, nil return data, &Response{resp}, nil
@ -188,7 +189,8 @@ func (c *Client) getWebResponse(method, path string, body io.Reader) ([]byte, *R
func (c *Client) doRequest(method, path string, header http.Header, body io.Reader) (*Response, error) { func (c *Client) doRequest(method, path string, header http.Header, body io.Reader) (*Response, error) {
c.mutex.RLock() c.mutex.RLock()
if c.debug { debug := c.debug
if debug {
fmt.Printf("%s: %s\nHeader: %v\nBody: %s\n", method, c.url+"/api/v1"+path, header, body) fmt.Printf("%s: %s\nHeader: %v\nBody: %s\n", method, c.url+"/api/v1"+path, header, body)
} }
req, err := http.NewRequestWithContext(c.ctx, method, c.url+"/api/v1"+path, body) req, err := http.NewRequestWithContext(c.ctx, method, c.url+"/api/v1"+path, body)
@ -220,7 +222,7 @@ func (c *Client) doRequest(method, path string, header http.Header, body io.Read
if err != nil { if err != nil {
return nil, err return nil, err
} }
if c.debug { if debug {
fmt.Printf("Response: %v\n\n", resp) fmt.Printf("Response: %v\n\n", resp)
} }
return &Response{resp}, nil return &Response{resp}, nil