Skip to content

Commit

Permalink
fix: reorganize the job actual ip related functions
Browse files Browse the repository at this point in the history
  • Loading branch information
gbaranski committed Jan 15, 2025
1 parent c33d1f5 commit 8425660
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 62 deletions.
70 changes: 56 additions & 14 deletions daemon/job_actual_ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"time"

"github.com/NordSecurity/nordvpn-linux/core"
"github.com/NordSecurity/nordvpn-linux/daemon/state"
"github.com/NordSecurity/nordvpn-linux/events"
"github.com/NordSecurity/nordvpn-linux/internal"
"github.com/NordSecurity/nordvpn-linux/network"
)
Expand Down Expand Up @@ -64,25 +66,65 @@ func insightsIPUntilSuccess(ctx context.Context, api core.InsightsAPI, backoff f
}
}

func JobActualIP(dm *DataManager, api core.InsightsAPI) func(context.Context, bool) error {
return func(ctx context.Context, isConnected bool) error {
var newIP netip.Addr
defer func() {
dm.SetActualIP(newIP)
}()
func updateActualIP(dm *DataManager, api core.InsightsAPI, ctx context.Context, isConnected bool) error {
var newIP netip.Addr
defer func() {
dm.SetActualIP(newIP)
}()

if !isConnected {
return nil
}
if !isConnected {
return nil
}

insightsIP, err := insightsIPUntilSuccess(ctx, api, network.ExponentialBackoff)
insightsIP, err := insightsIPUntilSuccess(ctx, api, network.ExponentialBackoff)
if err != nil {
return err
}
if insightsIP.IsValid() {
newIP = insightsIP
}

return nil
}

// JobActualIP is a long-running job that will update the actual IP address indefinitely
// it reacts to state updates from the statePublisher
func JobActualIP(statePublisher *state.StatePublisher, dm *DataManager, api core.InsightsAPI) {
call := func(ctx context.Context, isConnected bool) {
err := updateActualIP(dm, api, ctx, isConnected)
if err != nil {
return err
if err == context.Canceled {
return
}
log.Println(internal.ErrorPrefix, "actual ip job error: ", err)
}
if insightsIP.IsValid() {
newIP = insightsIP
}

stateChan, _ := statePublisher.AddSubscriber()
var cancel context.CancelFunc

for ev := range stateChan {
_, isConnect := ev.(events.DataConnect)
_, isDisconnect := ev.(events.DataDisconnect)

if isConnect || isDisconnect {
if cancel != nil {
cancel()
}

var ctx context.Context
ctx, cancel = context.WithCancel(context.Background())

if isConnect {
go call(ctx, true)
} else {
call(ctx, false) // should finish immediately, that's why it's not a separate goroutine
}
}
}

return nil
// Ensure the context is canceled when the loop exits
if cancel != nil {
cancel()
}
}
9 changes: 2 additions & 7 deletions daemon/job_actual_ip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ import (
"github.com/stretchr/testify/assert"
)

// func TestJobActualIP(t *testing.T) {

// }

type mockInsights struct {
insightsFunc insightFunc
}
Expand Down Expand Up @@ -130,7 +126,7 @@ func TestInsightsIPUntilSuccess(t *testing.T) {
}
}

func TestJobActualIP(t *testing.T) {
func TestUpdateActualIP(t *testing.T) {
tests := []struct {
name string
isConnected bool
Expand Down Expand Up @@ -177,8 +173,7 @@ func TestJobActualIP(t *testing.T) {
insightsFunc: tt.insightsAPI,
}

job := JobActualIP(dm, api)
err := job(ctx, tt.isConnected)
err := updateActualIP(dm, api, ctx, tt.isConnected)

assert.ErrorIs(t, err, tt.expectedErr)

Expand Down
42 changes: 1 addition & 41 deletions daemon/jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,47 +75,7 @@ func (r *RPC) StartJobs(
}
}

actualIP := JobActualIP(r.dm, r.api)

go func() {
call := func(ctx context.Context, isConnected bool) {
err := actualIP(ctx, isConnected)
if err != nil {
if err == context.Canceled {
return
}
log.Println(internal.ErrorPrefix, "actual ip job error: ", err)
}
}

stateChan, _ := statePublisher.AddSubscriber()
var cancel context.CancelFunc

for ev := range stateChan {
_, isConnect := ev.(events.DataConnect)
_, isDisconnect := ev.(events.DataDisconnect)

if isConnect || isDisconnect {
if cancel != nil {
cancel()
}

var ctx context.Context
ctx, cancel = context.WithCancel(context.Background())

if isConnect {
go call(ctx, true)
} else {
call(ctx, false) // should finish immediately, that's why it's not a separate goroutine
}
}
}

// Ensure the context is canceled when the loop exits
if cancel != nil {
cancel()
}
}()
go JobActualIP(statePublisher, r.dm, r.api)

go func() {
stateChan, _ := statePublisher.AddSubscriber()
Expand Down

0 comments on commit 8425660

Please sign in to comment.