Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support singleflight to memcached and redis client set #7172

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions pkg/cacheutil/memcached_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/bradfitz/gomemcache/memcache"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/golang/groupcache/singleflight"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
Expand Down Expand Up @@ -206,6 +207,8 @@ type memcachedClient struct {
p *AsyncOperationProcessor

setAsyncCircuitBreaker CircuitBreaker

g singleflight.Group
}

// AddressProvider performs node address resolution given a list of clusters.
Expand Down Expand Up @@ -388,11 +391,14 @@ func (c *memcachedClient) SetAsync(key string, value []byte, ttl time.Duration)
c.operations.WithLabelValues(opSet).Inc()

err := c.setAsyncCircuitBreaker.Execute(func() error {
return c.client.Set(&memcache.Item{
Key: key,
Value: value,
Expiration: int32(time.Now().Add(ttl).Unix()),
_, err := c.g.Do(key, func() (interface{}, error) {
return nil, c.client.Set(&memcache.Item{
Key: key,
Value: value,
Expiration: int32(time.Now().Add(ttl).Unix()),
})
})
return err
})
if err != nil {
if errors.Is(err, gobreaker.ErrOpenState) || errors.Is(err, gobreaker.ErrTooManyRequests) {
Expand Down
8 changes: 7 additions & 1 deletion pkg/cacheutil/redis_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/golang/groupcache/singleflight"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
Expand Down Expand Up @@ -160,6 +161,8 @@ type RedisClient struct {
p *AsyncOperationProcessor

setAsyncCircuitBreaker CircuitBreaker

g singleflight.Group
}

// NewRedisClient makes a new RedisClient.
Expand Down Expand Up @@ -262,7 +265,10 @@ func (c *RedisClient) SetAsync(key string, value []byte, ttl time.Duration) erro
return c.p.EnqueueAsync(func() {
start := time.Now()
err := c.setAsyncCircuitBreaker.Execute(func() error {
return c.client.Do(context.Background(), c.client.B().Set().Key(key).Value(rueidis.BinaryString(value)).ExSeconds(int64(ttl.Seconds())).Build()).Error()
_, err := c.g.Do(key, func() (interface{}, error) {
return nil, c.client.Do(context.Background(), c.client.B().Set().Key(key).Value(rueidis.BinaryString(value)).ExSeconds(int64(ttl.Seconds())).Build()).Error()
})
return err
})
if err != nil {
level.Warn(c.logger).Log("msg", "failed to set item into redis", "err", err, "key", key, "value_size", len(value))
Expand Down
Loading