diff options
author | Jason A. Donenfeld <Jason@zx2c4.com> | 2019-10-01 13:59:21 +0200 |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2019-10-01 13:59:42 +0200 |
commit | 82fca19c7968e4b6f85f3b2a854055b16a277202 (patch) | |
tree | 203291edbce9fdef744c1786e091c1e7fe7dcd72 | |
parent | tunnel: windows does not always add/remove routes with up/down interface (diff) | |
download | wireguard-windows-82fca19c7968e4b6f85f3b2a854055b16a277202.tar.xz wireguard-windows-82fca19c7968e4b6f85f3b2a854055b16a277202.zip |
tunnel: smooth bursts from windows network notifiers
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
-rw-r--r-- | tunnel/defaultroutemonitor.go | 30 |
1 files changed, 25 insertions, 5 deletions
diff --git a/tunnel/defaultroutemonitor.go b/tunnel/defaultroutemonitor.go index f9c63e56..2d63e5db 100644 --- a/tunnel/defaultroutemonitor.go +++ b/tunnel/defaultroutemonitor.go @@ -8,6 +8,7 @@ package tunnel import ( "log" "sync" + "time" "golang.org/x/sys/windows" "golang.zx2c4.com/wireguard/device" @@ -64,10 +65,7 @@ func monitorDefaultRoutes(family winipcfg.AddressFamily, device *device.Device, lastLUID := winipcfg.LUID(0) lastIndex := uint32(0) lastMTU := uint32(0) - mutex := sync.Mutex{} doIt := func() error { - mutex.Lock() - defer mutex.Unlock() err := bindSocketRoute(family, device, ourLUID, &lastLUID, &lastIndex) if err != nil { return err @@ -107,9 +105,31 @@ func monitorDefaultRoutes(family winipcfg.AddressFamily, device *device.Device, if err != nil { return nil, err } + + firstBurst := time.Time{} + burstMutex := sync.Mutex{} + burstTimer := time.AfterFunc(time.Hour*200, func() { + burstMutex.Lock() + firstBurst = time.Time{} + doIt() + burstMutex.Unlock() + }) + burstTimer.Stop() + bump := func() { + burstMutex.Lock() + burstTimer.Reset(time.Millisecond * 150) + if firstBurst.IsZero() { + firstBurst = time.Now() + } else if time.Since(firstBurst) > time.Second*2 { + firstBurst = time.Time{} + doIt() + } + burstMutex.Unlock() + } + cbr, err := winipcfg.RegisterRouteChangeCallback(func(notificationType winipcfg.MibNotificationType, route *winipcfg.MibIPforwardRow2) { if route != nil && route.DestinationPrefix.PrefixLength == 0 { - doIt() + bump() } }) if err != nil { @@ -117,7 +137,7 @@ func monitorDefaultRoutes(family winipcfg.AddressFamily, device *device.Device, } cbi, err := winipcfg.RegisterInterfaceChangeCallback(func(notificationType winipcfg.MibNotificationType, iface *winipcfg.MibIPInterfaceRow) { if notificationType == winipcfg.MibParameterNotification { - doIt() + bump() } }) if err != nil { |