summaryrefslogtreecommitdiff
path: root/vendor/github.com/KimMachineGun/automemlimit/memlimit/provider.go
diff options
context:
space:
mode:
authorLibravatar dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>2023-12-11 10:35:15 +0000
committerLibravatar GitHub <noreply@github.com>2023-12-11 10:35:15 +0000
commitcd1611362f6f8e7b20b5962b20a5b37d624d8cc6 (patch)
treef17730d75b7bf77e52ea50178964282b9c0ad83a /vendor/github.com/KimMachineGun/automemlimit/memlimit/provider.go
parent[chore]: Bump github.com/miekg/dns from 1.1.56 to 1.1.57 (#2439) (diff)
downloadgotosocial-cd1611362f6f8e7b20b5962b20a5b37d624d8cc6.tar.xz
[chore]: Bump github.com/KimMachineGun/automemlimit from 0.3.0 to 0.4.0 (#2440)
Bumps [github.com/KimMachineGun/automemlimit](https://github.com/KimMachineGun/automemlimit) from 0.3.0 to 0.4.0. - [Commits](https://github.com/KimMachineGun/automemlimit/compare/v0.3.0...v0.4.0) --- updated-dependencies: - dependency-name: github.com/KimMachineGun/automemlimit dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Diffstat (limited to 'vendor/github.com/KimMachineGun/automemlimit/memlimit/provider.go')
-rw-r--r--vendor/github.com/KimMachineGun/automemlimit/memlimit/provider.go40
1 files changed, 40 insertions, 0 deletions
diff --git a/vendor/github.com/KimMachineGun/automemlimit/memlimit/provider.go b/vendor/github.com/KimMachineGun/automemlimit/memlimit/provider.go
new file mode 100644
index 000000000..32cc1eea6
--- /dev/null
+++ b/vendor/github.com/KimMachineGun/automemlimit/memlimit/provider.go
@@ -0,0 +1,40 @@
+package memlimit
+
+import (
+ "fmt"
+)
+
+// Provider is a function that returns the memory limit.
+type Provider func() (uint64, error)
+
+// Limit is a helper Provider function that returns the given limit.
+func Limit(limit uint64) func() (uint64, error) {
+ return func() (uint64, error) {
+ return limit, nil
+ }
+}
+
+// ApplyRationA is a helper Provider function that applies the given ratio to the given provider.
+func ApplyRatio(provider Provider, ratio float64) Provider {
+ return func() (uint64, error) {
+ if ratio <= 0 || ratio > 1 {
+ return 0, fmt.Errorf("invalid ratio: %f, ratio should be in the range (0.0,1.0]", ratio)
+ }
+ limit, err := provider()
+ if err != nil {
+ return 0, err
+ }
+ return uint64(float64(limit) * ratio), nil
+ }
+}
+
+// ApplyFallback is a helper Provider function that sets the fallback provider.
+func ApplyFallback(provider Provider, fallback Provider) Provider {
+ return func() (uint64, error) {
+ limit, err := provider()
+ if err != nil {
+ return fallback()
+ }
+ return limit, nil
+ }
+}