diff --git a/routers/web/repo/code_frequency.go b/routers/web/repo/code_frequency.go index 2b2dd5744a..9e5c1dc1e3 100644 --- a/routers/web/repo/code_frequency.go +++ b/routers/web/repo/code_frequency.go @@ -4,7 +4,6 @@ package repo import ( - "errors" "net/http" "code.gitea.io/gitea/modules/templates" @@ -30,11 +29,7 @@ func CodeFrequency(ctx *context.Context) { // CodeFrequencyData returns JSON of code frequency data func CodeFrequencyData(ctx *context.Context) { if contributorStats, err := contributors_service.GetContributorStats(ctx, ctx.Cache, ctx.Repo.Repository, ctx.Repo.Repository.DefaultBranch); err != nil { - if errors.Is(err, contributors_service.ErrAwaitGeneration) { - ctx.Status(http.StatusAccepted) - return - } - ctx.ServerError("GetContributorStats", err) + ctx.ServerError("GetCodeFrequencyData", err) } else { ctx.JSON(http.StatusOK, contributorStats["total"].Weeks) } diff --git a/routers/web/repo/contributors.go b/routers/web/repo/contributors.go index 26ab0a806c..c7785ce5a6 100644 --- a/routers/web/repo/contributors.go +++ b/routers/web/repo/contributors.go @@ -4,8 +4,6 @@ package repo import ( - std_ctx "context" - "errors" "net/http" "code.gitea.io/gitea/modules/templates" @@ -28,10 +26,6 @@ func Contributors(ctx *context.Context) { // ContributorsData renders JSON of contributors along with their weekly commit statistics func ContributorsData(ctx *context.Context) { if contributorStats, err := contributors_service.GetContributorStats(ctx, ctx.Cache, ctx.Repo.Repository, ctx.Repo.Repository.DefaultBranch); err != nil { - if errors.Is(err, contributors_service.ErrAwaitGeneration) || errors.Is(err, std_ctx.DeadlineExceeded) { - ctx.Status(http.StatusAccepted) - return - } ctx.ServerError("GetContributorStats", err) } else { ctx.JSON(http.StatusOK, contributorStats) diff --git a/services/repository/contributors_graph.go b/services/repository/contributors_graph.go index f91d18fc92..b9abc7612b 100644 --- a/services/repository/contributors_graph.go +++ b/services/repository/contributors_graph.go @@ -29,10 +29,7 @@ const ( contributorStatsCacheTimeout int64 = 60 * 10 ) -var ( - ErrAwaitGeneration = errors.New("generation took longer than ") - awaitGenerationTime = time.Second * 60 -) +var ErrAwaitGeneration = errors.New("generation took longer than ") type WeekData struct { Week int64 `json:"week"` // Starting day of the week as Unix timestamp @@ -95,19 +92,18 @@ func GetContributorStats(ctx context.Context, cache cache.StringCache, repo *rep } defer releaser() - // set a timeout for the generation - ctx, cancel := context.WithTimeout(ctx, awaitGenerationTime) - defer cancel() + // check if generation is already completed by other request + if cache.IsExist(cacheKey) { + var res map[string]*ContributorData + if _, cacheErr := cache.GetJSON(cacheKey, &res); cacheErr != nil { + return nil, fmt.Errorf("cached error: %w", cacheErr.ToError()) + } + return res, nil + } // run generation async res, err := generateContributorStats(ctx, cache, cacheKey, repo, revision) if err != nil { - switch { - case errors.Is(err, context.DeadlineExceeded): - _ = cache.PutJSON(cacheKey, fmt.Errorf("generateContributorStats: %w", ErrAwaitGeneration), contributorStatsCacheTimeout) - default: - _ = cache.PutJSON(cacheKey, fmt.Errorf("generateContributorStats: %w", err), contributorStatsCacheTimeout) - } return nil, err }