cache branch not found and only log error if error != NotFound

This commit is contained in:
6543 2022-11-11 15:27:18 +01:00
parent 7526873049
commit 8c3b74518c
No known key found for this signature in database
GPG key ID: B8BE6D610E61C862
3 changed files with 18 additions and 2 deletions

View file

@ -67,6 +67,7 @@ func (f FileResponse) createHttpResponse(cacheKey string) (http.Header, int) {
type BranchTimestamp struct { type BranchTimestamp struct {
Branch string Branch string
Timestamp time.Time Timestamp time.Time
notFound bool
} }
type writeCacheReader struct { type writeCacheReader struct {

View file

@ -190,12 +190,22 @@ func (client *Client) GiteaGetRepoBranchTimestamp(repoOwner, repoName, branchNam
cacheKey := fmt.Sprintf("%s/%s/%s/%s", branchTimestampCacheKeyPrefix, repoOwner, repoName, branchName) cacheKey := fmt.Sprintf("%s/%s/%s/%s", branchTimestampCacheKeyPrefix, repoOwner, repoName, branchName)
if stamp, ok := client.responseCache.Get(cacheKey); ok && stamp != nil { if stamp, ok := client.responseCache.Get(cacheKey); ok && stamp != nil {
return stamp.(*BranchTimestamp), nil branchTimeStamp := stamp.(*BranchTimestamp)
if branchTimeStamp.notFound {
log.Trace().Msgf("use cache branch [%s] not found", branchName)
return &BranchTimestamp{}, ErrorNotFound
}
log.Trace().Msgf("use cache branch [%s] exist", branchName)
return branchTimeStamp, nil
} }
branch, resp, err := client.sdkClient.GetRepoBranch(repoOwner, repoName, branchName) branch, resp, err := client.sdkClient.GetRepoBranch(repoOwner, repoName, branchName)
if err != nil { if err != nil {
if resp != nil && resp.StatusCode == http.StatusNotFound { if resp != nil && resp.StatusCode == http.StatusNotFound {
log.Trace().Msgf("set cache branch [%s] not found", branchName)
if err := client.responseCache.Set(cacheKey, &BranchTimestamp{Branch: branchName, notFound: true}, branchExistenceCacheTimeout); err != nil {
log.Error().Err(err).Msgf("error on store of repo branch timestamp [%s/%s@%s]", repoOwner, repoName, branchName)
}
return &BranchTimestamp{}, ErrorNotFound return &BranchTimestamp{}, ErrorNotFound
} }
return &BranchTimestamp{}, err return &BranchTimestamp{}, err
@ -209,6 +219,7 @@ func (client *Client) GiteaGetRepoBranchTimestamp(repoOwner, repoName, branchNam
Timestamp: branch.Commit.Timestamp, Timestamp: branch.Commit.Timestamp,
} }
log.Trace().Msgf("set cache branch [%s] exist", branchName)
if err := client.responseCache.Set(cacheKey, stamp, branchExistenceCacheTimeout); err != nil { if err := client.responseCache.Set(cacheKey, stamp, branchExistenceCacheTimeout); err != nil {
log.Error().Err(err).Msgf("error on store of repo branch timestamp [%s/%s@%s]", repoOwner, repoName, branchName) log.Error().Err(err).Msgf("error on store of repo branch timestamp [%s/%s@%s]", repoOwner, repoName, branchName)
} }

View file

@ -1,6 +1,8 @@
package upstream package upstream
import ( import (
"errors"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"codeberg.org/codeberg/pages/server/gitea" "codeberg.org/codeberg/pages/server/gitea"
@ -24,7 +26,9 @@ func GetBranchTimestamp(giteaClient *gitea.Client, owner, repo, branch string) *
timestamp, err := giteaClient.GiteaGetRepoBranchTimestamp(owner, repo, branch) timestamp, err := giteaClient.GiteaGetRepoBranchTimestamp(owner, repo, branch)
if err != nil { if err != nil {
log.Err(err).Msg("Could not get latest commit's timestamp from branch") if !errors.Is(err, gitea.ErrorNotFound) {
log.Error().Err(err).Msg("Could not get latest commit's timestamp from branch")
}
return nil return nil
} }
log.Debug().Msgf("Succesfully fetched latest commit's timestamp from branch: %#v", timestamp) log.Debug().Msgf("Succesfully fetched latest commit's timestamp from branch: %#v", timestamp)