Skip to content

Commit f7f709b

Browse files
committed
feat!: simplify v1 API and harden cgroup detection
1 parent 6d12049 commit f7f709b

42 files changed

Lines changed: 2521 additions & 1104 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,16 @@ See more details about `GOMEMLIMIT` [here](https://tip.golang.org/doc/gc-guide#M
1010

1111
## Notice
1212

13-
Version `v0.5.0` introduces a fallback to system memory limits as an experimental feature when cgroup limits are unavailable. Activate this by setting `AUTOMEMLIMIT_EXPERIMENT=system`.
14-
You can also use system memory limits via `memlimit.FromSystem` provider directly.
13+
Version `v1.0.0` simplifies the public API and improves cgroup memory limit detection.
1514

16-
This feature is under evaluation and might become a default or be removed based on user feedback.
17-
If you have any feedback about this feature, please open an issue.
15+
Key changes from v0.x:
16+
- `SetGoMemLimitWithOpts`, `SetGoMemLimit`, `SetGoMemLimitWithProvider`, `SetGoMemLimitWithEnv` removed. Use `Set` instead
17+
- `FromCgroupV1`, `FromCgroupV2`, `FromCgroupHybrid` removed. Use `FromCgroup` instead
18+
- `WithEnv`, `AUTOMEMLIMIT_EXPERIMENT`, and `AUTOMEMLIMIT_DEBUG` removed
19+
- `import _ "github.com/KimMachineGun/automemlimit"` now enables system memory fallback by default
20+
- `memlimit.Set` uses cgroup memory by default. Configure `FromSystem` as a fallback to match the blank import behavior
21+
- `WithMin` added to set a minimum `GOMEMLIMIT`
22+
- `WithRefreshInterval` now takes a `context.Context` to control the refresh goroutine
1823

1924
## Installation
2025

@@ -27,41 +32,33 @@ go get github.com/KimMachineGun/automemlimit@latest
2732
```go
2833
package main
2934

30-
// By default, it sets `GOMEMLIMIT` to 90% of cgroup's memory limit.
31-
// This is equivalent to `memlimit.SetGoMemLimitWithOpts(memlimit.WithLogger(slog.Default()))`
32-
// To disable logging, use `memlimit.SetGoMemLimitWithOpts` directly.
35+
// Sets GOMEMLIMIT automatically with system memory fallback enabled.
3336
import _ "github.com/KimMachineGun/automemlimit"
3437
```
3538

36-
or
39+
For more control, use `memlimit.Set()` directly:
3740

3841
```go
3942
package main
4043

41-
import "github.com/KimMachineGun/automemlimit/memlimit"
44+
import (
45+
"log"
4246

43-
func init() {
44-
memlimit.SetGoMemLimitWithOpts(
45-
memlimit.WithRatio(0.9),
46-
memlimit.WithProvider(memlimit.FromCgroup),
47-
memlimit.WithLogger(slog.Default()),
48-
memlimit.WithRefreshInterval(1*time.Minute),
49-
)
50-
memlimit.SetGoMemLimitWithOpts(
47+
"github.com/KimMachineGun/automemlimit/memlimit"
48+
)
49+
50+
func main() {
51+
_, err := memlimit.Set(
5152
memlimit.WithRatio(0.9),
5253
memlimit.WithProvider(
5354
memlimit.ApplyFallback(
5455
memlimit.FromCgroup,
5556
memlimit.FromSystem,
5657
),
5758
),
58-
memlimit.WithRefreshInterval(1*time.Minute),
5959
)
60-
memlimit.SetGoMemLimit(0.9)
61-
memlimit.SetGoMemLimitWithProvider(memlimit.Limit(1024*1024), 0.9)
62-
memlimit.SetGoMemLimitWithProvider(memlimit.FromCgroup, 0.9)
63-
memlimit.SetGoMemLimitWithProvider(memlimit.FromCgroupV1, 0.9)
64-
memlimit.SetGoMemLimitWithProvider(memlimit.FromCgroupHybrid, 0.9)
65-
memlimit.SetGoMemLimitWithProvider(memlimit.FromCgroupV2, 0.9)
60+
if err != nil {
61+
log.Fatal(err)
62+
}
6663
}
6764
```

automemlimit.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
// Package automemlimit provides automatic GOMEMLIMIT configuration.
2+
//
3+
// Importing this package sets GOMEMLIMIT automatically with system memory
4+
// fallback enabled.
5+
//
6+
// import _ "github.com/KimMachineGun/automemlimit"
7+
//
8+
// For more control, use the memlimit package directly.
19
package automemlimit
210

311
import (
@@ -7,7 +15,13 @@ import (
715
)
816

917
func init() {
10-
memlimit.SetGoMemLimitWithOpts(
18+
memlimit.Set(
19+
memlimit.WithProvider(
20+
memlimit.ApplyFallback(
21+
memlimit.FromCgroup,
22+
memlimit.FromSystem,
23+
),
24+
),
1125
memlimit.WithLogger(slog.Default()),
1226
)
1327
}

examples/dynamic/main.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package main
22

33
import (
44
"bytes"
5+
"context"
56
"errors"
7+
"log"
68
"log/slog"
79
"os"
810
"os/signal"
@@ -12,24 +14,25 @@ import (
1214
"github.com/KimMachineGun/automemlimit/memlimit"
1315
)
1416

15-
func init() {
17+
func main() {
1618
slog.SetDefault(slog.New(slog.NewJSONHandler(os.Stderr, nil)))
1719

18-
memlimit.SetGoMemLimitWithOpts(
20+
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
21+
defer stop()
22+
23+
_, err := memlimit.Set(
1924
memlimit.WithProvider(
2025
FileProvider("limit.txt"),
2126
),
22-
memlimit.WithRefreshInterval(5*time.Second),
27+
memlimit.WithRefreshInterval(ctx, 5*time.Second),
2328
memlimit.WithLogger(slog.Default()),
2429
)
25-
}
26-
27-
func main() {
28-
c := make(chan os.Signal, 1)
29-
signal.Notify(c, os.Interrupt)
30+
if err != nil {
31+
log.Fatal(err)
32+
}
3033

31-
s := <-c
32-
slog.Info("signal captured", slog.Any("signal", s))
34+
<-ctx.Done()
35+
slog.Info("shutdown")
3336
}
3437

3538
func FileProvider(path string) memlimit.Provider {

examples/logger/main.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
package main
22

33
import (
4+
"log"
45
"log/slog"
56
"os"
67

78
"github.com/KimMachineGun/automemlimit/memlimit"
89
)
910

10-
func init() {
11-
memlimit.SetGoMemLimitWithOpts(
11+
func main() {
12+
_, err := memlimit.Set(
1213
memlimit.WithProvider(
1314
memlimit.Limit(1024*1024*1024),
1415
),
1516
memlimit.WithLogger(slog.New(slog.NewJSONHandler(os.Stderr, nil))),
1617
)
18+
if err != nil {
19+
log.Fatal(err)
20+
}
1721
}
18-
19-
func main() {}

examples/system/main.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
package main
22

33
import (
4+
"log"
5+
46
"github.com/KimMachineGun/automemlimit/memlimit"
57
)
68

7-
func init() {
8-
memlimit.SetGoMemLimitWithOpts(
9+
func main() {
10+
_, err := memlimit.Set(
911
memlimit.WithProvider(
1012
memlimit.ApplyFallback(
1113
memlimit.FromCgroup,
1214
memlimit.FromSystem,
1315
),
1416
),
1517
)
18+
if err != nil {
19+
log.Fatal(err)
20+
}
1621
}
17-
18-
func main() {}

0 commit comments

Comments
 (0)