diff --git a/server/gitea/client_fasthttp.go b/server/gitea/client_fasthttp.go index 1ec97f3..ce34fae 100644 --- a/server/gitea/client_fasthttp.go +++ b/server/gitea/client_fasthttp.go @@ -87,19 +87,42 @@ func (client *Client) ServeRawContent(targetOwner, targetRepo, ref, resource str } } -func (client *Client) GiteaGetRepoBranchTimestamp(repoOwner, repoName, branchName string) (time.Time, error) { +func (client *Client) GiteaGetRepoBranchTimestamp(repoOwner, repoName, branchName string) (*BranchTimestamp, error) { + 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 + } + url := joinURL(client.giteaRoot, giteaAPIRepos, repoOwner, repoName, "branches", branchName) res, err := client.do(client.infoTimeout, url) if err != nil { - return time.Time{}, err + return &BranchTimestamp{}, err } if res.StatusCode() != fasthttp.StatusOK { - return time.Time{}, fmt.Errorf("unexpected status code '%d'", res.StatusCode()) + return &BranchTimestamp{}, fmt.Errorf("unexpected status code '%d'", res.StatusCode()) } - return time.Parse(time.RFC3339, fastjson.GetString(res.Body(), "commit", "timestamp")) + timestamp, err := time.Parse(time.RFC3339, fastjson.GetString(res.Body(), "commit", "timestamp")) + if err != nil { + return &BranchTimestamp{}, err + } + + stamp := &BranchTimestamp{ + Branch: branchName, + Timestamp: timestamp, + } + + client.responseCache.Set(cacheKey, stamp, branchExistenceCacheTimeout) + return stamp, nil } func (client *Client) GiteaGetRepoDefaultBranch(repoOwner, repoName string) (string, error) { + cacheKey := fmt.Sprintf("%s/%s/%s", defaultBranchCacheKeyPrefix, repoOwner, repoName) + + if branch, ok := client.responseCache.Get(cacheKey); ok && branch != nil { + return branch.(string), nil + } + url := joinURL(client.giteaRoot, giteaAPIRepos, repoOwner, repoName) res, err := client.do(client.infoTimeout, url) if err != nil { @@ -108,7 +131,10 @@ func (client *Client) GiteaGetRepoDefaultBranch(repoOwner, repoName string) (str if res.StatusCode() != fasthttp.StatusOK { return "", fmt.Errorf("unexpected status code '%d'", res.StatusCode()) } - return fastjson.GetString(res.Body(), "default_branch"), nil + + branch := fastjson.GetString(res.Body(), "default_branch") + client.responseCache.Set(cacheKey, branch, defaultBranchCacheTimeout) + return branch, nil } func (client *Client) do(timeout time.Duration, url string) (*fasthttp.Response, error) { diff --git a/server/try.go b/server/try.go index 3f2d7dc..a1ee8ac 100644 --- a/server/try.go +++ b/server/try.go @@ -45,7 +45,7 @@ func tryUpstream(ctx *fasthttp.RequestCtx, giteaClient *gitea.Client, targetOptions.TargetPath = targetPath // Try to request the file from the Gitea API - if !targetOptions.Upstream(ctx, giteaClient, branchTimestampCache) { + if !targetOptions.Upstream(ctx, giteaClient) { html.ReturnErrorPage(ctx, ctx.Response.StatusCode()) } } diff --git a/server/upstream/upstream.go b/server/upstream/upstream.go index e647671..76dce20 100644 --- a/server/upstream/upstream.go +++ b/server/upstream/upstream.go @@ -5,7 +5,6 @@ import ( "errors" "fmt" "io" - "net/http" "strings" "time" @@ -43,7 +42,7 @@ type Options struct { } // Upstream requests a file from the Gitea API at GiteaRoot and writes it to the request context. -func (o *Options) Upstream(ctx *http.Response, giteaClient *gitea.Client) (final bool) { +func (o *Options) Upstream(ctx *fasthttp.RequestCtx, giteaClient *gitea.Client) (final bool) { log := log.With().Strs("upstream", []string{o.TargetOwner, o.TargetRepo, o.TargetBranch, o.TargetPath}).Logger() // Check if the branch exists and when it was modified @@ -74,14 +73,11 @@ func (o *Options) Upstream(ctx *http.Response, giteaClient *gitea.Client) (final // Make a GET request to the upstream URL uri := o.generateUri() - var res *fasthttp.Response var cachedResponse gitea.FileResponse - var err error - if cachedValue, ok := fileResponseCache.Get(uri + "?timestamp=" + o.timestamp()); ok && !cachedValue.(gitea.FileResponse).IsEmpty() { - cachedResponse = cachedValue.(gitea.FileResponse) - } else { - res, err = giteaClient.ServeRawContent(o.generateUriClientArgs()) - } + // if cachedValue, ok := fileResponseCache.Get(uri + "?timestamp=" + o.timestamp()); ok && !cachedValue.(gitea.FileResponse).IsEmpty() { + // cachedResponse = cachedValue.(gitea.FileResponse) + // } else { + res, err := giteaClient.ServeRawContent(o.generateUriClientArgs()) log.Debug().Msg("acquisition") // Handle errors