From eff755f49b0d227ffd16007b0809fc450ee9dc61 Mon Sep 17 00:00:00 2001 From: jebbs Date: Fri, 14 Oct 2022 14:47:00 +0800 Subject: [PATCH] code optimize --- balancer/healthcheck.go | 9 +++------ outbound/balancer.go | 7 +++---- outbound/leastload.go | 8 ++------ outbound/leastping.go | 8 ++------ 4 files changed, 10 insertions(+), 22 deletions(-) diff --git a/balancer/healthcheck.go b/balancer/healthcheck.go index 1e9981c0..d8a5a2ec 100644 --- a/balancer/healthcheck.go +++ b/balancer/healthcheck.go @@ -2,7 +2,6 @@ package balancer import ( "fmt" - "io" "math/rand" "sync" "time" @@ -10,13 +9,11 @@ import ( "github.com/sagernet/sing-box/adapter" "github.com/sagernet/sing-box/log" "github.com/sagernet/sing-box/option" - "github.com/sagernet/sing/common" E "github.com/sagernet/sing/common/exceptions" ) var ( - _ common.Starter = (*HealthCheck)(nil) - _ io.Closer = (*HealthCheck)(nil) + _ adapter.Service = (*HealthCheck)(nil) ) // 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 { h.mutex.Lock() defer h.mutex.Unlock() @@ -93,7 +90,7 @@ func (h *HealthCheck) Start() error { 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 { h.mutex.Lock() defer h.mutex.Unlock() diff --git a/outbound/balancer.go b/outbound/balancer.go index 7504d44d..344a3956 100644 --- a/outbound/balancer.go +++ b/outbound/balancer.go @@ -18,8 +18,7 @@ import ( var ( _ adapter.Outbound = (*Balancer)(nil) _ adapter.OutboundGroup = (*Balancer)(nil) - _ common.Starter = (*Balancer)(nil) - _ io.Closer = (*Balancer)(nil) + _ adapter.Service = (*Balancer)(nil) ) // 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) } -// Close implements io.Closer +// Close implements adapter.Service func (s *Balancer) Close() error { if c, ok := s.Balancer.(io.Closer); ok { return c.Close() @@ -123,7 +122,7 @@ func (s *Balancer) Close() error { return nil } -// Start implements common.Starter +// Start implements adapter.Service func (s *Balancer) Start() error { // the fallback is required, in case that all outbounds are not available, // we can pick it instead of returning nil to avoid panic. diff --git a/outbound/leastload.go b/outbound/leastload.go index 137217a0..3defa8ec 100644 --- a/outbound/leastload.go +++ b/outbound/leastload.go @@ -1,21 +1,17 @@ package outbound import ( - "io" - "github.com/sagernet/sing-box/adapter" "github.com/sagernet/sing-box/balancer" C "github.com/sagernet/sing-box/constant" "github.com/sagernet/sing-box/log" "github.com/sagernet/sing-box/option" - "github.com/sagernet/sing/common" ) var ( _ adapter.Outbound = (*LeastLoad)(nil) _ adapter.OutboundGroup = (*LeastLoad)(nil) - _ common.Starter = (*LeastLoad)(nil) - _ io.Closer = (*LeastLoad)(nil) + _ adapter.Service = (*LeastLoad)(nil) ) // 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 } -// Start implements common.Starter +// Start implements adapter.Service func (s *LeastLoad) Start() error { b, err := balancer.NewLeastLoad(s.router, s.logger, s.options) if err != nil { diff --git a/outbound/leastping.go b/outbound/leastping.go index 97150a23..ea90355a 100644 --- a/outbound/leastping.go +++ b/outbound/leastping.go @@ -1,21 +1,17 @@ package outbound import ( - "io" - "github.com/sagernet/sing-box/adapter" "github.com/sagernet/sing-box/balancer" C "github.com/sagernet/sing-box/constant" "github.com/sagernet/sing-box/log" "github.com/sagernet/sing-box/option" - "github.com/sagernet/sing/common" ) var ( _ adapter.Outbound = (*LeastPing)(nil) _ adapter.OutboundGroup = (*LeastPing)(nil) - _ common.Starter = (*LeastPing)(nil) - _ io.Closer = (*LeastPing)(nil) + _ adapter.Service = (*LeastPing)(nil) ) // 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 } -// Start implements common.Starter +// Start implements adapter.Service func (s *LeastPing) Start() error { b, err := balancer.NewLeastPing(s.router, s.logger, s.options) if err != nil {