mirror of
https://github.com/SagerNet/sing-box.git
synced 2025-06-13 21:54:13 +08:00
make sure health check is stoped
This commit is contained in:
parent
a20ce25d39
commit
341624dcfa
@ -2,6 +2,7 @@ package balancer
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
@ -9,9 +10,15 @@ 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 (
|
||||||
|
_ common.Starter = (*HealthCheck)(nil)
|
||||||
|
_ io.Closer = (*HealthCheck)(nil)
|
||||||
|
)
|
||||||
|
|
||||||
// HealthCheck is the health checker for balancers
|
// HealthCheck is the health checker for balancers
|
||||||
type HealthCheck struct {
|
type HealthCheck struct {
|
||||||
mutex sync.Mutex
|
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 {
|
func (h *HealthCheck) Start() error {
|
||||||
h.mutex.Lock()
|
h.mutex.Lock()
|
||||||
defer h.mutex.Unlock()
|
defer h.mutex.Unlock()
|
||||||
@ -86,14 +93,15 @@ func (h *HealthCheck) Start() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stop stops the health check service
|
// Close stops the health check service, implements io.Closer
|
||||||
func (h *HealthCheck) Stop() {
|
func (h *HealthCheck) Close() error {
|
||||||
h.mutex.Lock()
|
h.mutex.Lock()
|
||||||
defer h.mutex.Unlock()
|
defer h.mutex.Unlock()
|
||||||
if h.ticker != nil {
|
if h.ticker != nil {
|
||||||
h.ticker.Stop()
|
h.ticker.Stop()
|
||||||
h.ticker = nil
|
h.ticker = nil
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check does a one time health check
|
// Check does a one time health check
|
||||||
|
@ -2,6 +2,7 @@ package outbound
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"io"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"net"
|
"net"
|
||||||
|
|
||||||
@ -17,6 +18,8 @@ import (
|
|||||||
var (
|
var (
|
||||||
_ adapter.Outbound = (*Balancer)(nil)
|
_ adapter.Outbound = (*Balancer)(nil)
|
||||||
_ adapter.OutboundGroup = (*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
|
// 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)
|
return s.pick(N.NetworkUDP).NewPacketConnection(ctx, conn, metadata)
|
||||||
}
|
}
|
||||||
|
|
||||||
// initialize inits the balancer
|
// Close implements io.Closer
|
||||||
func (s *Balancer) initialize() error {
|
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,
|
// 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.
|
||||||
if s.fallbackTag == "" {
|
if s.fallbackTag == "" {
|
||||||
|
@ -1,16 +1,21 @@
|
|||||||
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)
|
||||||
|
_ 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
|
||||||
@ -33,7 +38,7 @@ func NewLeastLoad(router adapter.Router, logger log.ContextLogger, tag string, o
|
|||||||
|
|
||||||
// Start implements common.Starter
|
// Start implements common.Starter
|
||||||
func (s *LeastLoad) Start() error {
|
func (s *LeastLoad) Start() error {
|
||||||
err := s.Balancer.initialize()
|
err := s.Balancer.Start()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -1,16 +1,21 @@
|
|||||||
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)
|
||||||
|
_ 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
|
||||||
@ -33,7 +38,7 @@ func NewLeastPing(router adapter.Router, logger log.ContextLogger, tag string, o
|
|||||||
|
|
||||||
// Start implements common.Starter
|
// Start implements common.Starter
|
||||||
func (s *LeastPing) Start() error {
|
func (s *LeastPing) Start() error {
|
||||||
err := s.Balancer.initialize()
|
err := s.Balancer.Start()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user