3ec5be3f51
This file was never truly necessary and has never actually been used in the history of Tailscale's open source releases. A Brief History of AUTHORS files --- The AUTHORS file was a pattern developed at Google, originally for Chromium, then adopted by Go and a bunch of other projects. The problem was that Chromium originally had a copyright line only recognizing Google as the copyright holder. Because Google (and most open source projects) do not require copyright assignemnt for contributions, each contributor maintains their copyright. Some large corporate contributors then tried to add their own name to the copyright line in the LICENSE file or in file headers. This quickly becomes unwieldy, and puts a tremendous burden on anyone building on top of Chromium, since the license requires that they keep all copyright lines intact. The compromise was to create an AUTHORS file that would list all of the copyright holders. The LICENSE file and source file headers would then include that list by reference, listing the copyright holder as "The Chromium Authors". This also become cumbersome to simply keep the file up to date with a high rate of new contributors. Plus it's not always obvious who the copyright holder is. Sometimes it is the individual making the contribution, but many times it may be their employer. There is no way for the proejct maintainer to know. Eventually, Google changed their policy to no longer recommend trying to keep the AUTHORS file up to date proactively, and instead to only add to it when requested: https://opensource.google/docs/releasing/authors. They are also clear that: > Adding contributors to the AUTHORS file is entirely within the > project's discretion and has no implications for copyright ownership. It was primarily added to appease a small number of large contributors that insisted that they be recognized as copyright holders (which was entirely their right to do). But it's not truly necessary, and not even the most accurate way of identifying contributors and/or copyright holders. In practice, we've never added anyone to our AUTHORS file. It only lists Tailscale, so it's not really serving any purpose. It also causes confusion because Tailscalars put the "Tailscale Inc & AUTHORS" header in other open source repos which don't actually have an AUTHORS file, so it's ambiguous what that means. Instead, we just acknowledge that the contributors to Tailscale (whoever they are) are copyright holders for their individual contributions. We also have the benefit of using the DCO (developercertificate.org) which provides some additional certification of their right to make the contribution. The source file changes were purely mechanical with: git ls-files | xargs sed -i -e 's/\(Tailscale Inc &\) AUTHORS/\1 contributors/g' Updates #cleanup Change-Id: Ia101a4a3005adb9118051b3416f5a64a4a45987d Signed-off-by: Will Norris <will@tailscale.com>
162 lines
4.4 KiB
Go
162 lines
4.4 KiB
Go
// Copyright (c) Tailscale Inc & contributors
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
// Package lazy provides types for lazily initialized values.
|
|
package lazy
|
|
|
|
import (
|
|
"sync"
|
|
"sync/atomic"
|
|
|
|
"tailscale.com/types/ptr"
|
|
)
|
|
|
|
// nilErrPtr is a sentinel *error value for SyncValue.err to signal
|
|
// that SyncValue.v is valid.
|
|
var nilErrPtr = ptr.To[error](nil)
|
|
|
|
// SyncValue is a lazily computed value.
|
|
//
|
|
// Use either Get or GetErr, depending on whether your fill function returns an
|
|
// error.
|
|
//
|
|
// Recursive use of a SyncValue from its own fill function will deadlock.
|
|
//
|
|
// SyncValue is safe for concurrent use.
|
|
//
|
|
// Unlike [sync.OnceValue], the linker can do better dead code elimination
|
|
// with SyncValue. See https://github.com/golang/go/issues/62202.
|
|
type SyncValue[T any] struct {
|
|
once sync.Once
|
|
v T
|
|
|
|
// err is either:
|
|
// * nil, if not yet computed
|
|
// * nilErrPtr, if completed and nil
|
|
// * non-nil and not nilErrPtr on error.
|
|
//
|
|
// It is an atomic.Pointer so it can be read outside of the sync.Once.Do.
|
|
//
|
|
// Writes to err must happen after a write to v so a caller seeing a non-nil
|
|
// err can safely read v.
|
|
err atomic.Pointer[error]
|
|
}
|
|
|
|
// Set attempts to set z's value to val, and reports whether it succeeded.
|
|
// Set only succeeds if none of Get/GetErr/Set have been called before.
|
|
func (z *SyncValue[T]) Set(val T) bool {
|
|
var wasSet bool
|
|
z.once.Do(func() {
|
|
z.v = val
|
|
z.err.Store(nilErrPtr) // after write to z.v; see docs
|
|
wasSet = true
|
|
})
|
|
return wasSet
|
|
}
|
|
|
|
// MustSet sets z's value to val, or panics if z already has a value.
|
|
func (z *SyncValue[T]) MustSet(val T) {
|
|
if !z.Set(val) {
|
|
panic("Set after already filled")
|
|
}
|
|
}
|
|
|
|
// Get returns z's value, calling fill to compute it if necessary.
|
|
// f is called at most once.
|
|
func (z *SyncValue[T]) Get(fill func() T) T {
|
|
z.once.Do(func() {
|
|
z.v = fill()
|
|
z.err.Store(nilErrPtr) // after write to z.v; see docs
|
|
})
|
|
return z.v
|
|
}
|
|
|
|
// GetErr returns z's value, calling fill to compute it if necessary.
|
|
// f is called at most once, and z remembers both of fill's outputs.
|
|
func (z *SyncValue[T]) GetErr(fill func() (T, error)) (T, error) {
|
|
z.once.Do(func() {
|
|
var err error
|
|
z.v, err = fill()
|
|
|
|
// Update z.err after z.v; see field docs.
|
|
if err != nil {
|
|
z.err.Store(ptr.To(err))
|
|
} else {
|
|
z.err.Store(nilErrPtr)
|
|
}
|
|
})
|
|
return z.v, *z.err.Load()
|
|
}
|
|
|
|
// Peek returns z's value and a boolean indicating whether the value has been
|
|
// set successfully. If a value has not been set, the zero value of T is
|
|
// returned.
|
|
//
|
|
// This function is safe to call concurrently with Get/GetErr/Set, but it's
|
|
// undefined whether a value set by a concurrent call will be visible to Peek.
|
|
//
|
|
// To get any error that's been set, use PeekErr.
|
|
//
|
|
// If GetErr's fill function returned a valid T and an non-nil error, Peek
|
|
// discards that valid T value. PeekErr returns both.
|
|
func (z *SyncValue[T]) Peek() (v T, ok bool) {
|
|
if z.err.Load() == nilErrPtr {
|
|
return z.v, true
|
|
}
|
|
var zero T
|
|
return zero, false
|
|
}
|
|
|
|
// PeekErr returns z's value and error and a boolean indicating whether the
|
|
// value or error has been set. If ok is false, T and err are the zero value.
|
|
//
|
|
// This function is safe to call concurrently with Get/GetErr/Set, but it's
|
|
// undefined whether a value set by a concurrent call will be visible to Peek.
|
|
//
|
|
// Unlike Peek, PeekErr reports ok if either v or err has been set, not just v,
|
|
// and returns both the T and err returned by GetErr's fill function.
|
|
func (z *SyncValue[T]) PeekErr() (v T, err error, ok bool) {
|
|
if e := z.err.Load(); e != nil {
|
|
return z.v, *e, true
|
|
}
|
|
var zero T
|
|
return zero, nil, false
|
|
}
|
|
|
|
// testing_TB is a subset of testing.TB that we use to set up test helpers.
|
|
// It's defined here to avoid pulling in the testing package.
|
|
type testing_TB interface {
|
|
Helper()
|
|
Cleanup(func())
|
|
}
|
|
|
|
// SetForTest sets z's value and error.
|
|
// It's used in tests only and reverts z's state back when tb and all its
|
|
// subtests complete.
|
|
// It is not safe for concurrent use and must not be called concurrently with
|
|
// any SyncValue methods, including another call to itself.
|
|
//
|
|
// The provided tb should be a [*testing.T] or [*testing.B].
|
|
func (z *SyncValue[T]) SetForTest(tb testing_TB, val T, err error) {
|
|
tb.Helper()
|
|
|
|
oldErr, oldVal := z.err.Load(), z.v
|
|
z.once.Do(func() {})
|
|
|
|
z.v = val
|
|
if err != nil {
|
|
z.err.Store(ptr.To(err))
|
|
} else {
|
|
z.err.Store(nilErrPtr)
|
|
}
|
|
|
|
tb.Cleanup(func() {
|
|
if oldErr == nil {
|
|
*z = SyncValue[T]{}
|
|
} else {
|
|
z.v = oldVal
|
|
z.err.Store(oldErr)
|
|
}
|
|
})
|
|
}
|