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

Commit

Permalink
Reset crane update hook when stopping SPN
Browse files Browse the repository at this point in the history
  • Loading branch information
dhaavi committed Mar 24, 2022
1 parent 9a987a6 commit 9e2deb2
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
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
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 9e2deb2

Please sign in to comment.