Skip to content

Commit

Permalink
Reuse http client used with ASM API (Azure#3858)
Browse files Browse the repository at this point in the history
* Reuse the http client for ASM calls. Throttle retries when ServiceUnavailable responses are received

* code cleanup:-
Removed the only other occurence in sendAzureRequest() since the httpClient was already setup in makeClient().
Removed the nil check inside the function as the parameter is passed in as a value type.

* Reverted the change as it was inadvertent during testing

* Code improvements.  Use the configurable Poll interval and check for both service unavaialbe and throttling messages
  • Loading branch information
nshampur authored and jhendrixMSFT committed Jan 15, 2019
1 parent 82a3481 commit 5c287d1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
12 changes: 10 additions & 2 deletions services/classic/management/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package management
import (
"errors"
"fmt"
"net/http"
"runtime"
"time"

Expand All @@ -44,6 +45,7 @@ const (
type client struct {
publishSettings publishSettings
config ClientConfig
httpClient *http.Client
}

// Client is the base Azure Service Management API client instance that
Expand Down Expand Up @@ -154,10 +156,16 @@ func makeClient(subscriptionID string, managementCert []byte, config ClientConfi
config.UserAgent = DefaultUserAgent
}

return client{
clientObj := client{
publishSettings: publishSettings,
config: config,
}, nil
}
var err error
clientObj.httpClient, err = clientObj.createHTTPClient()
if err != nil {
return nil, err
}
return clientObj, nil
}

func userAgent() string {
Expand Down
12 changes: 6 additions & 6 deletions services/classic/management/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"crypto/tls"
"fmt"
"net/http"
"time"
)

const (
Expand Down Expand Up @@ -86,12 +87,7 @@ func (client client) sendAzureRequest(method, url, contentType string, data []by
return nil, fmt.Errorf(errParamNotSpecified, "url")
}

httpClient, err := client.createHTTPClient()
if err != nil {
return nil, err
}

response, err := client.sendRequest(httpClient, url, method, contentType, data, 5)
response, err := client.sendRequest(client.httpClient, url, method, contentType, data, 5)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -161,6 +157,10 @@ func (client client) sendRequest(httpClient *http.Client, url, requestType, cont
if numberOfRetries == 0 {
return nil, azureErr
}
if response.StatusCode == http.StatusServiceUnavailable || response.StatusCode == http.StatusTooManyRequests {
// Wait before retrying the operation
time.Sleep(client.config.OperationPollInterval)
}

return client.sendRequest(httpClient, url, requestType, contentType, data, numberOfRetries-1)
}
Expand Down

0 comments on commit 5c287d1

Please sign in to comment.