mirror of
https://github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin.git
synced 2026-09-02 20:28:50 +02:00
cache: keep redis readers by pointer
A pooled SimpleRedis holds a sync.Mutex, so appending one into rc.readers by value copies the lock and trips go vet's copylocks check. Keep the readers by pointer instead; the round-robin over replicas is unchanged. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01D95Nh68xKXhynPXHzozrXp
This commit is contained in:
Vendored
+5
-3
@@ -52,7 +52,7 @@ func (localCache) delete(key string) {
|
|||||||
type redisCache struct {
|
type redisCache struct {
|
||||||
log *slog.Logger
|
log *slog.Logger
|
||||||
writer simpleredis.SimpleRedis
|
writer simpleredis.SimpleRedis
|
||||||
readers []simpleredis.SimpleRedis
|
readers []*simpleredis.SimpleRedis
|
||||||
counter atomic.Uint64
|
counter atomic.Uint64
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -62,7 +62,7 @@ func (rc *redisCache) nextReader() *simpleredis.SimpleRedis {
|
|||||||
return &rc.writer
|
return &rc.writer
|
||||||
}
|
}
|
||||||
idx := rc.counter.Add(1) % uint64(n)
|
idx := rc.counter.Add(1) % uint64(n)
|
||||||
return &rc.readers[idx]
|
return rc.readers[idx]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rc *redisCache) get(key string) (string, error) {
|
func (rc *redisCache) get(key string) (string, error) {
|
||||||
@@ -115,7 +115,9 @@ func (c *Client) New(log *slog.Logger, isRedis bool, writeHost string, readHosts
|
|||||||
rc := &redisCache{log: log}
|
rc := &redisCache{log: log}
|
||||||
rc.writer.Init(writeHost, pass, database)
|
rc.writer.Init(writeHost, pass, database)
|
||||||
for _, h := range readHosts {
|
for _, h := range readHosts {
|
||||||
var r simpleredis.SimpleRedis
|
// A pooled SimpleRedis holds a mutex, so it is kept by pointer:
|
||||||
|
// appending it by value would copy the lock along with it.
|
||||||
|
r := &simpleredis.SimpleRedis{}
|
||||||
r.Init(h, pass, database)
|
r.Init(h, pass, database)
|
||||||
rc.readers = append(rc.readers, r)
|
rc.readers = append(rc.readers, r)
|
||||||
}
|
}
|
||||||
|
|||||||
Vendored
+5
-2
@@ -130,7 +130,7 @@ func indexOfReader(rc *redisCache, r *simpleredis.SimpleRedis) int {
|
|||||||
return -1
|
return -1
|
||||||
}
|
}
|
||||||
for i := range rc.readers {
|
for i := range rc.readers {
|
||||||
if r == &rc.readers[i] {
|
if r == rc.readers[i] {
|
||||||
return i
|
return i
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -151,7 +151,10 @@ func Test_nextReader(t *testing.T) {
|
|||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
rc := &redisCache{log: logger.New("INFO", "")}
|
rc := &redisCache{log: logger.New("INFO", "")}
|
||||||
rc.readers = make([]simpleredis.SimpleRedis, tt.readers)
|
rc.readers = make([]*simpleredis.SimpleRedis, tt.readers)
|
||||||
|
for i := range rc.readers {
|
||||||
|
rc.readers[i] = &simpleredis.SimpleRedis{}
|
||||||
|
}
|
||||||
for call, want := range tt.want {
|
for call, want := range tt.want {
|
||||||
if got := indexOfReader(rc, rc.nextReader()); got != want {
|
if got := indexOfReader(rc, rc.nextReader()); got != want {
|
||||||
t.Errorf("call %d: nextReader() -> reader[%d], want reader[%d]", call, got, want)
|
t.Errorf("call %d: nextReader() -> reader[%d], want reader[%d]", call, got, want)
|
||||||
|
|||||||
Reference in New Issue
Block a user