diff options
author | Jaime Martinez <jmartinez@gitlab.com> | 2020-11-13 07:37:38 +0300 |
---|---|---|
committer | Jaime Martinez <jmartinez@gitlab.com> | 2020-11-30 02:15:09 +0300 |
commit | 188a44eba522c23110897ac6fb1091a68680db4e (patch) | |
tree | 4b562ba0b4d7ebc0e52a27d34ae059fadf0e23b8 | |
parent | b0e17d6a69a6e81cd1ca693cba47840de1f230da (diff) |
Add default config
Use DefaultConfig as global
-rw-r--r-- | internal/config/config.go | 41 | ||||
-rw-r--r-- | internal/serving/disk/zip/serving.go | 2 | ||||
-rw-r--r-- | internal/vfs/zip/archive_test.go | 12 | ||||
-rw-r--r-- | internal/vfs/zip/vfs.go | 16 | ||||
-rw-r--r-- | internal/vfs/zip/vfs_test.go | 7 | ||||
-rw-r--r-- | main.go | 29 |
6 files changed, 57 insertions, 50 deletions
diff --git a/internal/config/config.go b/internal/config/config.go index ef2af686..3e0f1965 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -1,22 +1,41 @@ package config -import "time" +import ( + "time" +) // TODO: refactor config flags in main.go and find a better way to handle all settings -// ZipVFSConfig struct that can be accessed by different packages to share +type Config struct { + zip *ZipServing +} + +// DefaultConfig struct that can be accessed by different packages to share // configuration parameters -var ZipVFSConfig *ZipServing +var DefaultConfig = &Config{ + // TODO: think of a way to not repeat these here and in main.go + zip: &ZipServing{ + ExpirationInterval: time.Minute, + CleanupInterval: time.Minute / 2, + RefreshInterval: time.Minute / 2, + OpenTimeout: time.Minute / 2, + }, +} // ZipServing stores all configuration values to be used by the zip VFS opening and // caching type ZipServing struct { - ExpirationInterval time.Duration - CleanupInterval time.Duration - RefreshInterval time.Duration - OpenTimeout time.Duration - DataOffsetItems int64 - DataOffsetExpirationInterval time.Duration - ReadlinkItems int64 - ReadlinkExpirationInterval time.Duration + ExpirationInterval time.Duration + CleanupInterval time.Duration + RefreshInterval time.Duration + OpenTimeout time.Duration +} + +// SetZip config to the global config +func (c *Config) SetZip(zip *ZipServing) { + c.zip = zip +} + +func (c *Config) GetZip() *ZipServing { + return c.zip } diff --git a/internal/serving/disk/zip/serving.go b/internal/serving/disk/zip/serving.go index 9b3a7aa7..53e66f13 100644 --- a/internal/serving/disk/zip/serving.go +++ b/internal/serving/disk/zip/serving.go @@ -14,7 +14,7 @@ var instance serving.Serving // from a zip archives opened from a URL, most likely stored in object storage func Instance() serving.Serving { if instance == nil { - instance = disk.New(vfs.Instrumented(zip.New(config.ZipVFSConfig))) + instance = disk.New(vfs.Instrumented(zip.New(config.DefaultConfig.GetZip()))) } return instance diff --git a/internal/vfs/zip/archive_test.go b/internal/vfs/zip/archive_test.go index f4fd6b5a..2bafece4 100644 --- a/internal/vfs/zip/archive_test.go +++ b/internal/vfs/zip/archive_test.go @@ -20,14 +20,10 @@ import ( var chdirSet = false var zipCfg = &config.ZipServing{ - ExpirationInterval: time.Minute, - CleanupInterval: time.Minute / 2, - RefreshInterval: time.Minute / 2, - OpenTimeout: time.Minute / 2, - DataOffsetItems: 1000, - DataOffsetExpirationInterval: time.Hour, - ReadlinkItems: 500, - ReadlinkExpirationInterval: time.Hour, + ExpirationInterval: time.Minute, + CleanupInterval: time.Minute / 2, + RefreshInterval: time.Minute / 2, + OpenTimeout: time.Minute / 2, } func TestOpen(t *testing.T) { diff --git a/internal/vfs/zip/vfs.go b/internal/vfs/zip/vfs.go index feeab974..d90d4796 100644 --- a/internal/vfs/zip/vfs.go +++ b/internal/vfs/zip/vfs.go @@ -15,6 +15,18 @@ import ( "gitlab.com/gitlab-org/gitlab-pages/metrics" ) +const ( + // we assume that each item costs around 100 bytes + // this gives around 5MB of raw memory needed without acceleration structures + defaultDataOffsetItems = 50000 + defaultDataOffsetExpirationInterval = time.Hour + + // we assume that each item costs around 200 bytes + // this gives around 2MB of raw memory needed without acceleration structures + defaultReadlinkItems = 10000 + defaultReadlinkExpirationInterval = time.Hour +) + var ( errAlreadyCached = errors.New("archive already cached") ) @@ -52,8 +64,8 @@ func New(cfg *config.ZipServing) vfs.VFS { }) // TODO: To be removed with https://gitlab.com/gitlab-org/gitlab-pages/-/issues/480 - zipVFS.dataOffsetCache = newLruCache("data-offset", cfg.DataOffsetItems, cfg.DataOffsetExpirationInterval) - zipVFS.readlinkCache = newLruCache("readlink", cfg.ReadlinkItems, cfg.ReadlinkExpirationInterval) + zipVFS.dataOffsetCache = newLruCache("data-offset", defaultDataOffsetItems, defaultDataOffsetExpirationInterval) + zipVFS.readlinkCache = newLruCache("readlink", defaultReadlinkItems, defaultReadlinkExpirationInterval) return zipVFS } diff --git a/internal/vfs/zip/vfs_test.go b/internal/vfs/zip/vfs_test.go index aa1ed109..0a8a5e0b 100644 --- a/internal/vfs/zip/vfs_test.go +++ b/internal/vfs/zip/vfs_test.go @@ -9,7 +9,6 @@ import ( "github.com/prometheus/client_golang/prometheus/testutil" "github.com/stretchr/testify/require" - "gitlab.com/gitlab-org/gitlab-pages/internal/config" "gitlab.com/gitlab-org/gitlab-pages/internal/httprange" "gitlab.com/gitlab-org/gitlab-pages/internal/vfs" "gitlab.com/gitlab-org/gitlab-pages/metrics" @@ -74,11 +73,7 @@ func TestVFSFindOrOpenArchiveConcurrentAccess(t *testing.T) { path := testServerURL + "/public.zip" - vfs := New(config.ZipServing{ - OpenTimeout: time.Second, - DataOffsetItems: 20, - ReadlinkItems: 20, - }).(*zipVFS) + vfs := New(zipCfg).(*zipVFS) root, err := vfs.Root(context.Background(), path) require.NoError(t, err) @@ -84,17 +84,7 @@ var ( zipCacheExpiration = flag.Duration("zip-cache-expiration", 60*time.Second, "Zip serving archive cache expiration interval (default: 60s)") zipCacheCleanup = flag.Duration("zip-cache-cleanup", 30*time.Second, "Zip serving archive cache cleanup interval (default: 30s)") zipCacheRefresh = flag.Duration("zip-cache-refresh", 30*time.Second, "Zip serving archive cache refresh interval (default: 30s)") - - // we assume that each item costs around 100 bytes - // this gives around 5MB of raw memory needed without acceleration structures - zipCacheDataOffsetItems = flag.Int64("zip-cache-dataoffset-items", 50000, "Zip serving number of files to cache per archive (default: 50,000)") - zipCachetDataOffsetExpiration = flag.Duration("zip-cache-dataoffset-expiration", time.Hour, "Zip serving cached files expiration interval (default: 1h)") - // we assume that each item costs around 200 bytes - // this gives around 2MB of raw memory needed without acceleration structures - zipCacheReadlinkItems = flag.Int64("zip-cache-readlink-items", 10000, "Zip serving number of symbolic links to cache per archive (default: 10,000)") - zipCacheReadlinkExpirationInterval = flag.Duration("zip-cache-readlink-expiration", time.Hour, "Zip serving cached symbolic links expiration interval (default: 1h)") - - zipOpenTimeout = flag.Duration("zip-open-timeout", 30*time.Second, "Zip archive open timeout (default: 30s)") + zipOpenTimeout = flag.Duration("zip-open-timeout", 30*time.Second, "Zip archive open timeout (default: 30s)") // See init() listenHTTP MultiStringFlag @@ -230,18 +220,13 @@ func configFromFlags() appConfig { checkAuthenticationConfig(config) - cfg.ZipVFSConfig = &cfg.ZipServing{ - ExpirationInterval: *zipCacheExpiration, - CleanupInterval: *zipCacheCleanup, - RefreshInterval: *zipCacheRefresh, - OpenTimeout: *zipOpenTimeout, - DataOffsetItems: *zipCacheDataOffsetItems, - DataOffsetExpirationInterval: *zipCachetDataOffsetExpiration, - ReadlinkItems: *zipCacheReadlinkItems, - ReadlinkExpirationInterval: *zipCacheReadlinkExpirationInterval, - } + cfg.DefaultConfig.SetZip(&cfg.ZipServing{ + ExpirationInterval: *zipCacheExpiration, + CleanupInterval: *zipCacheCleanup, + RefreshInterval: *zipCacheRefresh, + OpenTimeout: *zipOpenTimeout, + }) - fmt.Printf("THE CONFIG FIRST:\n%+v\n", cfg.ZipVFSConfig) return config } |