-
Notifications
You must be signed in to change notification settings - Fork 1k
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 AzDo Throttling Timeouts #1715
Closed
oliverisaac
wants to merge
5
commits into
runatlantis:master
from
oliverisaac:azdo-throttling-timeouts
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
905665d
Added config and roundtripper to allow us to handle Azure Devops Retr…
oliverisaac 0f19899
added the cli flags for azuredveops-timeout-seconds and azuredevops-h…
oliverisaac 9aa463b
added check for if GetDiffs returns an error
oliverisaac 3cd98b9
added test for azdo client test to include change counts
oliverisaac 82850c6
added workaround for the ChangeCount bug in teh azdo client
oliverisaac File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,8 @@ import ( | |
"net/http" | ||
"net/url" | ||
"path/filepath" | ||
"regexp" | ||
"strconv" | ||
"strings" | ||
"time" | ||
|
||
|
@@ -22,14 +24,53 @@ type AzureDevopsClient struct { | |
UserName string | ||
} | ||
|
||
type ThrottleRoundtripper struct { | ||
// If azure devops is throttling, this is how long to wait before the next request | ||
RetryAfter time.Duration | ||
ParentRoundtripper http.RoundTripper | ||
} | ||
|
||
func NewThrottleRoundtripper(rt http.RoundTripper) http.RoundTripper { | ||
return &ThrottleRoundtripper{ | ||
ParentRoundtripper: rt, | ||
} | ||
} | ||
|
||
// RoundTrip follows http.Transport interface and will automatically sleep for retryAfter seconds | ||
// This allows us to honor the Retry-After header from Azdo | ||
func (rt *ThrottleRoundtripper) RoundTrip(req *http.Request) (resp *http.Response, err error) { | ||
time.Sleep(rt.RetryAfter) | ||
rt.RetryAfter = 0 | ||
|
||
resp, err = rt.ParentRoundtripper.RoundTrip(req) | ||
if err != nil { | ||
return | ||
} | ||
|
||
retryAfterVal := resp.Header.Get("retry-after") | ||
if retryAfterVal != "" { | ||
retryAfterSec, err := strconv.Atoi(retryAfterVal) | ||
if err == nil { | ||
rt.RetryAfter = time.Duration(retryAfterSec) * time.Second | ||
} | ||
} | ||
Comment on lines
+50
to
+56
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add exponential backoff and jitter here? I know we do this in a lot of other spots but it's not great practice. Risk of thundering herd causing a chain reaction. Also probably good to call out the risks of using this flag in the docs. |
||
|
||
return | ||
} | ||
|
||
// NewAzureDevopsClient returns a valid Azure DevOps client. | ||
func NewAzureDevopsClient(hostname string, userName string, token string) (*AzureDevopsClient, error) { | ||
func NewAzureDevopsClient(hostname string, userName string, token string, timeout time.Duration, honorRetryAfterHeader bool) (*AzureDevopsClient, error) { | ||
tp := azuredevops.BasicAuthTransport{ | ||
Username: "", | ||
Password: strings.TrimSpace(token), | ||
} | ||
httpClient := tp.Client() | ||
httpClient.Timeout = time.Second * 10 | ||
httpClient.Timeout = timeout | ||
|
||
if honorRetryAfterHeader { | ||
httpClient.Transport = NewThrottleRoundtripper(httpClient.Transport) | ||
} | ||
|
||
var adClient, err = azuredevops.NewClient(httpClient) | ||
if err != nil { | ||
return nil, err | ||
|
@@ -68,6 +109,14 @@ func (g *AzureDevopsClient) GetModifiedFiles(repo models.Repo, pull models.PullR | |
sourceRefName := strings.Replace(pullRequest.GetSourceRefName(), "refs/heads/", "", 1) | ||
|
||
r, resp, err := g.Client.Git.GetDiffs(g.ctx, owner, project, repoName, targetRefName, sourceRefName) | ||
if err != nil { | ||
// There is a bug in the underlying azdo library which does not unmarshal requests correctly | ||
// If this PR gets merged, then this bug will go away: https://github.com/mcdafydd/go-azuredevops/pull/12 | ||
matched, err := regexp.MatchString(`cannot unmarshal number [A-Za-z]+ into Go struct field GitCommitDiffs.changeCounts of type azuredevops.VersionControlChangeType`, err.Error()) | ||
if !matched { | ||
return nil, errors.Wrapf(err, "Error getting diff %s to %s", sourceRefName, targetRefName) | ||
} | ||
} | ||
if resp.StatusCode != http.StatusOK { | ||
return nil, errors.Wrapf(err, "http response code %d getting diff %s to %s", resp.StatusCode, sourceRefName, targetRefName) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be nice to put the word "client" in here to be more specific.