-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 5b45e20
Showing
8 changed files
with
159 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2021 Keenan Nemetz | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Ulimit | ||
|
||
A pure Go library for managing resource limits. | ||
|
||
## Usage | ||
|
||
```go | ||
import "github.com/nasdf/ulimit" | ||
``` | ||
|
||
### Set limits | ||
|
||
```go | ||
err := ulimit.SetRlimit(2048) | ||
``` | ||
|
||
### Get limits | ||
|
||
```go | ||
soft, hard, err := ulimit.GetRlimit() | ||
``` | ||
|
||
## License | ||
|
||
MIT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module github.com/nasdf/ulimit | ||
|
||
go 1.14 | ||
|
||
require golang.org/x/sys v0.0.0-20210123111255-9b0068b26619 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
golang.org/x/sys v0.0.0-20210123111255-9b0068b26619 h1:yLLDsUUPDliIQpKl7BjVb1igwngIMH2GBjo1VpwLTE0= | ||
golang.org/x/sys v0.0.0-20210123111255-9b0068b26619/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Package ulimit contains methods for setting resource limits. | ||
package ulimit | ||
|
||
// GetRlimit returns the soft and hard resource limits. | ||
func GetRlimit() (uint64, uint64, error) { | ||
return getRlimit() | ||
} | ||
|
||
// SetRlimit attempts to set the soft and hard resource limits. | ||
// If the hard limit fails to update only the soft limit is set. | ||
func SetRlimit(target uint64) error { | ||
soft, hard, err := getRlimit() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if target <= soft { | ||
return nil | ||
} | ||
|
||
if err = setRlimit(target, target); err == nil { | ||
return nil | ||
} | ||
|
||
if target > hard { | ||
target = hard | ||
} | ||
|
||
return setRlimit(target, hard) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package ulimit | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestSetRlimit(t *testing.T) { | ||
const target = uint64(2048) | ||
if err := SetRlimit(target); err != nil { | ||
t.Fatal("failed to set rlimit") | ||
} | ||
|
||
soft, hard, err := GetRlimit() | ||
if err != nil { | ||
t.Fatal("failed to get rlimit") | ||
} | ||
|
||
if soft != target { | ||
t.Error("unexpected soft target") | ||
} | ||
|
||
if hard != target { | ||
t.Error("unexpected hard target") | ||
} | ||
} | ||
|
||
func TestGetRlimit(t *testing.T) { | ||
soft, hard, err := GetRlimit() | ||
if err != nil { | ||
t.Fatal("failed to get rlimit") | ||
} | ||
|
||
if soft == 0 { | ||
t.Error("unexpected soft limit") | ||
} | ||
|
||
if hard == 0 { | ||
t.Error("unexpected hard limit") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// +build darwin linux netbsd openbsd freebsd | ||
|
||
package ulimit | ||
|
||
import ( | ||
"golang.org/x/sys/unix" | ||
) | ||
|
||
func getRlimit() (uint64, uint64, error) { | ||
var limit unix.Rlimit | ||
if err := unix.Getrlimit(unix.RLIMIT_NOFILE, &limit); err != nil { | ||
return 0, 0, err | ||
} | ||
|
||
return limit.Cur, limit.Max, nil | ||
} | ||
|
||
func setRlimit(soft uint64, max uint64) error { | ||
limit := unix.Rlimit{ | ||
Cur: soft, | ||
Max: max, | ||
} | ||
|
||
return unix.Setrlimit(unix.RLIMIT_NOFILE, &limit) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// +build windows | ||
|
||
package ulimit | ||
|
||
func getRlimit() (uint64, uint64, error) { | ||
return 0, 0, nil | ||
} | ||
|
||
func setRlimit(soft uint64, max uint64) error { | ||
return nil | ||
} |