Update
This commit is contained in:
1 parent
3530afb854
commit
6903fcff27
2146 files changed
+112256
-190703
No files matched your search
+48
-3
@@ -1,4 +1,4 @@
|
||||
// Copyright (c) Tailscale Inc & AUTHORS
|
||||
// Copyright (c) Tailscale Inc & contributors
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
// Package feature tracks which features are linked into the binary.
|
||||
@@ -21,12 +21,43 @@ var in = map[string]bool{}
|
||||
// not accessed concurrently with calls to Register.
|
||||
func Registered() map[string]bool { return in }
|
||||
|
||||
// Register notes that the named feature is linked into the binary.
|
||||
func Register(name string) {
|
||||
// IsRegistered reports whether the named feature package's init
|
||||
// function has run and registered itself via [Register] in this
|
||||
// binary. It is distinct from the compile-time [buildfeatures]
|
||||
// constants: a feature package can be present in the binary but not
|
||||
// imported (e.g. tsnet deliberately does not import many features),
|
||||
// in which case its init does not run.
|
||||
func IsRegistered(name string) bool { return in[name] }
|
||||
|
||||
// Register notes that the named feature is linked into the binary and
|
||||
// reports whether it should initialize itself. It reports false if the
|
||||
// feature was disabled via the TS_DISABLE_FEATURE environment variable,
|
||||
// in which case the feature is not recorded as registered and the
|
||||
// caller should skip the rest of its registration, such as setting
|
||||
// hooks and registering extensions and handlers. The typical use is at
|
||||
// the top of a feature package's init:
|
||||
//
|
||||
// func init() {
|
||||
// if !feature.Register("foo") {
|
||||
// return
|
||||
// }
|
||||
// // ... set hooks, register extensions and handlers ...
|
||||
// }
|
||||
//
|
||||
// A feature package with more than one init that registers things puts
|
||||
// this gate in one of them and guards the others with [Disabled], or
|
||||
// better, consolidates them into a single init.
|
||||
//
|
||||
// Register panics if the feature is already registered.
|
||||
func Register(name string) bool {
|
||||
if Disabled(name) {
|
||||
return false
|
||||
}
|
||||
if _, ok := in[name]; ok {
|
||||
panic("duplicate feature registration for " + name)
|
||||
}
|
||||
in[name] = true
|
||||
return true
|
||||
}
|
||||
|
||||
// Hook is a func that can only be set once.
|
||||
@@ -46,7 +77,15 @@ func (h *Hook[Func]) IsSet() bool {
|
||||
// or f is the zero value.
|
||||
//
|
||||
// It's meant to be called in init.
|
||||
//
|
||||
// As a backstop for feature packages that forget to consult
|
||||
// [Register], if the caller is a package under tailscale.com/feature/
|
||||
// whose feature was disabled via TS_DISABLE_FEATURE, the call is
|
||||
// silently ignored and the hook is left unset.
|
||||
func (h *Hook[Func]) Set(f Func) {
|
||||
if callerFeatureDisabled() {
|
||||
return
|
||||
}
|
||||
if h.ok {
|
||||
panic("Set on already-set feature hook")
|
||||
}
|
||||
@@ -102,7 +141,13 @@ type Hooks[Func any] []Func
|
||||
// Add should only be called during early program
|
||||
// startup before Tailscale has started.
|
||||
// It is not safe for concurrent use.
|
||||
//
|
||||
// Like [Hook.Set], it is silently ignored if the calling feature
|
||||
// package was disabled via TS_DISABLE_FEATURE.
|
||||
func (h *Hooks[Func]) Add(f Func) {
|
||||
if callerFeatureDisabled() {
|
||||
return
|
||||
}
|
||||
if reflect.ValueOf(f).IsZero() {
|
||||
panic("Add with zero value")
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user