Skip to content

Commit 72e2e51

Browse files
committed
Refactor and replace KEYS with SCAN
1 parent c00f16f commit 72e2e51

File tree

1 file changed

+39
-25
lines changed

1 file changed

+39
-25
lines changed

plugin/scheduler.go

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,43 +21,57 @@ func (p *Plugin) PeriodicInvalidator() {
2121
p.Logger.Trace("Got proxies from GatewayD", "proxies", proxies)
2222

2323
// Get all the client keys and delete the ones that are not valid.
24-
// TODO: use scan instead of keys.
25-
for _, address := range p.RedisClient.Keys(context.Background(), "*:*").Val() {
26-
valid := false
27-
28-
// Validate the address if the address is an IP address.
29-
if ok, err := validateAddressPort(address); ok && err == nil {
30-
valid = true
31-
} else {
32-
p.Logger.Trace(
33-
"Skipping connection because it is invalid", "address", address, "error", err)
24+
var cursor uint64
25+
for {
26+
scanResult := p.RedisClient.Scan(context.Background(), cursor, "*:*", p.ScanCount)
27+
if scanResult.Err() != nil {
28+
p.Logger.Error("Failed to scan keys", "error", scanResult.Err())
29+
break
3430
}
3531

36-
if !valid {
37-
// Validate the address if the address is a hostname.
38-
if ok, err := validateHostPort(address); ok && err == nil {
32+
var addresses []string
33+
addresses, cursor = scanResult.Val()
34+
for _, address := range addresses {
35+
valid := false
36+
37+
// Validate the address if the address is an IP address.
38+
if ok, err := validateAddressPort(address); ok && err == nil {
3939
valid = true
4040
} else {
4141
p.Logger.Trace(
4242
"Skipping connection because it is invalid", "address", address, "error", err)
43+
}
44+
45+
if !valid {
46+
// Validate the address if the address is a hostname.
47+
if ok, err := validateHostPort(address); ok && err == nil {
48+
valid = true
49+
} else {
50+
p.Logger.Trace(
51+
"Skipping connection because it is invalid", "address", address, "error", err)
52+
continue
53+
}
54+
}
55+
56+
// If the address is not valid, skip it.
57+
if !valid {
4358
continue
4459
}
45-
}
4660

47-
// If the address is not valid, skip it.
48-
if !valid {
49-
continue
50-
}
61+
// If the connection is busy (a client is connected), it is not safe to delete the key.
62+
if isBusy(proxies, address) {
63+
p.Logger.Trace("Skipping connection because it is busy", "address", address)
64+
continue
65+
}
5166

52-
// If the connection is busy (a client is connected), it is not safe to delete the key.
53-
if isBusy(proxies, address) {
54-
p.Logger.Trace("Skipping connection because it is busy", "address", address)
55-
continue
67+
p.RedisClient.Del(context.Background(), address)
68+
p.Logger.Trace("Deleted stale address", "address", address)
69+
CacheDeletesCounter.Inc()
5670
}
5771

58-
p.RedisClient.Del(context.Background(), address)
59-
p.Logger.Trace("Deleted stale address", "address", address)
60-
CacheDeletesCounter.Inc()
72+
if cursor == 0 {
73+
break
74+
}
6175
}
6276
}); err != nil {
6377
p.Logger.Error("Failed to start periodic invalidator",

0 commit comments

Comments
 (0)