Skip to content

Commit e684a3d

Browse files
authored
Merge pull request #46 from gatewayd-io/replace-keys-with-scan
Replace keys with scan
2 parents 3c880b6 + 72e2e51 commit e684a3d

File tree

2 files changed

+43
-27
lines changed

2 files changed

+43
-27
lines changed

plugin/plugin.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,16 +268,18 @@ func (p *Plugin) invalidateDML(ctx context.Context, query string) {
268268
// TODO: This is not efficient. We should be able to invalidate the cache
269269
// for a specific key instead of invalidating the entire table.
270270
pipeline := p.RedisClient.Pipeline()
271+
var cursor uint64
271272
for {
272-
scanResult := p.RedisClient.Scan(ctx, 0, table+":*", p.ScanCount)
273+
scanResult := p.RedisClient.Scan(ctx, cursor, table+":*", p.ScanCount)
273274
if scanResult.Err() != nil {
274275
CacheMissesCounter.Inc()
275276
p.Logger.Debug("Failed to scan keys", "error", scanResult.Err())
276277
break
277278
}
278279

279280
// Per each key, delete the cache entry and the table cache key itself.
280-
keys, cursor := scanResult.Val()
281+
var keys []string
282+
keys, cursor = scanResult.Val()
281283
for _, tableKey := range keys {
282284
// Invalidate the cache for the table.
283285
cachedRespnseKey := strings.TrimPrefix(tableKey, table+":")

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)