code optimize

This commit is contained in:
jebbs 2022-10-14 14:47:00 +08:00
parent 9440f8f060
commit eff755f49b
4 changed files with 10 additions and 22 deletions

View File

@ -2,7 +2,6 @@ package balancer
import ( import (
"fmt" "fmt"
"io"
"math/rand" "math/rand"
"sync" "sync"
"time" "time"
@ -10,13 +9,11 @@ import (
"github.com/sagernet/sing-box/adapter" "github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing-box/log" "github.com/sagernet/sing-box/log"
"github.com/sagernet/sing-box/option" "github.com/sagernet/sing-box/option"
"github.com/sagernet/sing/common"
E "github.com/sagernet/sing/common/exceptions" E "github.com/sagernet/sing/common/exceptions"
) )
var ( var (
_ common.Starter = (*HealthCheck)(nil) _ adapter.Service = (*HealthCheck)(nil)
_ io.Closer = (*HealthCheck)(nil)
) )
// HealthCheck is the health checker for balancers // HealthCheck is the health checker for balancers
@ -69,7 +66,7 @@ func NewHealthCheck(router adapter.Router, tags []string, logger log.Logger, con
} }
} }
// Start starts the health check service, implements common.Starter // Start starts the health check service, implements adapter.Service
func (h *HealthCheck) Start() error { func (h *HealthCheck) Start() error {
h.mutex.Lock() h.mutex.Lock()
defer h.mutex.Unlock() defer h.mutex.Unlock()
@ -93,7 +90,7 @@ func (h *HealthCheck) Start() error {
return nil return nil
} }
// Close stops the health check service, implements io.Closer // Close stops the health check service, implements adapter.Service
func (h *HealthCheck) Close() error { func (h *HealthCheck) Close() error {
h.mutex.Lock() h.mutex.Lock()
defer h.mutex.Unlock() defer h.mutex.Unlock()

View File

@ -18,8 +18,7 @@ import (
var ( var (
_ adapter.Outbound = (*Balancer)(nil) _ adapter.Outbound = (*Balancer)(nil)
_ adapter.OutboundGroup = (*Balancer)(nil) _ adapter.OutboundGroup = (*Balancer)(nil)
_ common.Starter = (*Balancer)(nil) _ adapter.Service = (*Balancer)(nil)
_ io.Closer = (*Balancer)(nil)
) )
// Balancer is a outbound group that picks outbound with least load // Balancer is a outbound group that picks outbound with least load
@ -115,7 +114,7 @@ func (s *Balancer) NewPacketConnection(ctx context.Context, conn N.PacketConn, m
return s.pick(ctx, N.NetworkUDP).NewPacketConnection(ctx, conn, metadata) return s.pick(ctx, N.NetworkUDP).NewPacketConnection(ctx, conn, metadata)
} }
// Close implements io.Closer // Close implements adapter.Service
func (s *Balancer) Close() error { func (s *Balancer) Close() error {
if c, ok := s.Balancer.(io.Closer); ok { if c, ok := s.Balancer.(io.Closer); ok {
return c.Close() return c.Close()
@ -123,7 +122,7 @@ func (s *Balancer) Close() error {
return nil return nil
} }
// Start implements common.Starter // Start implements adapter.Service
func (s *Balancer) Start() error { func (s *Balancer) Start() error {
// the fallback is required, in case that all outbounds are not available, // the fallback is required, in case that all outbounds are not available,
// we can pick it instead of returning nil to avoid panic. // we can pick it instead of returning nil to avoid panic.

View File

@ -1,21 +1,17 @@
package outbound package outbound
import ( import (
"io"
"github.com/sagernet/sing-box/adapter" "github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing-box/balancer" "github.com/sagernet/sing-box/balancer"
C "github.com/sagernet/sing-box/constant" C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/log" "github.com/sagernet/sing-box/log"
"github.com/sagernet/sing-box/option" "github.com/sagernet/sing-box/option"
"github.com/sagernet/sing/common"
) )
var ( var (
_ adapter.Outbound = (*LeastLoad)(nil) _ adapter.Outbound = (*LeastLoad)(nil)
_ adapter.OutboundGroup = (*LeastLoad)(nil) _ adapter.OutboundGroup = (*LeastLoad)(nil)
_ common.Starter = (*LeastLoad)(nil) _ adapter.Service = (*LeastLoad)(nil)
_ io.Closer = (*LeastLoad)(nil)
) )
// LeastLoad is a outbound group that picks outbound with least load // LeastLoad is a outbound group that picks outbound with least load
@ -36,7 +32,7 @@ func NewLeastLoad(router adapter.Router, logger log.ContextLogger, tag string, o
}, nil }, nil
} }
// Start implements common.Starter // Start implements adapter.Service
func (s *LeastLoad) Start() error { func (s *LeastLoad) Start() error {
b, err := balancer.NewLeastLoad(s.router, s.logger, s.options) b, err := balancer.NewLeastLoad(s.router, s.logger, s.options)
if err != nil { if err != nil {

View File

@ -1,21 +1,17 @@
package outbound package outbound
import ( import (
"io"
"github.com/sagernet/sing-box/adapter" "github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing-box/balancer" "github.com/sagernet/sing-box/balancer"
C "github.com/sagernet/sing-box/constant" C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/log" "github.com/sagernet/sing-box/log"
"github.com/sagernet/sing-box/option" "github.com/sagernet/sing-box/option"
"github.com/sagernet/sing/common"
) )
var ( var (
_ adapter.Outbound = (*LeastPing)(nil) _ adapter.Outbound = (*LeastPing)(nil)
_ adapter.OutboundGroup = (*LeastPing)(nil) _ adapter.OutboundGroup = (*LeastPing)(nil)
_ common.Starter = (*LeastPing)(nil) _ adapter.Service = (*LeastPing)(nil)
_ io.Closer = (*LeastPing)(nil)
) )
// LeastPing is a outbound group that picks outbound with least load // LeastPing is a outbound group that picks outbound with least load
@ -36,7 +32,7 @@ func NewLeastPing(router adapter.Router, logger log.ContextLogger, tag string, o
}, nil }, nil
} }
// Start implements common.Starter // Start implements adapter.Service
func (s *LeastPing) Start() error { func (s *LeastPing) Start() error {
b, err := balancer.NewLeastPing(s.router, s.logger, s.options) b, err := balancer.NewLeastPing(s.router, s.logger, s.options)
if err != nil { if err != nil {