aboutsummaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
authorRasmus Dahlberg <rasmus@rgdd.se>2023-03-30 16:28:19 +0200
committerRasmus Dahlberg <rasmus@rgdd.se>2023-03-30 16:28:19 +0200
commit4a36fbdc9972c5b6101d6f3bc9bd0a77c8c83fd4 (patch)
tree6efecb8d7417b25016571d7401446bd76a0152ae /main.go
parentd44f024386316a364ddf9bc17762883cea3ddfc0 (diff)
Add input file read rate-limit
Diffstat (limited to 'main.go')
-rw-r--r--main.go19
1 files changed, 18 insertions, 1 deletions
diff --git a/main.go b/main.go
index 55e7989..072924a 100644
--- a/main.go
+++ b/main.go
@@ -217,6 +217,7 @@ func workGenerator(ctx context.Context, opts options.Options, fp *os.File, quest
buf := make([]byte, 0, opts.MaxFileBuffer*1024*1024)
scanner.Buffer(buf, opts.MaxFileBuffer*1024*1024)
+ // roll-up to the requested start line
nextLine := int64(0)
if opts.StartLineInclusive > nextLine {
for scanner.Scan() {
@@ -233,17 +234,32 @@ func workGenerator(ctx context.Context, opts options.Options, fp *os.File, quest
}
}
+ // initialize ticker
ticker := time.NewTicker(opts.MetricsInterval)
defer ticker.Stop()
-
startTime := time.Now().Unix()
latestTime := startTime
latestCount := opts.StartLineInclusive
+
+ // initialize rate-limit
+ limit := time.NewTicker(time.Second)
+ defer limit.Stop()
+ readCount := 0
+
for scanner.Scan() {
if opts.EndLineExclusive > 0 && nextLine == opts.EndLineExclusive {
break
}
+ if readCount == opts.Limit {
+ select {
+ case <-ctx.Done():
+ return nextLine, false
+ case <-limit.C:
+ readCount = 0
+ }
+ }
+
select {
case <-ctx.Done():
return nextLine, false
@@ -259,6 +275,7 @@ func workGenerator(ctx context.Context, opts options.Options, fp *os.File, quest
latestTime = now
case questionCh <- qna.Question{Domain: scanner.Text()}:
nextLine++
+ readCount++
}
}