1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-05 09:47:56 -05:00
v2fly/app/router/router_health.go
Jebbs fa0cf6db26
v5: Health Check & LeastLoad Strategy (rebased from 2c5a714903)
Some changes will be necessary to integrate it into V2Ray
2021-09-04 11:11:45 +01:00

119 lines
2.4 KiB
Go

package router
import (
"errors"
"strings"
"github.com/v2fly/v2ray-core/v4/features/routing"
)
// CheckHanlders implements routing.RouterChecker.
func (r *Router) CheckHanlders(tags []string) error {
errs := make([]error, 0)
for _, b := range r.balancers {
checker, ok := b.strategy.(routing.HealthChecker)
if !ok {
continue
}
all, err := b.SelectOutbounds()
if err != nil {
return err
}
ts := getCheckTags(tags, all)
err = checker.Check(ts)
if err != nil {
errs = append(errs, err)
}
}
if len(errs) == 0 {
return nil
}
return getCollectError(errs)
}
func getCheckTags(tags, all []string) []string {
ts := make([]string, 0)
for _, t := range tags {
if findSliceIndex(all, t) >= 0 && findSliceIndex(ts, t) < 0 {
ts = append(ts, t)
}
}
return ts
}
// CheckBalancers implements routing.RouterChecker.
func (r *Router) CheckBalancers(tags []string) error {
errs := make([]error, 0)
for t, b := range r.balancers {
if len(tags) > 0 && findSliceIndex(tags, t) < 0 {
continue
}
checker, ok := b.strategy.(routing.HealthChecker)
if !ok {
continue
}
tags, err := b.SelectOutbounds()
if err != nil {
errs = append(errs, err)
}
err = checker.Check(tags)
if err != nil {
errs = append(errs, err)
}
}
if len(errs) == 0 {
return nil
}
return getCollectError(errs)
}
func getCollectError(errs []error) error {
sb := new(strings.Builder)
sb.WriteString("collect errors:\n")
for _, err := range errs {
sb.WriteString(" * ")
sb.WriteString(err.Error())
sb.WriteString("\n")
}
return errors.New(sb.String())
}
// GetBalancersInfo implements routing.RouterChecker.
func (r *Router) GetBalancersInfo(tags []string) (resp []*routing.BalancerInfo, err error) {
resp = make([]*routing.BalancerInfo, 0)
for t, b := range r.balancers {
if len(tags) > 0 && findSliceIndex(tags, t) < 0 {
continue
}
all, err := b.SelectOutbounds()
if err != nil {
return nil, err
}
var override *routing.BalancingOverrideInfo
if o := b.override.Get(); o != nil {
override = &routing.BalancingOverrideInfo{
Until: o.until,
Selects: o.selects,
}
}
stat := &routing.BalancerInfo{
Tag: t,
Override: override,
Strategy: b.strategy.GetInformation(all),
}
resp = append(resp, stat)
}
return resp, nil
}
func findSliceIndex(slice []string, find string) int {
index := -1
for i, v := range slice {
if find == v {
index = i
break
}
}
return index
}