Skip to content

Commit

Permalink
address potential memory issues in Redis readAll function
Browse files Browse the repository at this point in the history
  • Loading branch information
Radu Ifrim committed Jul 5, 2023
1 parent d3624cd commit 9c049f6
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions pkg/storage/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,6 @@ func (b *RedisDB) ReadAll(ctx context.Context, namespace string) (map[string][]b
return readAll(ctx, keys, b)
}

// TODO: This potentially could dangerous as it might run out of memory as we populate result
func readAll(ctx context.Context, keys []string, b *RedisDB) (map[string][]byte, error) {
result := make(map[string][]byte, len(keys))

Expand All @@ -272,10 +271,21 @@ func readAll(ctx context.Context, keys []string, b *RedisDB) (map[string][]byte,

// result needs to take the namespace out of the key
namespaceDashIndex := strings.Index(keys[0], NamespaceKeySeparator)
for i, val := range values {
byteValue := []byte(fmt.Sprintf("%v", val))
key := keys[i][namespaceDashIndex+1:]
result[key] = byteValue

// Create a channel to process the keys and values in a separate goroutine
ch := make(chan [2][]byte)
go func() {
defer close(ch)
for i, val := range values {
byteValue := []byte(fmt.Sprintf("%v", val))
key := keys[i][namespaceDashIndex+1:]
ch <- [2][]byte{[]byte(key), byteValue}
}
}()

// Range over the channel to process each key-value pair individually
for pair := range ch {
result[string(pair[0])] = pair[1]
}

return result, nil
Expand Down

0 comments on commit 9c049f6

Please sign in to comment.