Skip to content

Commit

Permalink
Add throttle option to scanner
Browse files Browse the repository at this point in the history
  • Loading branch information
obukhov committed Jul 25, 2021
1 parent 19cdd1f commit c88ab42
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
12 changes: 10 additions & 2 deletions cmd/inventory.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

var (
output, outputParams, separators, pattern string
maxChildren, scanCount int
maxChildren, scanCount, throttleNs int
)

var scanCmd = &cobra.Command{
Expand All @@ -37,7 +37,14 @@ var scanCmd = &cobra.Command{
)

resultTrie := trie.NewTrie(trie.NewPunctuationSplitter([]rune(separators)...), maxChildren)
redisScanner.Scan(scanner.ScanOptions{ScanCount: scanCount, Pattern: pattern}, resultTrie)
redisScanner.Scan(
scanner.ScanOptions{
ScanCount: scanCount,
Pattern: pattern,
Throttle: throttleNs,
},
resultTrie,
)

r, err := renderer.NewRenderer(output, outputParams)
if err != nil {
Expand All @@ -62,4 +69,5 @@ func init() {
scanCmd.Flags().IntVarP(&maxChildren, "maxChildren", "m", 10, "Maximum children node can have before start aggregating")
scanCmd.Flags().StringVarP(&pattern, "pattern", "k", "*", "Glob pattern limiting the keys to be aggregated")
scanCmd.Flags().IntVarP(&scanCount, "scanCount", "c", 1000, "Number of keys to be scanned in one iteration (argument of scan command)")
scanCmd.Flags().IntVarP(&throttleNs, "throttle", "t", 0, "Throttle: number of nanoseconds to sleep between keys")
}
5 changes: 5 additions & 0 deletions src/scanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/mediocregopher/radix/v4"
"github.com/obukhov/redis-inventory/src/trie"
"github.com/rs/zerolog"
"time"
)

// RedisScanner scans redis keys and puts them in a trie
Expand All @@ -27,6 +28,7 @@ func NewScanner(client radix.Client, scanProgress ProgressWriter, logger zerolog
type ScanOptions struct {
Pattern string
ScanCount int
Throttle int
}

// Scan initiates scanning process
Expand Down Expand Up @@ -64,6 +66,9 @@ func (s *RedisScanner) Scan(options ScanOptions, result *trie.Trie) {

s.logger.Debug().Msgf("Dump %s value: %d", key, res)
s.scanProgress.Increment()
if options.Throttle > 0 {
time.Sleep(time.Nanosecond * time.Duration(options.Throttle))
}
}
s.scanProgress.Stop()
}
Expand Down

0 comments on commit c88ab42

Please sign in to comment.