diff options
author | John Cai <jcai@gitlab.com> | 2022-08-03 21:02:43 +0300 |
---|---|---|
committer | John Cai <jcai@gitlab.com> | 2022-11-04 21:50:55 +0300 |
commit | 2bdf577155f60ab6ffc78f57735babd9d972c362 (patch) | |
tree | 6154c010ed37d6f0f74236e28f2d8da19625980d | |
parent | 854ed56dd3349c508decaecae816f8b88d929ece (diff) |
hook: Refactor runPackObjects into an injectable function
This is a preperatory commit to make it easier to test concurrency
limiting we will be adding in a later commit. This way we can swap in a
custom function we can control in order to see if the concurrency
limiting is working without using some kind of timer.
-rw-r--r-- | internal/gitaly/service/hook/pack_objects.go | 25 | ||||
-rw-r--r-- | internal/gitaly/service/hook/server.go | 14 |
2 files changed, 33 insertions, 6 deletions
diff --git a/internal/gitaly/service/hook/pack_objects.go b/internal/gitaly/service/hook/pack_objects.go index 3028a46bc..758f8f36c 100644 --- a/internal/gitaly/service/hook/pack_objects.go +++ b/internal/gitaly/service/hook/pack_objects.go @@ -20,7 +20,7 @@ import ( "github.com/sirupsen/logrus" "gitlab.com/gitlab-org/gitaly/v15/internal/git" "gitlab.com/gitlab-org/gitaly/v15/internal/git/pktline" - "gitlab.com/gitlab-org/gitaly/v15/internal/gitaly/hook" + gitalyhook "gitlab.com/gitlab-org/gitaly/v15/internal/gitaly/hook" "gitlab.com/gitlab-org/gitaly/v15/internal/helper" "gitlab.com/gitlab-org/gitaly/v15/internal/stream" "gitlab.com/gitlab-org/gitaly/v15/proto/go/gitalypb" @@ -130,10 +130,23 @@ func (s *server) runPackObjects( defer stdin.Close() + return s.runPackObjectsFn(ctx, s.gitCmdFactory, w, req, args, stdin, key, s.concurrencyTracker) +} + +func runPackObjects( + ctx context.Context, + gitCmdFactory git.CommandFactory, + w io.Writer, + req *gitalypb.PackObjectsHookWithSidechannelRequest, + args *packObjectsArgs, + stdin io.Reader, + key string, + concurrencyTracker *gitalyhook.ConcurrencyTracker, +) error { repo := req.GetRepository() - if s.concurrencyTracker != nil { - finishRepoLog := s.concurrencyTracker.LogConcurrency(ctx, "repository", repo.GetRelativePath()) + if concurrencyTracker != nil { + finishRepoLog := concurrencyTracker.LogConcurrency(ctx, "repository", repo.GetRelativePath()) defer finishRepoLog() userID := req.GetGlId() @@ -142,7 +155,7 @@ func (s *server) runPackObjects( userID = "none" } - finishUserLog := s.concurrencyTracker.LogConcurrency(ctx, "user_id", userID) + finishUserLog := concurrencyTracker.LogConcurrency(ctx, "user_id", userID) defer finishUserLog() } @@ -165,7 +178,7 @@ func (s *server) runPackObjects( } }() - cmd, err := s.gitCmdFactory.New(ctx, repo, args.subcmd(), + cmd, err := gitCmdFactory.New(ctx, repo, args.subcmd(), git.WithStdin(stdin), git.WithStdout(stdout), git.WithStderr(stderr), @@ -301,7 +314,7 @@ func (s *server) PackObjectsHookWithSidechannel(ctx context.Context, req *gitaly return nil, helper.ErrInvalidArgumentf("invalid pack-objects command: %v: %w", req.Args, err) } - c, err := hook.GetSidechannel(ctx) + c, err := gitalyhook.GetSidechannel(ctx) if err != nil { return nil, err } diff --git a/internal/gitaly/service/hook/server.go b/internal/gitaly/service/hook/server.go index c04f82edb..5922f0b41 100644 --- a/internal/gitaly/service/hook/server.go +++ b/internal/gitaly/service/hook/server.go @@ -1,6 +1,9 @@ package hook import ( + "context" + "io" + "gitlab.com/gitlab-org/gitaly/v15/internal/git" gitalyhook "gitlab.com/gitlab-org/gitaly/v15/internal/gitaly/hook" "gitlab.com/gitlab-org/gitaly/v15/internal/streamcache" @@ -13,6 +16,16 @@ type server struct { gitCmdFactory git.CommandFactory packObjectsCache streamcache.Cache concurrencyTracker *gitalyhook.ConcurrencyTracker + runPackObjectsFn func( + context.Context, + git.CommandFactory, + io.Writer, + *gitalypb.PackObjectsHookWithSidechannelRequest, + *packObjectsArgs, + io.Reader, + string, + *gitalyhook.ConcurrencyTracker, + ) error } // NewServer creates a new instance of a gRPC namespace server @@ -27,6 +40,7 @@ func NewServer( gitCmdFactory: gitCmdFactory, packObjectsCache: packObjectsCache, concurrencyTracker: concurrencyTracker, + runPackObjectsFn: runPackObjects, } return srv |