Skip to content

Commit

Permalink
Only synchronize PUT and POST requests
Browse files Browse the repository at this point in the history
See Issue #5 for context. Serialized all requests to the API, but only
PUT and POST requests cause the issue.
  • Loading branch information
timohirt committed Jul 16, 2020
1 parent e7e5872 commit 94806e7
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions hetznerdns/api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,8 @@ func (c *Client) doHTTPRequest(apiToken string, method string, url string, body
req.Header.Set("Content-Type", "application/json; charset=utf-8")
}

// This lock ensures that only one request is sent to Hetzber API
// at a time. See issue #5 for context.
// It seems that Terraform creates multiple resources simultanously
// and the API can't handle this right now.
c.requestLock.Lock()

resp, err := client.Do(req)

c.requestLock.Unlock()

if err != nil {
return nil, err
}
Expand Down Expand Up @@ -128,7 +120,13 @@ func (c *Client) doPostRequest(url string, bodyJSON interface{}) (*http.Response
}
body := bytes.NewReader(reqJSON)

return c.doHTTPRequest(c.apiToken, http.MethodPost, url, body)
// This lock ensures that only one Post request is sent to Hetzber API
// at a time. See issue #5 for context.
c.requestLock.Lock()
response, err := c.doHTTPRequest(c.apiToken, http.MethodPost, url, body)
c.requestLock.Unlock()

return response, err
}

func (c *Client) doPutRequest(url string, bodyJSON interface{}) (*http.Response, error) {
Expand All @@ -138,7 +136,13 @@ func (c *Client) doPutRequest(url string, bodyJSON interface{}) (*http.Response,
}
body := bytes.NewReader(reqJSON)

return c.doHTTPRequest(c.apiToken, http.MethodPut, url, body)
// This lock ensures that only one Post request is sent to Hetzber API
// at a time. See issue #5 for context.
c.requestLock.Lock()
response, err := c.doHTTPRequest(c.apiToken, http.MethodPut, url, body)
c.requestLock.Unlock()

return response, err
}

func readAndParseJSONBody(resp *http.Response, respType interface{}) error {
Expand Down

0 comments on commit 94806e7

Please sign in to comment.