Skip to content
This repository has been archived by the owner on Mar 29, 2024. It is now read-only.

Commit

Permalink
Merge pull request #64 from safing/fix/spn-client-lifecycle-stopping
Browse files Browse the repository at this point in the history
Stop SPN client manager correctly when not yet connected
  • Loading branch information
dhaavi authored Mar 24, 2022
2 parents 6f80dd2 + 9e2deb2 commit 10cd33b
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 13 deletions.
22 changes: 19 additions & 3 deletions captain/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,22 @@ func clientManager(ctx context.Context) error {
// redirected to a closed port.
// We also can't add the nameserver as a module dependency, as the nameserver
// is not part of the server.
time.Sleep(1 * time.Second)
select {
case <-time.After(1 * time.Second):
case <-ctx.Done():
return nil
}

reconnect:
for {
// Check if we are shutting down.
select {
case <-ctx.Done():
return nil
default:
}

// Reset SPN status.
if ready.SetToIf(true, false) {
netenv.ConnectedToSPN.UnSet()
log.Info("spn/captain: client not ready")
Expand All @@ -125,8 +137,12 @@ reconnect:
// Continue
case clientResultRetry, clientResultReconnect:
// Wait for a short time to not loop too quickly.
time.Sleep(clientRetryConnectBackoffDuration)
continue reconnect
select {
case <-time.After(clientRetryConnectBackoffDuration):
continue reconnect
case <-ctx.Done():
return nil
}
case clientResultShutdown:
return nil
}
Expand Down
6 changes: 5 additions & 1 deletion captain/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ import (
"github.com/safing/spn/docks"
)

func initDockHooks() {
func startDockHooks() {
docks.RegisterCraneUpdateHook(handleCraneUpdate)
}

func stopDockHooks() {
docks.ResetCraneUpdateHook()
}

func handleCraneUpdate(crane *docks.Crane) {
if conf.Client() && crane.Controller.Abandoning.IsSet() {
// Check connection to home hub.
Expand Down
7 changes: 5 additions & 2 deletions captain/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ func start() error {
crew.EnableConnecting(publicIdentity.Hub)
}

// Subscribe to updates of connections.
initDockHooks()
// Subscribe to updates of cranes.
startDockHooks()

// bootstrapping
if err := processBootstrapHubFlag(); err != nil {
Expand Down Expand Up @@ -129,6 +129,9 @@ func stop() error {
// Reset intel resource so that it is loaded again when starting.
resetSPNIntel()

// Unregister crane update hook.
stopDockHooks()

// Send shutdown status message.
if conf.PublicHub() {
publishShutdownStatus()
Expand Down
1 change: 1 addition & 0 deletions captain/navigation.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ func connectToHomeHub(ctx context.Context, dst *hub.Hub) error {
select {
case <-gossipQuery.ctx.Done():
case <-ctx.Done():
return context.Canceled
}

// Create communication terminal.
Expand Down
26 changes: 19 additions & 7 deletions docks/cranehooks.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,42 @@
package docks

import (
"github.com/tevino/abool"
"sync"

"github.com/safing/portbase/log"
)

var (
craneUpdateHook func(crane *Crane)
craneUpdateHookEnabled = abool.New()
craneUpdateHookActive = abool.New()
craneUpdateHook func(crane *Crane)
craneUpdateHookLock sync.Mutex
)

// RegisterCraneUpdateHook allows the captain to hook into receiving updates for cranes.
func RegisterCraneUpdateHook(fn func(crane *Crane)) {
if craneUpdateHookEnabled.SetToIf(false, true) {
craneUpdateHookLock.Lock()
defer craneUpdateHookLock.Unlock()

if craneUpdateHook == nil {
craneUpdateHook = fn
craneUpdateHookActive.Set()
} else {
log.Error("spn/docks: crane update hook already registered")
}
}

// ResetCraneUpdateHook resets the hook for receiving updates for cranes.
func ResetCraneUpdateHook() {
craneUpdateHookLock.Lock()
defer craneUpdateHookLock.Unlock()

craneUpdateHook = nil
}

// NotifyUpdate calls the registers crane update hook function.
func (crane *Crane) NotifyUpdate() {
if craneUpdateHookActive.IsSet() {
craneUpdateHookLock.Lock()
defer craneUpdateHookLock.Unlock()

if craneUpdateHook != nil {
craneUpdateHook(crane)
}
}

0 comments on commit 10cd33b

Please sign in to comment.