mirror of
https://github.com/tailscale/tailscale.git
synced 2025-08-14 06:57:31 +00:00
ipn/ipnext: remove some interface indirection to add hooks
Now that 25c4dc5fd7
removed unregistering hooks and made them into
slices, just expose the slices and remove the setter funcs.
This removes boilerplate ceremony around adding new hooks.
This does export the hooks and make them mutable at runtime in theory,
but that'd be a data race. If we really wanted to lock it down in the
future we could make the feature.Hooks slice type be an opaque struct
with an All() iterator and a "frozen" bool and we could freeze all the
hooks after init. But that doesn't seem worth it.
This means that hook registration is also now all in one place, rather
than being mixed into ProfilesService vs ipnext.Host vs FooService vs
BarService. I view that as a feature. When we have a ton of hooks and
the list is long, then we can rearrange the fields in the Hooks struct
as needed, or make sub-structs, or big comments.
Updates #12614
Change-Id: I05ce5baa45a61e79c04591c2043c05f3288d8587
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:

committed by
Brad Fitzpatrick

parent
3d8533b5d0
commit
3bc10ea585
@@ -52,3 +52,21 @@ func (h *Hook[Func]) Get() Func {
|
||||
}
|
||||
return h.f
|
||||
}
|
||||
|
||||
// Hooks is a slice of funcs.
|
||||
//
|
||||
// As opposed to a single Hook, this is meant to be used when
|
||||
// multiple parties are able to install the same hook.
|
||||
type Hooks[Func any] []Func
|
||||
|
||||
// Add adds a hook to the list of hooks.
|
||||
//
|
||||
// Add should only be called during early program
|
||||
// startup before Tailscale has started.
|
||||
// It is not safe for concurrent use.
|
||||
func (h *Hooks[Func]) Add(f Func) {
|
||||
if reflect.ValueOf(f).IsZero() {
|
||||
panic("Add with zero value")
|
||||
}
|
||||
*h = append(*h, f)
|
||||
}
|
||||
|
@@ -71,7 +71,7 @@ func (e *extension) Name() string {
|
||||
func (e *extension) Init(host ipnext.Host) error {
|
||||
profile, prefs := host.Profiles().CurrentProfileState()
|
||||
e.profileStateChanged(profile, prefs, false)
|
||||
host.Profiles().RegisterProfileStateChangeCallback(e.profileStateChanged)
|
||||
host.Hooks().ProfileStateChange.Add(e.profileStateChanged)
|
||||
// TODO(jwhited): callback for netmap/nodeattr changes (e.hasNodeAttrRelayServer)
|
||||
return nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user