Skip to content

Commit 02e2364

Browse files
committed
Add automaxprocs tuning and goleak leak checks in CLI packages
1 parent ff3e028 commit 02e2364

6 files changed

Lines changed: 49 additions & 1 deletion

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ If you set `DATAFOG_API_TOKEN`, send it on every request using:
9898
| `DATAFOG_READ_HEADER_TIMEOUT` | `2s` | Request-header parse timeout |
9999
| `DATAFOG_IDLE_TIMEOUT` | `30s` | Idle keep-alive timeout |
100100
| `DATAFOG_SHUTDOWN_TIMEOUT` | `10s` | Graceful shutdown timeout |
101+
| `GOMAXPROCS` | *(runtime default)* | Auto-tuned at startup to detected CPU limit; set explicitly to override |
101102
| `DATAFOG_PPROF_ADDR` | *(unset)* | If set, starts optional profiling server on this address (example `localhost:6060`) |
102103
| `DATAFOG_FGPROF` | `false` | Add `/debug/fgprof` endpoint to the profiling server |
103104
| `DATAFOG_ENABLE_DEMO` | *(unset)* | Enable `/demo*` endpoints |

cmd/datafog-api/main.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"time"
1515

1616
"github.com/felixge/fgprof"
17+
"go.uber.org/automaxprocs/maxprocs"
1718

1819
"github.com/datafog/datafog-api/internal/policy"
1920
"github.com/datafog/datafog-api/internal/receipts"
@@ -22,6 +23,9 @@ import (
2223
)
2324

2425
func main() {
26+
revertAuto := configureMaxProcs(log.Default())
27+
defer revertAuto()
28+
2529
policyPath := getenv("DATAFOG_POLICY_PATH", "config/policy.json")
2630
receiptPath := getenv("DATAFOG_RECEIPT_PATH", "datafog_receipts.jsonl")
2731
apiToken := getenv("DATAFOG_API_TOKEN", "")
@@ -177,6 +181,20 @@ func getenvBool(key string, fallback bool) bool {
177181
return fallback
178182
}
179183

184+
func configureMaxProcs(logger *log.Logger) func() {
185+
if logger == nil {
186+
logger = log.Default()
187+
}
188+
undo, err := maxprocs.Set(maxprocs.Logger(func(format string, args ...interface{}) {
189+
logger.Printf(format, args...)
190+
}))
191+
if err != nil {
192+
logger.Printf("maxprocs configuration skipped: %v", err)
193+
return func() {}
194+
}
195+
return undo
196+
}
197+
180198
func startProfilingServer(addr string, enableFGProf bool, logger *log.Logger) *http.Server {
181199
mux := http.NewServeMux()
182200
mux.Handle("/debug/pprof/", http.DefaultServeMux)

cmd/datafog-api/main_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,22 @@ package main
33
import (
44
"testing"
55
"time"
6+
7+
"go.uber.org/goleak"
68
)
79

10+
func TestMain(m *testing.M) {
11+
goleak.VerifyTestMain(m)
12+
}
13+
14+
func TestConfigureMaxProcs(t *testing.T) {
15+
revert := configureMaxProcs(nil)
16+
if revert == nil {
17+
t.Fatalf("expected configureMaxProcs to return a revert function")
18+
}
19+
revert()
20+
}
21+
822
func TestGetenvDuration(t *testing.T) {
923
t.Run("fallback_when_missing", func(t *testing.T) {
1024
t.Setenv("DATAFOG_READ_TIMEOUT", "")

cmd/datafog-shim/main_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,13 @@ import (
88
"testing"
99

1010
"github.com/datafog/datafog-api/internal/shim"
11+
"go.uber.org/goleak"
1112
)
1213

14+
func TestMain(m *testing.M) {
15+
goleak.VerifyTestMain(m)
16+
}
17+
1318
func TestParseMode(t *testing.T) {
1419
t.Run("default", func(t *testing.T) {
1520
mode, err := parseMode("")

go.mod

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ module github.com/datafog/datafog-api
22

33
go 1.22
44

5-
require github.com/felixge/fgprof v0.9.5
5+
require (
6+
github.com/felixge/fgprof v0.9.5
7+
go.uber.org/automaxprocs v1.6.0
8+
go.uber.org/goleak v1.3.0
9+
)
610

711
require github.com/google/pprof v0.0.0-20240227163752-401108e1b7e7 // indirect

go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,17 @@ github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJ
2121
github.com/orisano/pixelmatch v0.0.0-20220722002657-fb0b55479cde/go.mod h1:nZgzbfBr3hhjoZnS66nKrHmduYNpc34ny7RK4z5/HM0=
2222
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
2323
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
24+
github.com/prashantv/gostub v1.1.0 h1:BTyx3RfQjRHnUWaGF9oQos79AlQ5k8WNktv7VGvVH4g=
25+
github.com/prashantv/gostub v1.1.0/go.mod h1:A5zLQHz7ieHGG7is6LLXLz7I8+3LZzsrV0P1IAHhP5U=
2426
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
2527
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
2628
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
2729
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
2830
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
31+
go.uber.org/automaxprocs v1.6.0 h1:O3y2/QNTOdbF+e/dpXNNW7Rx2hZ4sTIPyybbxyNqTUs=
32+
go.uber.org/automaxprocs v1.6.0/go.mod h1:ifeIMSnPZuznNm6jmdzmU3/bfk01Fe2fotchwEFJ8r8=
33+
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
34+
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
2935
golang.org/x/sys v0.0.0-20220310020820-b874c991c1a5/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
3036
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
3137
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

0 commit comments

Comments
 (0)