mirror of
https://github.com/SagerNet/sing-box.git
synced 2025-06-13 21:54:13 +08:00
code optimize
This commit is contained in:
parent
9440f8f060
commit
eff755f49b
@ -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()
|
||||
|
@ -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.
|
||||
|
@ -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 {
|
||||
|
@ -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 {
|
||||
|
Loading…
x
Reference in New Issue
Block a user