From 8c3b74518cce670f5e15f08189966f8b3a3ae467 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Fri, 11 Nov 2022 15:27:18 +0100 Subject: [PATCH] cache branch not found and only log error if error != NotFound --- server/gitea/cache.go | 1 + server/gitea/client.go | 13 ++++++++++++- server/upstream/helper.go | 6 +++++- 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/server/gitea/cache.go b/server/gitea/cache.go index ccc75b4..248c858 100644 --- a/server/gitea/cache.go +++ b/server/gitea/cache.go @@ -67,6 +67,7 @@ func (f FileResponse) createHttpResponse(cacheKey string) (http.Header, int) { type BranchTimestamp struct { Branch string Timestamp time.Time + notFound bool } type writeCacheReader struct { diff --git a/server/gitea/client.go b/server/gitea/client.go index 07302a1..ef35f64 100644 --- a/server/gitea/client.go +++ b/server/gitea/client.go @@ -190,12 +190,22 @@ func (client *Client) GiteaGetRepoBranchTimestamp(repoOwner, repoName, branchNam cacheKey := fmt.Sprintf("%s/%s/%s/%s", branchTimestampCacheKeyPrefix, repoOwner, repoName, branchName) 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) if err != nil { 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{}, err @@ -209,6 +219,7 @@ func (client *Client) GiteaGetRepoBranchTimestamp(repoOwner, repoName, branchNam Timestamp: branch.Commit.Timestamp, } + log.Trace().Msgf("set cache branch [%s] exist", branchName) 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) } diff --git a/server/upstream/helper.go b/server/upstream/helper.go index 7be783d..fa872c2 100644 --- a/server/upstream/helper.go +++ b/server/upstream/helper.go @@ -1,6 +1,8 @@ package upstream import ( + "errors" + "github.com/rs/zerolog/log" "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) 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 } log.Debug().Msgf("Succesfully fetched latest commit's timestamp from branch: %#v", timestamp)