From d1af9e61b5483d23877acb7a1976a6b7a1584837 Mon Sep 17 00:00:00 2001 From: "renovate[bot]" <29139614+renovate[bot]@users.noreply.github.com> Date: Mon, 14 Oct 2024 07:08:21 +0000 Subject: [PATCH] Update github.com/grafana/gomemcache digest to fdaf6a9 --- go.mod | 2 +- go.sum | 4 +- .../grafana/gomemcache/memcache/memcache.go | 65 ++++++++++++++++++- vendor/modules.txt | 4 +- 4 files changed, 69 insertions(+), 6 deletions(-) diff --git a/go.mod b/go.mod index 0ff5100a661..1e72d163288 100644 --- a/go.mod +++ b/go.mod @@ -205,7 +205,7 @@ require ( github.com/googleapis/enterprise-certificate-proxy v0.3.4 // indirect github.com/googleapis/gax-go/v2 v2.13.0 // indirect github.com/gosimple/slug v1.1.1 // indirect - github.com/grafana/gomemcache v0.0.0-20240229205252-cd6a66d6fb56 + github.com/grafana/gomemcache v0.0.0-20240805133030-fdaf6a95408e github.com/hashicorp/consul/api v1.29.4 // indirect github.com/hashicorp/errwrap v1.1.0 // indirect github.com/hashicorp/go-cleanhttp v0.5.2 // indirect diff --git a/go.sum b/go.sum index 67aaa10bb4f..95e48cb94c8 100644 --- a/go.sum +++ b/go.sum @@ -1264,8 +1264,8 @@ github.com/grafana/franz-go v0.0.0-20241009101240-fa97d35e871f h1:nsrRsQHfpqs6dW github.com/grafana/franz-go v0.0.0-20241009101240-fa97d35e871f/go.mod h1:NreRdJ2F7dziDY/m6VyspWd6sNxHKXdMZI42UfQ3GXM= github.com/grafana/goautoneg v0.0.0-20240607115440-f335c04c58ce h1:WI1olbgS+sEl77qxEYbmt9TgRUz7iLqmjh8lYPpGlKQ= github.com/grafana/goautoneg v0.0.0-20240607115440-f335c04c58ce/go.mod h1:GFAN9Jn9t1cX7sNfc6ZoFyc4f7i8jtm3SajrWdZM2EE= -github.com/grafana/gomemcache v0.0.0-20240229205252-cd6a66d6fb56 h1:X8IKQ0wu40wpvYcKfBcc5T4QnhdQjUhtUtB/1CY89lE= -github.com/grafana/gomemcache v0.0.0-20240229205252-cd6a66d6fb56/go.mod h1:PGk3RjYHpxMM8HFPhKKo+vve3DdlPUELZLSDEFehPuU= +github.com/grafana/gomemcache v0.0.0-20240805133030-fdaf6a95408e h1:UlEET0InuoFautfaFp8lDrNF7rPHYXuBMrzwWx9XqFY= +github.com/grafana/gomemcache v0.0.0-20240805133030-fdaf6a95408e/go.mod h1:IGRj8oOoxwJbHBYl1+OhS9UjQR0dv6SQOep7HqmtyFU= github.com/grafana/memberlist v0.3.1-0.20220714140823-09ffed8adbbe h1:yIXAAbLswn7VNWBIvM71O2QsgfgW9fRXZNR0DXe6pDU= github.com/grafana/memberlist v0.3.1-0.20220714140823-09ffed8adbbe/go.mod h1:MS2lj3INKhZjWNqd3N0m3J+Jxf3DAOnAH9VT3Sh9MUE= github.com/grafana/mimir-prometheus v0.0.0-20241003114040-35ec40c3b27a h1:Thi1mhNqRonfMLW6uQ9vVK+nmEfv0SDUGZWxhyWmO0Y= diff --git a/vendor/github.com/grafana/gomemcache/memcache/memcache.go b/vendor/github.com/grafana/gomemcache/memcache/memcache.go index c627cbdf983..28f81ac1128 100644 --- a/vendor/github.com/grafana/gomemcache/memcache/memcache.go +++ b/vendor/github.com/grafana/gomemcache/memcache/memcache.go @@ -20,6 +20,7 @@ package memcache import ( "bufio" "bytes" + "context" "errors" "fmt" "io" @@ -79,6 +80,10 @@ const ( // idle connection to consider it "recently used". The default value has been // set equal to the default TCP TIME_WAIT timeout in linux. defaultRecentlyUsedConnsThreshold = 2 * time.Minute + + // defaultReconnectCronTimeout is how often the client will attempt to reestablish + // connection to the server when initialized with NewWithBackgroundReconnect + defaultReconnectCronTimeout = 10 * time.Minute ) const buffered = 8 // arbitrary buffered channel size, for readability @@ -130,7 +135,35 @@ var ( func New(server ...string) *Client { ss := new(ServerList) _ = ss.SetServers(server...) - return NewFromSelector(ss) + c := NewFromSelector(ss) + c.serverList = append(c.serverList, server...) + + return c +} + +// NewWithBackgroundReconnect returns the same thing as New(server ...string), but takes a context and timeout to initiate a bakground reconnect cron +func NewWithBackgroundReconnect(ctx context.Context, interval time.Duration, server ...string) *Client { + c := New(server...) + + if interval == 0 { + interval = defaultReconnectCronTimeout + } + ticker := time.NewTicker(interval) + + // periodically ping the provided servers -- if there are timeouts, it will trigger a reconnect attempt + go func() { + for { + select { + case <-ctx.Done(): + ticker.Stop() + return + case <-ticker.C: + _ = c.Ping() + } + } + }() + + return c } // NewFromSelector returns a new Client using the provided ServerSelector. @@ -199,6 +232,9 @@ type Client struct { lk sync.Mutex freeconn map[string][]*conn + + serverList []string + reconnectLock sync.Mutex } // Item is an item to be got or stored in a memcached server. @@ -403,6 +439,9 @@ func (c *Client) dial(addr net.Addr) (net.Conn, error) { } if ne, ok := err.(net.Error); ok && ne.Timeout() { + // In the case of a timeout, we might need to reestablish a connection to one or more servers, for example in case of an IP change + // Fire this off in a goroutine so it doesn't delay a response back to the client + c.backgroundReconnect() return nil, &ConnectTimeoutError{addr} } @@ -420,6 +459,7 @@ func (c *Client) getConn(addr net.Addr) (*conn, error) { cn.extendDeadline() return cn, nil } + nc, err := c.dial(addr) if err != nil { return nil, err @@ -919,3 +959,26 @@ func (c *Client) incrDecr(verb, key string, delta uint64) (uint64, error) { }) return val, err } + +// backgroundReconnect makes an asyncronous attempt to reconnect to the provided server list and reset all open connections +func (c *Client) backgroundReconnect() { + go func() { + c.reconnectLock.Lock() + defer c.reconnectLock.Unlock() + + if sl, ok := c.selector.(*ServerList); ok { + if err := sl.SetServers(c.serverList...); err == nil { + c.lk.Lock() + defer c.lk.Unlock() + + // close all open connections before deleting them + for _, cs := range c.freeconn { + for _, conn := range cs { + conn.nc.Close() + } + } + c.freeconn = make(map[string][]*conn) + } + } + }() +} diff --git a/vendor/modules.txt b/vendor/modules.txt index c295f412ba8..e23a79d4b1b 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -666,8 +666,8 @@ github.com/grafana/e2e github.com/grafana/e2e/cache github.com/grafana/e2e/db github.com/grafana/e2e/images -# github.com/grafana/gomemcache v0.0.0-20240229205252-cd6a66d6fb56 -## explicit; go 1.18 +# github.com/grafana/gomemcache v0.0.0-20240805133030-fdaf6a95408e +## explicit; go 1.22 github.com/grafana/gomemcache/memcache # github.com/grafana/pyroscope-go/godeltaprof v0.1.8 ## explicit; go 1.18