-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sd: register in service discovery with weight
- Loading branch information
Showing
17 changed files
with
1,086 additions
and
51 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,37 @@ | ||
name: Tests register in SD | ||
|
||
on: | ||
push: | ||
branches: [ master ] | ||
pull_request: | ||
branches: [ master ] | ||
|
||
jobs: | ||
|
||
tests: | ||
env: | ||
CGO_ENABLED: 0 | ||
name: Test register in SD | ||
runs-on: ubuntu-latest | ||
strategy: | ||
matrix: | ||
go: | ||
- ^1 | ||
steps: | ||
|
||
- name: Set up Go | ||
uses: actions/setup-go@v2 | ||
with: | ||
go-version: ${{ matrix.go }} | ||
|
||
- name: Check out code | ||
uses: actions/checkout@v2 | ||
|
||
- name: Start consul | ||
run: | | ||
./tests/consul.sh 1.15.2 > /tmp/consul.log & | ||
sleep 30 | ||
shell: bash | ||
|
||
- name: Test | ||
run: go test ./sd/nginx -tags=test_sd -v |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,33 @@ | ||
package load_avg | ||
|
||
import ( | ||
"os" | ||
"strings" | ||
"syscall" | ||
"math" | ||
|
||
"github.com/msaf1980/go-stringutils" | ||
"github.com/msaf1980/go-syncutils/atomic" | ||
) | ||
|
||
var ( | ||
loadAvgStore atomic.Float64 | ||
) | ||
|
||
func Normalized() (float64, error) { | ||
var info syscall.Sysinfo_t | ||
err := syscall.Sysinfo(&info) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
cpus, err := CpuCount() | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
const si_load_shift = 16 | ||
load1 := float64(info.Loads[0]) / float64(1<<si_load_shift) / float64(cpus) | ||
return load1, nil | ||
} | ||
|
||
func CpuCount() (uint64, error) { | ||
b, err := os.ReadFile("/proc/cpuinfo") | ||
if err != nil { | ||
return 0, err | ||
} | ||
s := stringutils.UnsafeString(b) | ||
|
||
cpus := strings.Count(s, "processor\t: ") | ||
|
||
return uint64(cpus), nil | ||
} | ||
|
||
func Load() float64 { | ||
return loadAvgStore.Load() | ||
} | ||
|
||
func Store(f float64) { | ||
loadAvgStore.Store(f) | ||
} | ||
|
||
func Weight(n int, l float64) int64 { | ||
// (1 / normalized_load_avg - 1) | ||
l = math.Round(10*l) / 10 | ||
if l == 0 { | ||
return 2 * int64(n) | ||
} | ||
l = math.Log10(l) | ||
w := int64(n) - int64(float64(n)*l) | ||
if w < 0 { | ||
return 0 | ||
} | ||
return w | ||
} |
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,20 @@ | ||
//go:build !linux | ||
// +build !linux | ||
|
||
package load_avg | ||
|
||
import ( | ||
"os" | ||
"strings" | ||
"syscall" | ||
|
||
"github.com/msaf1980/go-stringutils" | ||
) | ||
|
||
func Normalized() (float64, error) { | ||
return 0, nil | ||
} | ||
|
||
func CpuCount() (uint64, error) { | ||
return 0, nil | ||
} |
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,41 @@ | ||
//go:build linux | ||
// +build linux | ||
|
||
package load_avg | ||
|
||
import ( | ||
"os" | ||
"strings" | ||
"syscall" | ||
|
||
"github.com/msaf1980/go-stringutils" | ||
) | ||
|
||
func Normalized() (float64, error) { | ||
var info syscall.Sysinfo_t | ||
err := syscall.Sysinfo(&info) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
cpus, err := CpuCount() | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
const si_load_shift = 16 | ||
load := float64(info.Loads[0]) / float64(1<<si_load_shift) / float64(cpus) | ||
return load, nil | ||
} | ||
|
||
func CpuCount() (uint64, error) { | ||
b, err := os.ReadFile("/proc/cpuinfo") | ||
if err != nil { | ||
return 0, err | ||
} | ||
s := stringutils.UnsafeString(b) | ||
|
||
cpus := strings.Count(s, "processor\t: ") | ||
|
||
return uint64(cpus), nil | ||
} |
Oops, something went wrong.