github.com/sagernet/sing-box@v1.9.0-rc.20/common/taskmonitor/monitor.go (about)

     1  package taskmonitor
     2  
     3  import (
     4  	"time"
     5  
     6  	F "github.com/sagernet/sing/common/format"
     7  	"github.com/sagernet/sing/common/logger"
     8  )
     9  
    10  type Monitor struct {
    11  	logger  logger.Logger
    12  	timeout time.Duration
    13  	timer   *time.Timer
    14  }
    15  
    16  func New(logger logger.Logger, timeout time.Duration) *Monitor {
    17  	return &Monitor{
    18  		logger:  logger,
    19  		timeout: timeout,
    20  	}
    21  }
    22  
    23  func (m *Monitor) Start(taskName ...any) {
    24  	m.timer = time.AfterFunc(m.timeout, func() {
    25  		m.logger.Warn(F.ToString(taskName...), " take too much time to finish!")
    26  	})
    27  }
    28  
    29  func (m *Monitor) Finish() {
    30  	m.timer.Stop()
    31  }