Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 2 additions & 21 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,6 @@ jobs:
go-version: stable
- uses: actions/checkout@v4
- name: golangci-lint
uses: golangci/golangci-lint-action@v8.0.0
uses: golangci/golangci-lint-action@v9.2.0
with:
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
version: v2.5.0

# Optional: working directory, useful for monorepos
# working-directory: somedir

# Optional: golangci-lint command line arguments.
# args: --issues-exit-code=0

# Optional: show only new issues if it's a pull request. The default value is `false`.
# only-new-issues: true

# Optional: if set to true then the action will use pre-installed Go.
# skip-go-installation: true

# Optional: if set to true then the action don't cache or restore ~/go/pkg.
# skip-pkg-cache: true

# Optional: if set to true then the action don't cache or restore ~/.cache/go-build.
# skip-build-cache: true
version: v2.11.3
24 changes: 10 additions & 14 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@ run:
linters:
default: all
disable:
- funlen
- gocyclo
- gocognit
- cyclop
- err113
- errcheck
- gocognit
- gocyclo
- funlen
- containedctx
- nestif
- noctx
- embeddedstructfieldcheck
- testpackage
- nilnil
- noinlineerr
- wsl_v5
- funcorder
Expand All @@ -36,22 +41,13 @@ linters:
- varnamelen
- wrapcheck
settings:
funlen:
lines: 150
statements: 50
cyclop:
max-complexity: 35
gocognit:
min-complexity: 50
nestif:
min-complexity: 12
dupl:
threshold: 100
errcheck:
check-type-assertions: true
check-blank: true
gocyclo:
min-complexity: 25
min-complexity: 20
misspell:
locale: US
unparam:
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#GOLANGCI_LINT_VERSION := "v2.5.0" # Optional configuration to pinpoint golangci-lint version.
#GOLANGCI_LINT_VERSION := "v2.11.3" # Optional configuration to pinpoint golangci-lint version.

# The head of Makefile determines location of dev-go to include standard targets.
GO ?= go
Expand Down
21 changes: 21 additions & 0 deletions cmd/catp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ catp [OPTIONS] PATH ...
-end-line int
stop printing lines at this line (exclusive),
default is 0 (no limit), each input file is counted separately
-http string
address to serve rate limit control interface on (e.g. :6060),
/ shows a status page, GET /rate-limit reads the current limit,
POST /rate-limit?value=X sets a new one (0 or less means unlimited)
-http-auth string
user:pass to require as HTTP basic auth for -http endpoints
-l count lines
-no-progress
disable progress printing
Expand Down Expand Up @@ -135,6 +141,21 @@ Each source line would follow the filtering pipeline:
skipped
* if not, source line gets into output because of `-pass-any`

### Control rate limit at runtime over HTTP

Start unlimited (or with an initial `-rate-limit`), then throttle or resume from another terminal.

```
catp -http :6060 -http-auth admin:secret -rate-limit 1000 -output out.log input.log
```

```
curl -u admin:secret http://localhost:6060/ # status page
curl -u admin:secret http://localhost:6060/rate-limit # read current limit
curl -u admin:secret -X POST "http://localhost:6060/rate-limit?value=100" # throttle down
curl -u admin:secret -X POST "http://localhost:6060/rate-limit?value=0" # unlimited
```

### Split matches into separate files

```
Expand Down
40 changes: 27 additions & 13 deletions cmd/catp/catp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import (
"bufio"
"bytes"
"context"
"errors"
"flag"
"fmt"
Expand All @@ -12,6 +13,7 @@
"os/signal"
"path/filepath"
"runtime/pprof"
"slices"
"sort"
"strings"
"sync/atomic"
Expand All @@ -20,6 +22,7 @@

"github.com/bool64/dev/version"
"github.com/bool64/progress"
"golang.org/x/time/rate"
)

// Main is the entry point for catp CLI tool.
Expand Down Expand Up @@ -90,6 +93,10 @@
flag.BoolVar(&r.noProgress, "no-progress", false, "disable progress printing")
flag.BoolVar(&r.countLines, "l", false, "count lines")
flag.Float64Var(&r.rateLimit, "rate-limit", 0, "output rate limit lines per second")
flag.StringVar(&r.httpAddr, "http", "", "address to serve rate limit control interface on (e.g. :6060),\n"+
"/ shows a status page, GET /rate-limit reads the current limit,\n"+
"POST /rate-limit?value=X sets a new one (0 or less means unlimited)")
flag.StringVar(&r.httpAuth, "http-auth", "", "user:pass to require as HTTP basic auth for -http endpoints")
progressJSON := flag.String("progress-json", "", "write current progress to a file")
ver := flag.Bool("version", false, "print version and exit")

Expand Down Expand Up @@ -161,17 +168,7 @@
}

for _, f := range glob {
alreadyThere := false

for _, e := range files {
if e == f {
alreadyThere = true

break
}
}

if !alreadyThere {
if !slices.Contains(files, f) {
files = append(files, f)
}
}
Expand Down Expand Up @@ -235,6 +232,24 @@
r.sizes[fn] = st.Size()
}

if r.rateLimit > 0 || r.httpAddr != "" {
lim := rate.Inf
if r.rateLimit > 0 {
lim = rate.Limit(r.rateLimit)
}

Check notice on line 239 in cmd/catp/catp/app.go

View workflow job for this annotation

GitHub Actions / test (stable)

3 statement(s) on lines 235:239 are not covered by tests.

r.limiter = rate.NewLimiter(lim, 100)

Check notice on line 241 in cmd/catp/catp/app.go

View workflow job for this annotation

GitHub Actions / test (stable)

1 statement(s) are not covered by tests.
}

if r.httpAddr != "" {
if err := r.startControlServer(); err != nil {
return err
}

Check notice on line 247 in cmd/catp/catp/app.go

View workflow job for this annotation

GitHub Actions / test (stable)

2 statement(s) on lines 244:247 are not covered by tests.
}

ctx, cancel := context.WithCancel(context.Background())
r.ctx = ctx

shutdown := make(chan os.Signal, 1)
signal.Notify(shutdown, syscall.SIGINT, syscall.SIGTERM)

Expand All @@ -243,6 +258,7 @@

println("received signal, shutting down...")
atomic.StoreInt64(&r.closed, 1)
cancel()
}()

if r.parallel >= 2 {
Expand All @@ -259,8 +275,6 @@
errs := make(chan error, r.parallel)

for _, fn := range files {
fn := fn

select {
case err := <-errs:
return err
Expand Down
Loading
Loading