Skip to content

Commit

Permalink
Reduce running of background tasks on sleep mode
Browse files Browse the repository at this point in the history
  • Loading branch information
vlabo committed Apr 19, 2023
1 parent 4784799 commit 7e09245
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 9 deletions.
11 changes: 7 additions & 4 deletions netenv/network-change.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,13 @@ serviceLoop:
for {
trigger := false

timeout := 15 * time.Second
if !Online() {
timeout = time.Second
var ticker *time.Ticker
if Online() {
ticker = monitorNetworkChangeOnlineTicker
} else {
ticker = monitorNetworkChangeOfflineTicker
}

// wait for trigger
select {
case <-ctx.Done():
Expand All @@ -54,7 +57,7 @@ serviceLoop:
// triggers the networkChangeCheck this way. If we would set
// trigger == true we would trigger the online check again
// resulting in a loop of pointless checks.
case <-time.After(timeout):
case <-ticker.C:
trigger = true
}

Expand Down
12 changes: 12 additions & 0 deletions netenv/os_android.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,20 @@ package netenv
import (
"github.com/safing/portmaster-android/go/app_interface"
"net"
"time"
)

var (
monitorNetworkChangeOnlineTicker = time.NewTicker(time.Second)
monitorNetworkChangeOfflineTicker = time.NewTicker(time.Second)
)

func init() {
// Network change event is monitored by the android system.
monitorNetworkChangeOnlineTicker.Stop()
monitorNetworkChangeOfflineTicker.Stop()
}

func osGetInterfaceAddrs() ([]net.Addr, error) {
list, err := app_interface.GetNetworkAddresses()
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions netenv/os_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ package netenv

import (
"net"
"time"
)

var (
monitorNetworkChangeOnlineTicker = time.NewTicker(15 * time.Second)
monitorNetworkChangeOfflineTicker = time.NewTicker(time.Second)
)

func osGetInterfaceAddrs() ([]net.Addr, error) {
Expand Down
4 changes: 2 additions & 2 deletions network/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ const (
)

func connectionCleaner(ctx context.Context) error {
ticker := time.NewTicker(cleanerTickDuration)
ticker := module.NewSleepyTicker(cleanerTickDuration, 0)

for {
select {
case <-ctx.Done():
ticker.Stop()
return nil
case <-ticker.C:
case <-ticker.Read():
// clean connections and processes
activePIDs := cleanConnections()
process.CleanProcessStorage(activePIDs)
Expand Down
4 changes: 2 additions & 2 deletions network/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,14 @@ func SaveOpenDNSRequest(q *resolver.Query, rrCache *resolver.RRCache, conn *Conn
}

func openDNSRequestWriter(ctx context.Context) error {
ticker := time.NewTicker(writeOpenDNSRequestsTickDuration)
ticker := module.NewSleepyTicker(writeOpenDNSRequestsTickDuration, 0)
defer ticker.Stop()

for {
select {
case <-ctx.Done():
return nil
case <-ticker.C:
case <-ticker.Read():
writeOpenDNSRequestsToDB()
}
}
Expand Down
6 changes: 5 additions & 1 deletion process/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,13 @@ func (p *Process) Save() {

p.UpdateMeta()

if p.processKey == "" {
p.processKey = getProcessKey(int32(p.Pid), p.CreatedAt)
}

if !p.KeyIsSet() {
// set key
p.SetKey(fmt.Sprintf("%s/%s", processDatabaseNamespace, getProcessKey(int32(p.Pid), p.CreatedAt)))
p.SetKey(fmt.Sprintf("%s/%s", processDatabaseNamespace, p.processKey))

// save
processesLock.Lock()
Expand Down

0 comments on commit 7e09245

Please sign in to comment.