From bd820aa9c52da4568b460a0b8604287f8ed8df26 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Wed, 15 Feb 2023 21:37:34 +0800 Subject: Add context cache as a request level cache (#22294) To avoid duplicated load of the same data in an HTTP request, we can set a context cache to do that. i.e. Some pages may load a user from a database with the same id in different areas on the same page. But the code is hidden in two different deep logic. How should we share the user? As a result of this PR, now if both entry functions accept `context.Context` as the first parameter and we just need to refactor `GetUserByID` to reuse the user from the context cache. Then it will not be loaded twice on an HTTP request. But of course, sometimes we would like to reload an object from the database, that's why `RemoveContextData` is also exposed. The core context cache is here. It defines a new context ```go type cacheContext struct { ctx context.Context data map[any]map[any]any lock sync.RWMutex } var cacheContextKey = struct{}{} func WithCacheContext(ctx context.Context) context.Context { return context.WithValue(ctx, cacheContextKey, &cacheContext{ ctx: ctx, data: make(map[any]map[any]any), }) } ``` Then you can use the below 4 methods to read/write/del the data within the same context. ```go func GetContextData(ctx context.Context, tp, key any) any func SetContextData(ctx context.Context, tp, key, value any) func RemoveContextData(ctx context.Context, tp, key any) func GetWithContextCache[T any](ctx context.Context, cacheGroupKey string, cacheTargetID any, f func() (T, error)) (T, error) ``` Then let's take a look at how `system.GetString` implement it. ```go func GetSetting(ctx context.Context, key string) (string, error) { return cache.GetWithContextCache(ctx, contextCacheKey, key, func() (string, error) { return cache.GetString(genSettingCacheKey(key), func() (string, error) { res, err := GetSettingNoCache(ctx, key) if err != nil { return "", err } return res.SettingValue, nil }) }) } ``` First, it will check if context data include the setting object with the key. If not, it will query from the global cache which may be memory or a Redis cache. If not, it will get the object from the database. In the end, if the object gets from the global cache or database, it will be set into the context cache. An object stored in the context cache will only be destroyed after the context disappeared. --- services/actions/notifier.go | 46 ++++++++++++++++++------------------- services/actions/notifier_helper.go | 6 ++--- 2 files changed, 26 insertions(+), 26 deletions(-) (limited to 'services/actions') diff --git a/services/actions/notifier.go b/services/actions/notifier.go index 0ed69097dc..cdf9087fef 100644 --- a/services/actions/notifier.go +++ b/services/actions/notifier.go @@ -52,7 +52,7 @@ func (n *actionsNotifier) NotifyNewIssue(ctx context.Context, issue *issues_mode Index: issue.Index, Issue: convert.ToAPIIssue(ctx, issue), Repository: convert.ToRepo(ctx, issue.Repo, mode), - Sender: convert.ToUser(issue.Poster, nil), + Sender: convert.ToUser(ctx, issue.Poster, nil), }).Notify(withMethod(ctx, "NotifyNewIssue")) } @@ -70,7 +70,7 @@ func (n *actionsNotifier) NotifyIssueChangeStatus(ctx context.Context, doer *use Index: issue.Index, PullRequest: convert.ToAPIPullRequest(db.DefaultContext, issue.PullRequest, nil), Repository: convert.ToRepo(ctx, issue.Repo, mode), - Sender: convert.ToUser(doer, nil), + Sender: convert.ToUser(ctx, doer, nil), CommitID: commitID, } if isClosed { @@ -88,7 +88,7 @@ func (n *actionsNotifier) NotifyIssueChangeStatus(ctx context.Context, doer *use Index: issue.Index, Issue: convert.ToAPIIssue(ctx, issue), Repository: convert.ToRepo(ctx, issue.Repo, mode), - Sender: convert.ToUser(doer, nil), + Sender: convert.ToUser(ctx, doer, nil), } if isClosed { apiIssue.Action = api.HookIssueClosed @@ -134,7 +134,7 @@ func (n *actionsNotifier) NotifyIssueChangeLabels(ctx context.Context, doer *use Index: issue.Index, PullRequest: convert.ToAPIPullRequest(ctx, issue.PullRequest, nil), Repository: convert.ToRepo(ctx, issue.Repo, perm_model.AccessModeNone), - Sender: convert.ToUser(doer, nil), + Sender: convert.ToUser(ctx, doer, nil), }). Notify(ctx) return @@ -146,7 +146,7 @@ func (n *actionsNotifier) NotifyIssueChangeLabels(ctx context.Context, doer *use Index: issue.Index, Issue: convert.ToAPIIssue(ctx, issue), Repository: convert.ToRepo(ctx, issue.Repo, mode), - Sender: convert.ToUser(doer, nil), + Sender: convert.ToUser(ctx, doer, nil), }). Notify(ctx) } @@ -165,9 +165,9 @@ func (n *actionsNotifier) NotifyCreateIssueComment(ctx context.Context, doer *us WithPayload(&api.IssueCommentPayload{ Action: api.HookIssueCommentCreated, Issue: convert.ToAPIIssue(ctx, issue), - Comment: convert.ToComment(comment), + Comment: convert.ToComment(ctx, comment), Repository: convert.ToRepo(ctx, repo, mode), - Sender: convert.ToUser(doer, nil), + Sender: convert.ToUser(ctx, doer, nil), IsPull: true, }). Notify(ctx) @@ -178,9 +178,9 @@ func (n *actionsNotifier) NotifyCreateIssueComment(ctx context.Context, doer *us WithPayload(&api.IssueCommentPayload{ Action: api.HookIssueCommentCreated, Issue: convert.ToAPIIssue(ctx, issue), - Comment: convert.ToComment(comment), + Comment: convert.ToComment(ctx, comment), Repository: convert.ToRepo(ctx, repo, mode), - Sender: convert.ToUser(doer, nil), + Sender: convert.ToUser(ctx, doer, nil), IsPull: false, }). Notify(ctx) @@ -210,7 +210,7 @@ func (n *actionsNotifier) NotifyNewPullRequest(ctx context.Context, pull *issues Index: pull.Issue.Index, PullRequest: convert.ToAPIPullRequest(ctx, pull, nil), Repository: convert.ToRepo(ctx, pull.Issue.Repo, mode), - Sender: convert.ToUser(pull.Issue.Poster, nil), + Sender: convert.ToUser(ctx, pull.Issue.Poster, nil), }). WithPullRequest(pull). Notify(ctx) @@ -222,8 +222,8 @@ func (n *actionsNotifier) NotifyCreateRepository(ctx context.Context, doer, u *u newNotifyInput(repo, doer, webhook_module.HookEventRepository).WithPayload(&api.RepositoryPayload{ Action: api.HookRepoCreated, Repository: convert.ToRepo(ctx, repo, perm_model.AccessModeOwner), - Organization: convert.ToUser(u, nil), - Sender: convert.ToUser(doer, nil), + Organization: convert.ToUser(ctx, u, nil), + Sender: convert.ToUser(ctx, doer, nil), }).Notify(ctx) } @@ -237,7 +237,7 @@ func (n *actionsNotifier) NotifyForkRepository(ctx context.Context, doer *user_m newNotifyInput(oldRepo, doer, webhook_module.HookEventFork).WithPayload(&api.ForkPayload{ Forkee: convert.ToRepo(ctx, oldRepo, oldMode), Repo: convert.ToRepo(ctx, repo, mode), - Sender: convert.ToUser(doer, nil), + Sender: convert.ToUser(ctx, doer, nil), }).Notify(ctx) u := repo.MustOwner(ctx) @@ -249,8 +249,8 @@ func (n *actionsNotifier) NotifyForkRepository(ctx context.Context, doer *user_m WithPayload(&api.RepositoryPayload{ Action: api.HookRepoCreated, Repository: convert.ToRepo(ctx, repo, perm_model.AccessModeOwner), - Organization: convert.ToUser(u, nil), - Sender: convert.ToUser(doer, nil), + Organization: convert.ToUser(ctx, u, nil), + Sender: convert.ToUser(ctx, doer, nil), }).Notify(ctx) } } @@ -291,7 +291,7 @@ func (n *actionsNotifier) NotifyPullRequestReview(ctx context.Context, pr *issue Index: review.Issue.Index, PullRequest: convert.ToAPIPullRequest(db.DefaultContext, pr, nil), Repository: convert.ToRepo(ctx, review.Issue.Repo, mode), - Sender: convert.ToUser(review.Reviewer, nil), + Sender: convert.ToUser(ctx, review.Reviewer, nil), Review: &api.ReviewPayload{ Type: string(reviewHookType), Content: review.Content, @@ -329,7 +329,7 @@ func (*actionsNotifier) NotifyMergePullRequest(ctx context.Context, doer *user_m Index: pr.Issue.Index, PullRequest: convert.ToAPIPullRequest(db.DefaultContext, pr, nil), Repository: convert.ToRepo(ctx, pr.Issue.Repo, mode), - Sender: convert.ToUser(doer, nil), + Sender: convert.ToUser(ctx, doer, nil), Action: api.HookIssueClosed, } @@ -343,7 +343,7 @@ func (*actionsNotifier) NotifyMergePullRequest(ctx context.Context, doer *user_m func (n *actionsNotifier) NotifyPushCommits(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, opts *repository.PushUpdateOptions, commits *repository.PushCommits) { ctx = withMethod(ctx, "NotifyPushCommits") - apiPusher := convert.ToUser(pusher, nil) + apiPusher := convert.ToUser(ctx, pusher, nil) apiCommits, apiHeadCommit, err := commits.ToAPIPayloadCommits(ctx, repo.RepoPath(), repo.HTMLURL()) if err != nil { log.Error("commits.ToAPIPayloadCommits failed: %v", err) @@ -369,7 +369,7 @@ func (n *actionsNotifier) NotifyPushCommits(ctx context.Context, pusher *user_mo func (n *actionsNotifier) NotifyCreateRef(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, refType, refFullName, refID string) { ctx = withMethod(ctx, "NotifyCreateRef") - apiPusher := convert.ToUser(pusher, nil) + apiPusher := convert.ToUser(ctx, pusher, nil) apiRepo := convert.ToRepo(ctx, repo, perm_model.AccessModeNone) refName := git.RefEndName(refFullName) @@ -388,7 +388,7 @@ func (n *actionsNotifier) NotifyCreateRef(ctx context.Context, pusher *user_mode func (n *actionsNotifier) NotifyDeleteRef(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, refType, refFullName string) { ctx = withMethod(ctx, "NotifyDeleteRef") - apiPusher := convert.ToUser(pusher, nil) + apiPusher := convert.ToUser(ctx, pusher, nil) apiRepo := convert.ToRepo(ctx, repo, perm_model.AccessModeNone) refName := git.RefEndName(refFullName) @@ -407,7 +407,7 @@ func (n *actionsNotifier) NotifyDeleteRef(ctx context.Context, pusher *user_mode func (n *actionsNotifier) NotifySyncPushCommits(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, opts *repository.PushUpdateOptions, commits *repository.PushCommits) { ctx = withMethod(ctx, "NotifySyncPushCommits") - apiPusher := convert.ToUser(pusher, nil) + apiPusher := convert.ToUser(ctx, pusher, nil) apiCommits, apiHeadCommit, err := commits.ToAPIPayloadCommits(db.DefaultContext, repo.RepoPath(), repo.HTMLURL()) if err != nil { log.Error("commits.ToAPIPayloadCommits failed: %v", err) @@ -490,7 +490,7 @@ func (n *actionsNotifier) NotifyPullRequestSynchronized(ctx context.Context, doe Index: pr.Issue.Index, PullRequest: convert.ToAPIPullRequest(ctx, pr, nil), Repository: convert.ToRepo(ctx, pr.Issue.Repo, perm_model.AccessModeNone), - Sender: convert.ToUser(doer, nil), + Sender: convert.ToUser(ctx, doer, nil), }). WithPullRequest(pr). Notify(ctx) @@ -521,7 +521,7 @@ func (n *actionsNotifier) NotifyPullRequestChangeTargetBranch(ctx context.Contex }, PullRequest: convert.ToAPIPullRequest(ctx, pr, nil), Repository: convert.ToRepo(ctx, pr.Issue.Repo, mode), - Sender: convert.ToUser(doer, nil), + Sender: convert.ToUser(ctx, doer, nil), }). WithPullRequest(pr). Notify(ctx) diff --git a/services/actions/notifier_helper.go b/services/actions/notifier_helper.go index 5b8f6bfdf6..df67d2fa11 100644 --- a/services/actions/notifier_helper.go +++ b/services/actions/notifier_helper.go @@ -205,9 +205,9 @@ func notifyRelease(ctx context.Context, doer *user_model.User, rel *repo_model.R WithRef(ref). WithPayload(&api.ReleasePayload{ Action: action, - Release: convert.ToRelease(rel), + Release: convert.ToRelease(ctx, rel), Repository: convert.ToRepo(ctx, rel.Repo, mode), - Sender: convert.ToUser(doer, nil), + Sender: convert.ToUser(ctx, doer, nil), }). Notify(ctx) } @@ -230,7 +230,7 @@ func notifyPackage(ctx context.Context, sender *user_model.User, pd *packages_mo WithPayload(&api.PackagePayload{ Action: action, Package: apiPackage, - Sender: convert.ToUser(sender, nil), + Sender: convert.ToUser(ctx, sender, nil), }). Notify(ctx) } -- cgit v1.2.3