Add re-try mechanism

This commit is contained in:
Gusted 2023-01-28 13:29:54 +01:00
parent 0ca3377e68
commit e1eb876c9b
No known key found for this signature in database
GPG key ID: FD821B732837125F

View file

@ -16,22 +16,37 @@ var canonicalDomainCacheTimeout = 15 * time.Minute
const canonicalDomainConfig = ".domains" const canonicalDomainConfig = ".domains"
// CheckCanonicalDomain returns the canonical domain specified in the repo (using the `.domains` file). // CheckCanonicalDomain returns the canonical domain specified in the repo (using the `.domains` file).
func (o *Options) CheckCanonicalDomain(giteaClient *gitea.Client, actualDomain, mainDomainSuffix string, canonicalDomainCache cache.SetGetKey) (string, bool) { func (o *Options) CheckCanonicalDomain(giteaClient *gitea.Client, actualDomain, mainDomainSuffix string, canonicalDomainCache cache.SetGetKey) (domain string, valid bool) {
var ( // Check if this request is cached.
domains []string
valid bool
)
if cachedValue, ok := canonicalDomainCache.Get(o.TargetOwner + "/" + o.TargetRepo + "/" + o.TargetBranch); ok { if cachedValue, ok := canonicalDomainCache.Get(o.TargetOwner + "/" + o.TargetRepo + "/" + o.TargetBranch); ok {
domains = cachedValue.([]string) domains := cachedValue.([]string)
for _, domain := range domains { for _, domain := range domains {
if domain == actualDomain { if domain == actualDomain {
valid = true valid = true
break break
} }
} }
} else { return domains[0], valid
body, err := giteaClient.GiteaRawContent(o.TargetOwner, o.TargetRepo, o.TargetBranch, canonicalDomainConfig) }
var body []byte
var err error
// Make a request to the Gitea instance.
// Do at least three attempts before bailing out.
for i := 0; i < 3; i++ {
body, err = giteaClient.GiteaRawContent(o.TargetOwner, o.TargetRepo, o.TargetBranch, canonicalDomainConfig)
if err == nil { if err == nil {
break
}
// Only log the error on the last iteration.
if i == 2 {
log.Info().Err(err).Msgf("could not read %s of %s/%s", canonicalDomainConfig, o.TargetOwner, o.TargetRepo)
}
}
var domains []string
for _, domain := range strings.Split(string(body), "\n") { for _, domain := range strings.Split(string(body), "\n") {
domain = strings.ToLower(domain) domain = strings.ToLower(domain)
domain = strings.TrimSpace(domain) domain = strings.TrimSpace(domain)
@ -44,17 +59,23 @@ func (o *Options) CheckCanonicalDomain(giteaClient *gitea.Client, actualDomain,
valid = true valid = true
} }
} }
} else {
log.Info().Err(err).Msgf("could not read %s of %s/%s", canonicalDomainConfig, o.TargetOwner, o.TargetRepo) // Add [owner].[pages-domain] as valid domnain.
}
domains = append(domains, o.TargetOwner+mainDomainSuffix) domains = append(domains, o.TargetOwner+mainDomainSuffix)
if domains[len(domains)-1] == actualDomain { if domains[len(domains)-1] == actualDomain {
valid = true valid = true
} }
// If the target repository isn't called pages, add `/[repository]` to the
// previous valid domain.
if o.TargetRepo != "" && o.TargetRepo != "pages" { if o.TargetRepo != "" && o.TargetRepo != "pages" {
domains[len(domains)-1] += "/" + o.TargetRepo domains[len(domains)-1] += "/" + o.TargetRepo
} }
// Add result to cache.
_ = canonicalDomainCache.Set(o.TargetOwner+"/"+o.TargetRepo+"/"+o.TargetBranch, domains, canonicalDomainCacheTimeout) _ = canonicalDomainCache.Set(o.TargetOwner+"/"+o.TargetRepo+"/"+o.TargetBranch, domains, canonicalDomainCacheTimeout)
}
// Return the first domain from the list and return if any of the domains
// matched the requested domain.
return domains[0], valid return domains[0], valid
} }