Skip to content

Commit fc3f1f0

Browse files
committed
feat!: ship v1 release with simplified APIs
1 parent 6d12049 commit fc3f1f0

14 files changed

Lines changed: 175 additions & 395 deletions

README.md

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,7 @@ 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.
15-
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.
13+
Version `v1.0.0` introduces breaking changes to simplify the API. The library now provides a single `memlimit.Set` function, and system memory limits are used automatically as a fallback when cgroup limits are unavailable.
1814

1915
## Installation
2016

@@ -28,40 +24,44 @@ go get github.com/KimMachineGun/automemlimit@latest
2824
package main
2925

3026
// 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.
27+
// System memory is used as a fallback if cgroup is not available.
3328
import _ "github.com/KimMachineGun/automemlimit"
3429
```
3530

31+
> **Note:** The automatic system-memory fallback applies only when you use the convenience import shown above. Calling `memlimit.Set` directly defaults to the cgroup provider; add an explicit fallback if you need it.
32+
3633
or
3734

3835
```go
3936
package main
4037

41-
import "github.com/KimMachineGun/automemlimit/memlimit"
38+
import (
39+
"context"
40+
"log/slog"
41+
"time"
42+
43+
"github.com/KimMachineGun/automemlimit/memlimit"
44+
)
4245

43-
func init() {
44-
memlimit.SetGoMemLimitWithOpts(
46+
func main() {
47+
// Set GOMEMLIMIT to 90% of cgroup's memory limit (no automatic fallback)
48+
memlimit.Set()
49+
50+
// With explicit system memory fallback
51+
memlimit.Set(
52+
memlimit.WithProvider(
53+
memlimit.ApplyFallback(memlimit.FromCgroup, memlimit.FromSystem),
54+
),
4555
memlimit.WithRatio(0.9),
46-
memlimit.WithProvider(memlimit.FromCgroup),
4756
memlimit.WithLogger(slog.Default()),
48-
memlimit.WithRefreshInterval(1*time.Minute),
4957
)
50-
memlimit.SetGoMemLimitWithOpts(
51-
memlimit.WithRatio(0.9),
52-
memlimit.WithProvider(
53-
memlimit.ApplyFallback(
54-
memlimit.FromCgroup,
55-
memlimit.FromSystem,
56-
),
57-
),
58-
memlimit.WithRefreshInterval(1*time.Minute),
58+
59+
// With refresh interval
60+
refreshCtx, cancel := context.WithCancel(context.Background())
61+
defer cancel()
62+
63+
memlimit.Set(
64+
memlimit.WithRefreshInterval(refreshCtx, 1*time.Minute),
5965
)
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)
6666
}
6767
```

automemlimit.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@ import (
77
)
88

99
func init() {
10-
memlimit.SetGoMemLimitWithOpts(
10+
memlimit.Set(
11+
memlimit.WithProvider(
12+
memlimit.ApplyFallback(
13+
memlimit.FromCgroup,
14+
memlimit.FromSystem,
15+
),
16+
),
1117
memlimit.WithLogger(slog.Default()),
1218
)
1319
}

examples/dynamic/main.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"bytes"
5+
"context"
56
"errors"
67
"log/slog"
78
"os"
@@ -15,11 +16,12 @@ import (
1516
func init() {
1617
slog.SetDefault(slog.New(slog.NewJSONHandler(os.Stderr, nil)))
1718

18-
memlimit.SetGoMemLimitWithOpts(
19+
refreshCtx := context.Background()
20+
memlimit.Set(
1921
memlimit.WithProvider(
2022
FileProvider("limit.txt"),
2123
),
22-
memlimit.WithRefreshInterval(5*time.Second),
24+
memlimit.WithRefreshInterval(refreshCtx, 5*time.Second),
2325
memlimit.WithLogger(slog.Default()),
2426
)
2527
}
@@ -37,7 +39,7 @@ func FileProvider(path string) memlimit.Provider {
3739
b, err := os.ReadFile(path)
3840
if err != nil {
3941
if errors.Is(err, os.ErrNotExist) {
40-
return memlimit.ApplyFallback(memlimit.FromCgroup, memlimit.FromSystem)()
42+
return memlimit.FromCgroup()
4143
}
4244
return 0, err
4345
}

examples/logger/main.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@ import (
88
)
99

1010
func init() {
11-
memlimit.SetGoMemLimitWithOpts(
12-
memlimit.WithProvider(
13-
memlimit.Limit(1024*1024*1024),
14-
),
11+
memlimit.Set(
1512
memlimit.WithLogger(slog.New(slog.NewJSONHandler(os.Stderr, nil))),
1613
)
1714
}

examples/system/main.go

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

3-
import (
4-
"github.com/KimMachineGun/automemlimit/memlimit"
5-
)
3+
import "github.com/KimMachineGun/automemlimit/memlimit"
64

75
func init() {
8-
memlimit.SetGoMemLimitWithOpts(
6+
memlimit.Set(
97
memlimit.WithProvider(
108
memlimit.ApplyFallback(
119
memlimit.FromCgroup,

memlimit/cgroups_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ func TestParseMountInfoLine(t *testing.T) {
8383
},
8484
},
8585
{
86-
name: "super options have spaces (issue #28)",
87-
input: `1391 1160 0:151 / /Docker/host rw,noatime - 9p C:\134Program\040Files\134Docker\134Docker\134resources rw,dirsync,aname=drvfs;path=C:\Program Files\Docker\Docker\resources;symlinkroot=/mnt/,mmap,access=client,msize=65536,trans=fd,rfd=3,wfd=3`,
86+
name: "super options have spaces (issue #28)",
87+
input: `1391 1160 0:151 / /Docker/host rw,noatime - 9p C:\134Program\040Files\134Docker\134Docker\134resources rw,dirsync,aname=drvfs;path=C:\Program Files\Docker\Docker\resources;symlinkroot=/mnt/,mmap,access=client,msize=65536,trans=fd,rfd=3,wfd=3`,
8888
want: mountInfo{
8989
Root: "/",
9090
MountPoint: "/Docker/host",

memlimit/experiment.go

Lines changed: 0 additions & 59 deletions
This file was deleted.

memlimit/experiment_test.go

Lines changed: 0 additions & 67 deletions
This file was deleted.

memlimit/logger.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,18 @@ import (
55
"log/slog"
66
)
77

8-
type noopLogger struct{}
8+
var _ slog.Handler = discardHandler{}
99

10-
func (noopLogger) Enabled(context.Context, slog.Level) bool { return false }
11-
func (noopLogger) Handle(context.Context, slog.Record) error { return nil }
12-
func (d noopLogger) WithAttrs([]slog.Attr) slog.Handler { return d }
13-
func (d noopLogger) WithGroup(string) slog.Handler { return d }
10+
type discardHandler struct{}
11+
12+
func (discardHandler) Enabled(context.Context, slog.Level) bool { return false }
13+
func (discardHandler) Handle(context.Context, slog.Record) error { return nil }
14+
func (dh discardHandler) WithAttrs([]slog.Attr) slog.Handler { return dh }
15+
func (dh discardHandler) WithGroup(string) slog.Handler { return dh }
16+
17+
func memlimitLogger(logger *slog.Logger) *slog.Logger {
18+
if logger == nil {
19+
return slog.New(discardHandler{})
20+
}
21+
return logger.With(slog.String("package", "github.com/KimMachineGun/automemlimit/memlimit"))
22+
}

0 commit comments

Comments
 (0)