Add pause support

This commit is contained in:
世界 2023-08-07 20:57:31 +08:00
parent 2835be4411
commit 606102e010
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
4 changed files with 16 additions and 4 deletions

View file

@ -6,11 +6,15 @@
package device
import (
"context"
"runtime"
"sync"
"sync/atomic"
"time"
"github.com/sagernet/sing/service"
"github.com/sagernet/sing/service/pause"
"github.com/sagernet/wireguard-go/conn"
"github.com/sagernet/wireguard-go/ratelimiter"
"github.com/sagernet/wireguard-go/rwcancel"
@ -86,9 +90,10 @@ type Device struct {
mtu atomic.Int32
}
ipcMutex sync.RWMutex
closed chan struct{}
log *Logger
ipcMutex sync.RWMutex
closed chan struct{}
log *Logger
pauseManager pause.Manager
}
// deviceState represents the state of a Device.
@ -281,8 +286,9 @@ func (device *Device) SetPrivateKey(sk NoisePrivateKey) error {
return nil
}
func NewDevice(tunDevice tun.Device, bind conn.Bind, logger *Logger, workers int) *Device {
func NewDevice(ctx context.Context, tunDevice tun.Device, bind conn.Bind, logger *Logger, workers int) *Device {
device := new(Device)
device.pauseManager = service.FromContext[pause.Manager](ctx)
device.state.state.Store(uint32(deviceStateDown))
device.closed = make(chan struct{})
device.log = logger

View file

@ -39,6 +39,9 @@ func (peer *Peer) NewTimer(expirationFunction func(*Peer)) *Timer {
timer.isPending = false
timer.modifyingLock.Unlock()
if pauseManager := peer.device.pauseManager; pauseManager != nil {
pauseManager.WaitActive()
}
expirationFunction(peer)
})
timer.Stop()