net/dns: close idle DoH connections when entering sleep mode

Updates tailscale/tailscale#3363
Updates tailscale/tailscale#6148

Provides a facility for the iOS code (and later Android) to signal the beginning of device-wide sleep mode to the LocalBackend, and wires it up to the DNS forwarder, to early-close any open DoH connections when the device is about to enter sleep mode (we don't want a single TCP keepalive to wake up the device again seconds later).

Signed-off-by: Andrea Gottardo <andrea@gottardo.me>
This commit is contained in:
Andrea Gottardo 2024-10-03 13:30:07 -07:00
parent 9bd158cc09
commit c751a21876
4 changed files with 39 additions and 0 deletions

View File

@ -2146,6 +2146,17 @@ func (b *LocalBackend) Start(opts ipn.Options) error {
return nil
}
// OnSleepChange is invoked by the system when transitioning into or out of sleep mode.
// This function is used to pause or resume non-essential network activity during sleep,
// with an eye towards power savings.
func (b *LocalBackend) OnSleepChange(isSleeping bool) {
b.logf("OnSleepChange(isSleeping=%v)", isSleeping)
dnsManager, ok := b.sys.DNSManager.GetOK()
if ok {
dnsManager.OnSleepChange(isSleeping)
}
}
// invalidPacketFilterWarnable is a Warnable to warn the user that the control server sent an invalid packet filter.
var invalidPacketFilterWarnable = health.Register(&health.Warnable{
Code: "invalid-packet-filter",

View File

@ -127,6 +127,14 @@ func (m *Manager) GetBaseConfig() (OSConfig, error) {
return m.os.GetBaseConfig()
}
// OnSleepChange is called by the backend when the device enters or leaves sleep mode.
// We use this to trigger behaviors needed to provide battery savings.
func (m *Manager) OnSleepChange(isSleeping bool) {
if isSleeping {
m.resolver.OnSleepChange(isSleeping)
}
}
// setLocked sets the DNS configuration.
//
// m.mu must be held.

View File

@ -273,6 +273,18 @@ func (f *forwarder) Close() error {
return nil
}
// CloseIdleConnections closes any idle connections to the upstream
// DoH servers. It is desirable to call this when the device enters
// sleep mode, when we know that no DNS queries will be made for a
// while, so that we can preserve battery life.
func (f *forwarder) CloseIdleConnections() {
f.mu.Lock()
defer f.mu.Unlock()
for _, c := range f.dohClient {
c.Transport.(*http.Transport).CloseIdleConnections()
}
}
// resolversWithDelays maps from a set of DNS server names to a slice of a type
// that included a startDelay, upgrading any well-known DoH (DNS-over-HTTP)
// servers in the process, insert a DoH lookup first before UDP fallbacks.

View File

@ -260,6 +260,14 @@ func (r *Resolver) SetMissingUpstreamRecovery(f func()) {
r.forwarder.missingUpstreamRecovery = f
}
// OnSleepChange asks the forwarder to close any idle connections to the upstream
// DoH servers when the device enters sleep mode.
func (r *Resolver) OnSleepChange(isSleeping bool) {
if isSleeping {
r.forwarder.CloseIdleConnections()
}
}
func (r *Resolver) TestOnlySetHook(hook func(Config)) { r.saveConfigForTests = hook }
func (r *Resolver) SetConfig(cfg Config) error {