improve logging

This commit is contained in:
jebbs 2022-10-12 17:03:10 +08:00
parent aa3725e2c1
commit e1bf48ee07
2 changed files with 20 additions and 13 deletions

View File

@ -1,6 +1,7 @@
package balancer package balancer
import ( import (
"context"
"math" "math"
"math/rand" "math/rand"
"sort" "sort"
@ -18,7 +19,7 @@ var _ Balancer = (*rttBasedBalancer)(nil)
// Balancer is interface for load balancers // Balancer is interface for load balancers
type Balancer interface { type Balancer interface {
// Pick picks a qualified nodes // Pick picks a qualified nodes
Pick(network string) string Pick(ctx context.Context, network string) string
// Networks returns the supported network types // Networks returns the supported network types
Networks() []string Networks() []string
} }
@ -27,6 +28,7 @@ type rttBasedBalancer struct {
nodes []*Node nodes []*Node
rttFunc rttFunc rttFunc rttFunc
options *option.BalancerOutboundOptions options *option.BalancerOutboundOptions
logger log.ContextLogger
*HealthCheck *HealthCheck
costs *WeightManager costs *WeightManager
@ -43,6 +45,7 @@ func newRTTBasedBalancer(
return &rttBasedBalancer{ return &rttBasedBalancer{
rttFunc: rttFunc, rttFunc: rttFunc,
options: &options, options: &options,
logger: logger,
HealthCheck: NewHealthCheck(router, options.Outbounds, logger, &options.Check), HealthCheck: NewHealthCheck(router, options.Outbounds, logger, &options.Check),
costs: NewWeightManager( costs: NewWeightManager(
logger, options.Pick.Costs, 1, logger, options.Pick.Costs, 1,
@ -94,7 +97,7 @@ func (s *rttBasedBalancer) Networks() []string {
} }
// Select selects qualified nodes // Select selects qualified nodes
func (s *rttBasedBalancer) Pick(network string) string { func (s *rttBasedBalancer) Pick(ctx context.Context, network string) string {
nodes := s.HealthCheck.Nodes(network) nodes := s.HealthCheck.Nodes(network)
var candidates []*Node var candidates []*Node
if len(nodes.Qualified) > 0 { if len(nodes.Qualified) > 0 {
@ -113,8 +116,10 @@ func (s *rttBasedBalancer) Pick(network string) string {
return "" return ""
} }
picked := selects[rand.Intn(count)] picked := selects[rand.Intn(count)]
s.logger.Debug( s.logger.DebugContext(
"pick [", picked.Tag, "]", ctx,
"(network=", network, ", candidates=", count, ")",
" => [", picked.Tag, "]",
" +W=", picked.Weighted, " +W=", picked.Weighted,
" STD=", picked.Deviation, " STD=", picked.Deviation,
" AVG=", picked.Average, " AVG=", picked.Average,

View File

@ -87,7 +87,7 @@ func (s *Balancer) Network() []string {
// Now implements adapter.OutboundGroup // Now implements adapter.OutboundGroup
func (s *Balancer) Now() string { func (s *Balancer) Now() string {
return s.pick("").Tag() return s.pick(context.Background(), "").Tag()
} }
// All implements adapter.OutboundGroup // All implements adapter.OutboundGroup
@ -97,22 +97,22 @@ func (s *Balancer) All() []string {
// DialContext implements adapter.Outbound // DialContext implements adapter.Outbound
func (s *Balancer) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) { func (s *Balancer) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
return s.pick(network).DialContext(ctx, network, destination) return s.pick(ctx, network).DialContext(ctx, network, destination)
} }
// ListenPacket implements adapter.Outbound // ListenPacket implements adapter.Outbound
func (s *Balancer) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) { func (s *Balancer) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
return s.pick(N.NetworkUDP).ListenPacket(ctx, destination) return s.pick(ctx, N.NetworkUDP).ListenPacket(ctx, destination)
} }
// NewConnection implements adapter.Outbound // NewConnection implements adapter.Outbound
func (s *Balancer) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error { func (s *Balancer) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
return s.pick(N.NetworkTCP).NewConnection(ctx, conn, metadata) return s.pick(ctx, N.NetworkTCP).NewConnection(ctx, conn, metadata)
} }
// NewPacketConnection implements adapter.Outbound // NewPacketConnection implements adapter.Outbound
func (s *Balancer) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error { func (s *Balancer) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
return s.pick(N.NetworkUDP).NewPacketConnection(ctx, conn, metadata) return s.pick(ctx, N.NetworkUDP).NewPacketConnection(ctx, conn, metadata)
} }
// Close implements io.Closer // Close implements io.Closer
@ -149,24 +149,26 @@ func (s *Balancer) setBalancer(b balancer.Balancer) error {
return nil return nil
} }
func (s *Balancer) pick(network string) adapter.Outbound { func (s *Balancer) pick(ctx context.Context, network string) adapter.Outbound {
tag := s.pickTag(network) tag := s.pickTag(ctx, network)
if tag == "" { if tag == "" {
s.logger.DebugContext(ctx, "(network=", network, ", candidates=0) => fallback [", s.fallbackTag, "]")
return s.fallback return s.fallback
} }
outbound, ok := s.router.Outbound(tag) outbound, ok := s.router.Outbound(tag)
if !ok { if !ok {
s.logger.DebugContext(ctx, "[", tag, "] not exist, fallback to [", s.fallbackTag, "]")
return s.fallback return s.fallback
} }
return outbound return outbound
} }
func (s *Balancer) pickTag(network string) string { func (s *Balancer) pickTag(ctx context.Context, network string) string {
if s.Balancer == nil { if s.Balancer == nil {
// not started yet, pick a random one // not started yet, pick a random one
return s.randomTag() return s.randomTag()
} }
tag := s.Balancer.Pick(network) tag := s.Balancer.Pick(ctx, network)
if tag == "" { if tag == "" {
return "" return ""
} }