github.com/sagernet/sing-box@v1.9.0-rc.20/experimental/libbox/service_pause.go (about)

     1  package libbox
     2  
     3  import (
     4  	"sync"
     5  	"time"
     6  )
     7  
     8  type servicePauseFields struct {
     9  	pauseAccess sync.Mutex
    10  	pauseTimer  *time.Timer
    11  }
    12  
    13  func (s *BoxService) Pause() {
    14  	s.pauseAccess.Lock()
    15  	defer s.pauseAccess.Unlock()
    16  	if s.pauseTimer != nil {
    17  		s.pauseTimer.Stop()
    18  	}
    19  	s.pauseTimer = time.AfterFunc(time.Minute, s.pause)
    20  }
    21  
    22  func (s *BoxService) pause() {
    23  	s.pauseAccess.Lock()
    24  	defer s.pauseAccess.Unlock()
    25  	s.pauseManager.DevicePause()
    26  	_ = s.instance.Router().ResetNetwork()
    27  	s.pauseTimer = nil
    28  }
    29  
    30  func (s *BoxService) Wake() {
    31  	_ = s.instance.Router().ResetNetwork()
    32  	s.pauseAccess.Lock()
    33  	defer s.pauseAccess.Unlock()
    34  	if s.pauseTimer != nil {
    35  		s.pauseTimer.Stop()
    36  		s.pauseTimer = nil
    37  		return
    38  	}
    39  	s.pauseManager.DeviceWake()
    40  }