diff options
author | Patrick Steinhardt <psteinhardt@gitlab.com> | 2021-05-05 10:22:20 +0300 |
---|---|---|
committer | Patrick Steinhardt <psteinhardt@gitlab.com> | 2021-05-10 09:26:28 +0300 |
commit | db551f291a7b985920b98f70914d8df63afcb222 (patch) | |
tree | a9346b67b4223d66dbdf6593816a5a22f20f860a | |
parent | f0af679fdee1a829dc33cecc28f27fb570492009 (diff) |
service: Set up catfile cache as global dependency
We have all prerequisites in place to start injecting the catfile cache
as dependency into its users, which will eventually let us get rid of
the global cache variable.
Implement a new `NewCache()` function which takes as input the Gitaly
configuration and command factory and inject it into service
dependencies.
-rw-r--r-- | cmd/gitaly/main.go | 4 | ||||
-rw-r--r-- | internal/git/catfile/batch_cache.go | 16 | ||||
-rw-r--r-- | internal/gitaly/service/dependencies.go | 7 | ||||
-rw-r--r-- | internal/testhelper/testserver/gitaly.go | 15 |
4 files changed, 36 insertions, 6 deletions
diff --git a/cmd/gitaly/main.go b/cmd/gitaly/main.go index f1b2d2e3c..c054123b5 100644 --- a/cmd/gitaly/main.go +++ b/cmd/gitaly/main.go @@ -15,6 +15,7 @@ import ( "gitlab.com/gitlab-org/gitaly/internal/bootstrap/starter" "gitlab.com/gitlab-org/gitaly/internal/cgroups" "gitlab.com/gitlab-org/gitaly/internal/git" + "gitlab.com/gitlab-org/gitaly/internal/git/catfile" "gitlab.com/gitlab-org/gitaly/internal/gitaly/config" "gitlab.com/gitlab-org/gitaly/internal/gitaly/config/sentry" "gitlab.com/gitlab-org/gitaly/internal/gitaly/hook" @@ -167,6 +168,8 @@ func run(cfg config.Cfg) error { gitCmdFactory := git.NewExecCommandFactory(cfg) prometheus.MustRegister(gitCmdFactory) + catfileCache := catfile.NewCache(gitCmdFactory, cfg) + gitalyServerFactory := server.NewGitalyServerFactory(cfg, registry) defer gitalyServerFactory.Stop() @@ -206,6 +209,7 @@ func run(cfg config.Cfg) error { ClientPool: conns, GitCmdFactory: gitCmdFactory, Linguist: ling, + CatfileCache: catfileCache, }) b.RegisterStarter(starter.New(c, srv)) } diff --git a/internal/git/catfile/batch_cache.go b/internal/git/catfile/batch_cache.go index 0cecf61b9..c9edcd189 100644 --- a/internal/git/catfile/batch_cache.go +++ b/internal/git/catfile/batch_cache.go @@ -44,12 +44,7 @@ var cache Cache func init() { config.RegisterHook(func(cfg *config.Cfg) error { - cache = newCache( - git.NewExecCommandFactory(*cfg), - defaultBatchfileTTL, - cfg.Git.CatfileCacheSize, - defaultEvictionInterval, - ) + cache = NewCache(git.NewExecCommandFactory(*cfg), *cfg) return nil }) } @@ -99,6 +94,15 @@ type BatchCache struct { injectSpawnErrors bool } +// NewCache creates a new catfile process cache. +func NewCache(gitCmdFactory git.CommandFactory, cfg config.Cfg) *BatchCache { + return newCache(gitCmdFactory, + defaultBatchfileTTL, + cfg.Git.CatfileCacheSize, + defaultEvictionInterval, + ) +} + func newCache(gitCmdFactory git.CommandFactory, ttl time.Duration, maxLen int, refreshInterval time.Duration) *BatchCache { if maxLen <= 0 { maxLen = defaultMaxLen diff --git a/internal/gitaly/service/dependencies.go b/internal/gitaly/service/dependencies.go index ee67bfd29..8f312e91e 100644 --- a/internal/gitaly/service/dependencies.go +++ b/internal/gitaly/service/dependencies.go @@ -4,6 +4,7 @@ import ( "gitlab.com/gitlab-org/gitaly/client" "gitlab.com/gitlab-org/gitaly/internal/backchannel" "gitlab.com/gitlab-org/gitaly/internal/git" + "gitlab.com/gitlab-org/gitaly/internal/git/catfile" "gitlab.com/gitlab-org/gitaly/internal/gitaly/config" gitalyhook "gitlab.com/gitlab-org/gitaly/internal/gitaly/hook" "gitlab.com/gitlab-org/gitaly/internal/gitaly/linguist" @@ -25,6 +26,7 @@ type Dependencies struct { Linguist *linguist.Instance BackchannelRegistry *backchannel.Registry GitlabClient gitlab.Client + CatfileCache catfile.Cache } // GetCfg returns service configuration. @@ -76,3 +78,8 @@ func (dc *Dependencies) GetBackchannelRegistry() *backchannel.Registry { func (dc *Dependencies) GetGitlabClient() gitlab.Client { return dc.GitlabClient } + +// GetCatfileCache returns catfile cache. +func (dc *Dependencies) GetCatfileCache() catfile.Cache { + return dc.CatfileCache +} diff --git a/internal/testhelper/testserver/gitaly.go b/internal/testhelper/testserver/gitaly.go index 4f6cce06c..5be0b5480 100644 --- a/internal/testhelper/testserver/gitaly.go +++ b/internal/testhelper/testserver/gitaly.go @@ -17,6 +17,7 @@ import ( "gitlab.com/gitlab-org/gitaly/client" "gitlab.com/gitlab-org/gitaly/internal/backchannel" "gitlab.com/gitlab-org/gitaly/internal/git" + "gitlab.com/gitlab-org/gitaly/internal/git/catfile" "gitlab.com/gitlab-org/gitaly/internal/gitaly/config" "gitlab.com/gitlab-org/gitaly/internal/gitaly/config/auth" gitalylog "gitlab.com/gitlab-org/gitaly/internal/gitaly/config/log" @@ -269,6 +270,7 @@ type gitalyServerDeps struct { gitCmdFactory git.CommandFactory linguist *linguist.Instance backchannelReg *backchannel.Registry + catfileCache catfile.Cache } func (gsd *gitalyServerDeps) createDependencies(t testing.TB, cfg config.Cfg, rubyServer *rubyserver.Server) *service.Dependencies { @@ -310,6 +312,10 @@ func (gsd *gitalyServerDeps) createDependencies(t testing.TB, cfg config.Cfg, ru require.NoError(t, err) } + if gsd.catfileCache == nil { + gsd.catfileCache = catfile.NewCache(gsd.gitCmdFactory, cfg) + } + return &service.Dependencies{ Cfg: cfg, RubyServer: rubyServer, @@ -321,6 +327,7 @@ func (gsd *gitalyServerDeps) createDependencies(t testing.TB, cfg config.Cfg, ru Linguist: gsd.linguist, BackchannelRegistry: gsd.backchannelReg, GitlabClient: gsd.gitlabClient, + CatfileCache: gsd.catfileCache, } } @@ -382,3 +389,11 @@ func WithBackchannelRegistry(backchannelReg *backchannel.Registry) GitalyServerO return deps } } + +// WithCatfileCache sets catfile.Cache instance that will be used for gitaly services initialisation. +func WithCatfileCache(catfileCache catfile.Cache) GitalyServerOpt { + return func(deps gitalyServerDeps) gitalyServerDeps { + deps.catfileCache = catfileCache + return deps + } +} |