make sure health check is stoped

This commit is contained in:
jebbs 2022-10-12 15:35:11 +08:00
parent a20ce25d39
commit 341624dcfa
4 changed files with 36 additions and 7 deletions

View File

@ -2,6 +2,7 @@ package balancer
import (
"fmt"
"io"
"math/rand"
"sync"
"time"
@ -9,9 +10,15 @@ 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)
)
// HealthCheck is the health checker for balancers
type HealthCheck struct {
mutex sync.Mutex
@ -62,7 +69,7 @@ func NewHealthCheck(router adapter.Router, tags []string, logger log.Logger, con
}
}
// Start starts the health check service
// Start starts the health check service, implements common.Starter
func (h *HealthCheck) Start() error {
h.mutex.Lock()
defer h.mutex.Unlock()
@ -86,14 +93,15 @@ func (h *HealthCheck) Start() error {
return nil
}
// Stop stops the health check service
func (h *HealthCheck) Stop() {
// Close stops the health check service, implements io.Closer
func (h *HealthCheck) Close() error {
h.mutex.Lock()
defer h.mutex.Unlock()
if h.ticker != nil {
h.ticker.Stop()
h.ticker = nil
}
return nil
}
// Check does a one time health check

View File

@ -2,6 +2,7 @@ package outbound
import (
"context"
"io"
"math/rand"
"net"
@ -17,6 +18,8 @@ import (
var (
_ adapter.Outbound = (*Balancer)(nil)
_ adapter.OutboundGroup = (*Balancer)(nil)
_ common.Starter = (*Balancer)(nil)
_ io.Closer = (*Balancer)(nil)
)
// Balancer is a outbound group that picks outbound with least load
@ -112,8 +115,16 @@ func (s *Balancer) NewPacketConnection(ctx context.Context, conn N.PacketConn, m
return s.pick(N.NetworkUDP).NewPacketConnection(ctx, conn, metadata)
}
// initialize inits the balancer
func (s *Balancer) initialize() error {
// Close implements io.Closer
func (s *Balancer) Close() error {
if c, ok := s.Balancer.(io.Closer); ok {
return c.Close()
}
return nil
}
// Start implements common.Starter
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.
if s.fallbackTag == "" {

View File

@ -1,16 +1,21 @@
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)
)
// LeastLoad is a outbound group that picks outbound with least load
@ -33,7 +38,7 @@ func NewLeastLoad(router adapter.Router, logger log.ContextLogger, tag string, o
// Start implements common.Starter
func (s *LeastLoad) Start() error {
err := s.Balancer.initialize()
err := s.Balancer.Start()
if err != nil {
return err
}

View File

@ -1,16 +1,21 @@
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)
)
// LeastPing is a outbound group that picks outbound with least load
@ -33,7 +38,7 @@ func NewLeastPing(router adapter.Router, logger log.ContextLogger, tag string, o
// Start implements common.Starter
func (s *LeastPing) Start() error {
err := s.Balancer.initialize()
err := s.Balancer.Start()
if err != nil {
return err
}