diff --git a/experimental/libbox/command_server.go b/experimental/libbox/command_server.go index 45470b79..a5bd032e 100644 --- a/experimental/libbox/command_server.go +++ b/experimental/libbox/command_server.go @@ -40,8 +40,13 @@ type CommandServer struct { grpcServer *grpc.Server listener net.Listener endPauseTimer *time.Timer + sleepAt time.Time } +// iOS wakes the extension for every push and background task; in collected power reports +// most sleeps last under two minutes and none exceeded ten. +const closeIdleConnectionsAfterSleep = 2 * time.Minute + type CommandServerHandler interface { ServiceStop() error ServiceReload() error @@ -94,7 +99,7 @@ func NewCommandServer(handler CommandServerHandler, platformInterface PlatformIn OOMRecorder: oomRecorder, }) if sPowerReportEnabled { - err := powerManager.Start(PowerReportOptions(server.StartedService, platformInterface)) + err := powerManager.Start(PowerReportOptions(server.StartedService)) if err != nil { log.StdLogger().Error(E.Cause(err, "start power report recorder")) } @@ -256,6 +261,7 @@ func (s *CommandServer) NeedFindProcess() bool { } func (s *CommandServer) Pause() { + s.sleepAt = time.Now().Round(0) recorder := s.powerManager.Recorder() if recorder != nil { recorder.RecordDeviceSleep() @@ -264,7 +270,6 @@ func (s *CommandServer) Pause() { if instance == nil || instance.PauseManager() == nil { return } - instance.Box().CloseIdleConnections() instance.PauseManager().DevicePause() if C.IsIos { // iOS calls wake within seconds of sleep while the device stays locked, so wake is @@ -288,14 +293,18 @@ func (s *CommandServer) endDevicePause() { } func (s *CommandServer) Wake() { + wakeAt := time.Now().Round(0) recorder := s.powerManager.Recorder() if recorder != nil { recorder.RecordDeviceWake() } instance := s.StartedService.Instance() - if instance == nil || instance.PauseManager() == nil { + if instance == nil || instance.Box() == nil || instance.PauseManager() == nil { return } + if !s.sleepAt.IsZero() && wakeAt.Sub(s.sleepAt) >= closeIdleConnectionsAfterSleep { + instance.Box().CloseIdleConnections() + } if !C.IsIos { instance.PauseManager().DeviceWake() }