From 00a981d341dfa62d2a9667b01500820ec7ddf19a Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Sun, 15 May 2022 20:43:27 +0200 Subject: [PATCH] Update go-chi/cache to utilize Ping() (#19719) * update gitea.com/go-chi/cache -> v0.2.0 * ajust to new interface * refactor --- go.mod | 2 +- go.sum | 4 ++-- modules/cache/cache.go | 27 +-------------------------- modules/cache/cache_redis.go | 5 +++++ modules/cache/cache_twoqueue.go | 5 +++++ routers/web/healthcheck/check.go | 2 +- 6 files changed, 15 insertions(+), 30 deletions(-) diff --git a/go.mod b/go.mod index 5ef996769d..23e922ecef 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ require ( code.gitea.io/gitea-vet v0.2.2-0.20220122151748-48ebc902541b code.gitea.io/sdk/gitea v0.15.1 gitea.com/go-chi/binding v0.0.0-20220309004920-114340dabecb - gitea.com/go-chi/cache v0.0.0-20211201020628-dcb774c4ffea + gitea.com/go-chi/cache v0.2.0 gitea.com/go-chi/captcha v0.0.0-20211013065431-70641c1a35d5 gitea.com/go-chi/session v0.0.0-20211218221615-e3605d8b28b8 gitea.com/lunny/levelqueue v0.4.1 diff --git a/go.sum b/go.sum index ae6b0ad83a..215450970f 100644 --- a/go.sum +++ b/go.sum @@ -72,8 +72,8 @@ dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7 gitea.com/go-chi/binding v0.0.0-20220309004920-114340dabecb h1:Yy0Bxzc8R2wxiwXoG/rECGplJUSpXqCsog9PuJFgiHs= gitea.com/go-chi/binding v0.0.0-20220309004920-114340dabecb/go.mod h1:77TZu701zMXWJFvB8gvTbQ92zQ3DQq/H7l5wAEjQRKc= gitea.com/go-chi/cache v0.0.0-20210110083709-82c4c9ce2d5e/go.mod h1:k2V/gPDEtXGjjMGuBJiapffAXTv76H4snSmlJRLUhH0= -gitea.com/go-chi/cache v0.0.0-20211201020628-dcb774c4ffea h1:Fq/f4hjigb5v5WpA5TfBCZOSavpHPBiB4Wkj/WsITYM= -gitea.com/go-chi/cache v0.0.0-20211201020628-dcb774c4ffea/go.mod h1:k2V/gPDEtXGjjMGuBJiapffAXTv76H4snSmlJRLUhH0= +gitea.com/go-chi/cache v0.2.0 h1:E0npuTfDW6CT1yD8NMDVc1SK6IeRjfmRL2zlEsCEd7w= +gitea.com/go-chi/cache v0.2.0/go.mod h1:iQlVK2aKTZ/rE9UcHyz9pQWGvdP9i1eI2spOpzgCrtE= gitea.com/go-chi/captcha v0.0.0-20211013065431-70641c1a35d5 h1:J/1i8u40TbcLP/w2w2KCkgW2PelIqYkD5UOwu8IOvVU= gitea.com/go-chi/captcha v0.0.0-20211013065431-70641c1a35d5/go.mod h1:hQ9SYHKdOX968wJglb/NMQ+UqpOKwW4L+EYdvkWjHSo= gitea.com/go-chi/session v0.0.0-20211218221615-e3605d8b28b8 h1:tJQRXgZigkLeeW9LPlps9G9aMoE6LAmqigLA+wxmd1Q= diff --git a/modules/cache/cache.go b/modules/cache/cache.go index fd32aa153b..21d0cd0a04 100644 --- a/modules/cache/cache.go +++ b/modules/cache/cache.go @@ -5,11 +5,9 @@ package cache import ( - "errors" "fmt" "strconv" - "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" mc "gitea.com/go-chi/cache" @@ -35,7 +33,7 @@ func NewContext() error { if conn, err = newCache(setting.CacheService.Cache); err != nil { return err } - if err = Ping(); err != nil { + if err = conn.Ping(); err != nil { return err } } @@ -43,29 +41,6 @@ func NewContext() error { return err } -// Ping checks if the cache service works or not, it not, it returns an error -func Ping() error { - if conn == nil { - return errors.New("cache not available") - } - var err error - const testKey = "__gitea_cache_test" - const testVal = "test-value" - if err = conn.Put(testKey, testVal, 10); err != nil { - return err - } - val := conn.Get(testKey) - if valStr, ok := val.(string); !ok || valStr != testVal { - // If the cache is full, the Get may not read the expected value stored by Put. - // Since we have checked that Put can success, so we just show a warning here, do not return an error to panic. - log.Warn("cache (adapter:%s, config:%s) doesn't seem to work correctly, set test value '%v' but get '%v'", - setting.CacheService.Cache.Adapter, setting.CacheService.Cache.Conn, - testVal, val, - ) - } - return nil -} - // GetCache returns the currently configured cache func GetCache() mc.Cache { return conn diff --git a/modules/cache/cache_redis.go b/modules/cache/cache_redis.go index ff6c8d424c..7bb71f08ce 100644 --- a/modules/cache/cache_redis.go +++ b/modules/cache/cache_redis.go @@ -153,6 +153,11 @@ func (c *RedisCacher) StartAndGC(opts cache.Options) error { return c.c.Ping(graceful.GetManager().HammerContext()).Err() } +// Ping tests if the cache is alive. +func (c *RedisCacher) Ping() error { + return c.c.Ping(graceful.GetManager().HammerContext()).Err() +} + func init() { cache.Register("redis", &RedisCacher{}) } diff --git a/modules/cache/cache_twoqueue.go b/modules/cache/cache_twoqueue.go index 275b73f068..9c26b011b6 100644 --- a/modules/cache/cache_twoqueue.go +++ b/modules/cache/cache_twoqueue.go @@ -199,6 +199,11 @@ func (c *TwoQueueCache) StartAndGC(opts mc.Options) error { return err } +// Ping tests if the cache is alive. +func (c *TwoQueueCache) Ping() error { + return mc.GenericPing(c) +} + func init() { mc.Register("twoqueue", &TwoQueueCache{}) } diff --git a/routers/web/healthcheck/check.go b/routers/web/healthcheck/check.go index 481f05c0da..57707b6121 100644 --- a/routers/web/healthcheck/check.go +++ b/routers/web/healthcheck/check.go @@ -126,7 +126,7 @@ func checkCache(checks checks) status { } st := componentStatus{} - if err := cache.Ping(); err != nil { + if err := cache.GetCache().Ping(); err != nil { st.Status = fail st.Time = getCheckTime() log.Error("cache ping failed with error: %v", err)