This repository has been archived by the owner on Aug 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Calculate goroutines with ulimit (#256)
* fix: Calculate goroutines with ulimit
- Loading branch information
Showing
7 changed files
with
64 additions
and
32 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,34 @@ | ||
package limit | ||
|
||
import ( | ||
"github.com/pbnjay/memory" | ||
) | ||
|
||
const ( | ||
gbInBytes int = 1024 * 1024 * 1024 | ||
goroutinesPerGB float64 = 250000 | ||
) | ||
|
||
func GetMaxGoRoutines() uint64 { | ||
limit := calculateGoRoutines(getMemory()) | ||
ulimit, err := getUlimit() | ||
if err != nil || ulimit == 0 { | ||
return limit | ||
} | ||
if ulimit > limit { | ||
return limit | ||
} | ||
return ulimit | ||
} | ||
|
||
func getMemory() uint64 { | ||
return memory.TotalMemory() | ||
} | ||
|
||
func calculateGoRoutines(totalMemory uint64) uint64 { | ||
if totalMemory == 0 { | ||
// assume we have 2 GB RAM | ||
return uint64(goroutinesPerGB * 2) | ||
} | ||
return uint64(goroutinesPerGB * float64(totalMemory) / float64(gbInBytes)) | ||
} |
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package helpers | ||
package limit | ||
|
||
import ( | ||
"testing" | ||
|
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,13 @@ | ||
//go:build darwin || linux | ||
|
||
package limit | ||
|
||
import ( | ||
"syscall" | ||
) | ||
|
||
func getUlimit() (uint64, error) { | ||
var rLimit syscall.Rlimit | ||
err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit) | ||
return rLimit.Max, err | ||
} |
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,7 @@ | ||
//go:build windows | ||
|
||
package limit | ||
|
||
func getUlimit() (uint64, error) { | ||
return 0, nil | ||
} |
This file was deleted.
Oops, something went wrong.
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
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