code optimize

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

View File

@ -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()

View File

@ -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.

View File

@ -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 {

View File

@ -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 {