mirror of
https://github.com/tailscale/tailscale.git
synced 2026-02-10 02:02:15 +01:00
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>
346 lines
8.4 KiB
Go
346 lines
8.4 KiB
Go
// Copyright (c) Tailscale Inc & contributors
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
package eventbus
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
"reflect"
|
|
"slices"
|
|
|
|
"tailscale.com/syncs"
|
|
"tailscale.com/types/logger"
|
|
"tailscale.com/util/set"
|
|
)
|
|
|
|
type PublishedEvent struct {
|
|
Event any
|
|
From *Client
|
|
}
|
|
|
|
type RoutedEvent struct {
|
|
Event any
|
|
From *Client
|
|
To []*Client
|
|
}
|
|
|
|
// Bus is an event bus that distributes published events to interested
|
|
// subscribers.
|
|
type Bus struct {
|
|
router *worker
|
|
write chan PublishedEvent
|
|
snapshot chan chan []PublishedEvent
|
|
routeDebug hook[RoutedEvent]
|
|
logf logger.Logf
|
|
|
|
topicsMu syncs.Mutex
|
|
topics map[reflect.Type][]*subscribeState
|
|
|
|
// Used for introspection/debugging only, not in the normal event
|
|
// publishing path.
|
|
clientsMu syncs.Mutex
|
|
clients set.Set[*Client]
|
|
}
|
|
|
|
// New returns a new bus with default options. It is equivalent to
|
|
// calling [NewWithOptions] with zero [BusOptions].
|
|
func New() *Bus { return NewWithOptions(BusOptions{}) }
|
|
|
|
// NewWithOptions returns a new [Bus] with the specified [BusOptions].
|
|
// Use [Bus.Client] to construct clients on the bus.
|
|
// Use [Publish] to make event publishers.
|
|
// Use [Subscribe] and [SubscribeFunc] to make event subscribers.
|
|
func NewWithOptions(opts BusOptions) *Bus {
|
|
ret := &Bus{
|
|
write: make(chan PublishedEvent),
|
|
snapshot: make(chan chan []PublishedEvent),
|
|
topics: map[reflect.Type][]*subscribeState{},
|
|
clients: set.Set[*Client]{},
|
|
logf: opts.logger(),
|
|
}
|
|
ret.router = runWorker(ret.pump)
|
|
return ret
|
|
}
|
|
|
|
// BusOptions are optional parameters for a [Bus]. A zero value is ready for
|
|
// use and provides defaults as described.
|
|
type BusOptions struct {
|
|
// Logf, if non-nil, is used for debug logs emitted by the bus and clients,
|
|
// publishers, and subscribers under its care. If it is nil, logs are sent
|
|
// to [log.Printf].
|
|
Logf logger.Logf
|
|
}
|
|
|
|
func (o BusOptions) logger() logger.Logf {
|
|
if o.Logf == nil {
|
|
return log.Printf
|
|
}
|
|
return o.Logf
|
|
}
|
|
|
|
// Client returns a new client with no subscriptions. Use [Subscribe]
|
|
// to receive events, and [Publish] to emit events.
|
|
//
|
|
// The client's name is used only for debugging, to tell humans what
|
|
// piece of code a publisher/subscriber belongs to. Aim for something
|
|
// short but unique, for example "kernel-route-monitor" or "taildrop",
|
|
// not "watcher".
|
|
func (b *Bus) Client(name string) *Client {
|
|
ret := &Client{
|
|
name: name,
|
|
bus: b,
|
|
pub: set.Set[publisher]{},
|
|
}
|
|
b.clientsMu.Lock()
|
|
defer b.clientsMu.Unlock()
|
|
b.clients.Add(ret)
|
|
return ret
|
|
}
|
|
|
|
// Debugger returns the debugging facility for the bus.
|
|
func (b *Bus) Debugger() *Debugger {
|
|
return &Debugger{b}
|
|
}
|
|
|
|
// Close closes the bus. It implicitly closes all clients, publishers and
|
|
// subscribers attached to the bus.
|
|
//
|
|
// Close blocks until the bus is fully shut down. The bus is
|
|
// permanently unusable after closing.
|
|
func (b *Bus) Close() {
|
|
b.router.StopAndWait()
|
|
|
|
b.clientsMu.Lock()
|
|
defer b.clientsMu.Unlock()
|
|
for c := range b.clients {
|
|
c.Close()
|
|
}
|
|
b.clients = nil
|
|
}
|
|
|
|
func (b *Bus) pump(ctx context.Context) {
|
|
// Limit how many published events we can buffer in the PublishedEvent queue.
|
|
//
|
|
// Subscribers have unbounded DeliveredEvent queues (see tailscale/tailscale#18020),
|
|
// so this queue doesn't need to be unbounded. Keeping it bounded may also help
|
|
// catch cases where subscribers stop pumping events completely, such as due to a bug
|
|
// in [subscribeState.pump], [Subscriber.dispatch], or [SubscriberFunc.dispatch]).
|
|
const maxPublishedEvents = 16
|
|
vals := queue[PublishedEvent]{capacity: maxPublishedEvents}
|
|
acceptCh := func() chan PublishedEvent {
|
|
if vals.Full() {
|
|
return nil
|
|
}
|
|
return b.write
|
|
}
|
|
for {
|
|
// Drain all pending events. Note that while we're draining
|
|
// events into subscriber queues, we continue to
|
|
// opportunistically accept more incoming events, if we have
|
|
// queue space for it.
|
|
for !vals.Empty() {
|
|
val := vals.Peek()
|
|
dests := b.dest(reflect.TypeOf(val.Event))
|
|
|
|
if b.routeDebug.active() {
|
|
clients := make([]*Client, len(dests))
|
|
for i := range len(dests) {
|
|
clients[i] = dests[i].client
|
|
}
|
|
b.routeDebug.run(RoutedEvent{
|
|
Event: val.Event,
|
|
From: val.From,
|
|
To: clients,
|
|
})
|
|
}
|
|
|
|
for _, d := range dests {
|
|
evt := DeliveredEvent{
|
|
Event: val.Event,
|
|
From: val.From,
|
|
To: d.client,
|
|
}
|
|
deliverOne:
|
|
for {
|
|
select {
|
|
case d.write <- evt:
|
|
break deliverOne
|
|
case <-d.closed():
|
|
// Queue closed, don't block but continue
|
|
// delivering to others.
|
|
break deliverOne
|
|
case in := <-acceptCh():
|
|
vals.Add(in)
|
|
in.From.publishDebug.run(in)
|
|
case <-ctx.Done():
|
|
return
|
|
case ch := <-b.snapshot:
|
|
ch <- vals.Snapshot()
|
|
}
|
|
}
|
|
}
|
|
vals.Drop()
|
|
}
|
|
|
|
// Inbound queue empty, wait for at least 1 work item before
|
|
// resuming.
|
|
for vals.Empty() {
|
|
select {
|
|
case <-ctx.Done():
|
|
return
|
|
case in := <-b.write:
|
|
vals.Add(in)
|
|
in.From.publishDebug.run(in)
|
|
case ch := <-b.snapshot:
|
|
ch <- nil
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// logger returns a [logger.Logf] to which logs related to bus activity should be written.
|
|
func (b *Bus) logger() logger.Logf { return b.logf }
|
|
|
|
func (b *Bus) dest(t reflect.Type) []*subscribeState {
|
|
b.topicsMu.Lock()
|
|
defer b.topicsMu.Unlock()
|
|
return b.topics[t]
|
|
}
|
|
|
|
func (b *Bus) shouldPublish(t reflect.Type) bool {
|
|
if b.routeDebug.active() {
|
|
return true
|
|
}
|
|
|
|
b.topicsMu.Lock()
|
|
defer b.topicsMu.Unlock()
|
|
return len(b.topics[t]) > 0
|
|
}
|
|
|
|
func (b *Bus) listClients() []*Client {
|
|
b.clientsMu.Lock()
|
|
defer b.clientsMu.Unlock()
|
|
return b.clients.Slice()
|
|
}
|
|
|
|
func (b *Bus) snapshotPublishQueue() []PublishedEvent {
|
|
resp := make(chan []PublishedEvent)
|
|
select {
|
|
case b.snapshot <- resp:
|
|
return <-resp
|
|
case <-b.router.Done():
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func (b *Bus) subscribe(t reflect.Type, q *subscribeState) (cancel func()) {
|
|
b.topicsMu.Lock()
|
|
defer b.topicsMu.Unlock()
|
|
b.topics[t] = append(b.topics[t], q)
|
|
return func() {
|
|
b.unsubscribe(t, q)
|
|
}
|
|
}
|
|
|
|
func (b *Bus) unsubscribe(t reflect.Type, q *subscribeState) {
|
|
b.topicsMu.Lock()
|
|
defer b.topicsMu.Unlock()
|
|
// Topic slices are accessed by pump without holding a lock, so we
|
|
// have to replace the entire slice when unsubscribing.
|
|
// Unsubscribing should be infrequent enough that this won't
|
|
// matter.
|
|
i := slices.Index(b.topics[t], q)
|
|
if i < 0 {
|
|
return
|
|
}
|
|
b.topics[t] = slices.Delete(slices.Clone(b.topics[t]), i, i+1)
|
|
}
|
|
|
|
// A worker runs a worker goroutine and helps coordinate its shutdown.
|
|
type worker struct {
|
|
ctx context.Context
|
|
stop context.CancelFunc
|
|
stopped chan struct{}
|
|
}
|
|
|
|
// runWorker creates a worker goroutine running fn. The context passed
|
|
// to fn is canceled by [worker.Stop].
|
|
func runWorker(fn func(context.Context)) *worker {
|
|
ctx, stop := context.WithCancel(context.Background())
|
|
ret := &worker{
|
|
ctx: ctx,
|
|
stop: stop,
|
|
stopped: make(chan struct{}),
|
|
}
|
|
go ret.run(fn)
|
|
return ret
|
|
}
|
|
|
|
func (w *worker) run(fn func(context.Context)) {
|
|
defer close(w.stopped)
|
|
fn(w.ctx)
|
|
}
|
|
|
|
// Stop signals the worker goroutine to shut down.
|
|
func (w *worker) Stop() { w.stop() }
|
|
|
|
// Done returns a channel that is closed when the worker goroutine
|
|
// exits.
|
|
func (w *worker) Done() <-chan struct{} { return w.stopped }
|
|
|
|
// Wait waits until the worker goroutine has exited.
|
|
func (w *worker) Wait() { <-w.stopped }
|
|
|
|
// StopAndWait signals the worker goroutine to shut down, then waits
|
|
// for it to exit.
|
|
func (w *worker) StopAndWait() {
|
|
w.stop()
|
|
<-w.stopped
|
|
}
|
|
|
|
// stopFlag is a value that can be watched for a notification. The
|
|
// zero value is ready for use.
|
|
//
|
|
// The flag is notified by running [stopFlag.Stop]. Stop can be called
|
|
// multiple times. Upon the first call to Stop, [stopFlag.Done] is
|
|
// closed, all pending [stopFlag.Wait] calls return, and future Wait
|
|
// calls return immediately.
|
|
//
|
|
// A stopFlag can only notify once, and is intended for use as a
|
|
// one-way shutdown signal that's lighter than a cancellable
|
|
// context.Context.
|
|
type stopFlag struct {
|
|
// guards the lazy construction of stopped, and the value of
|
|
// alreadyStopped.
|
|
mu syncs.Mutex
|
|
stopped chan struct{}
|
|
alreadyStopped bool
|
|
}
|
|
|
|
func (s *stopFlag) Stop() {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
if s.alreadyStopped {
|
|
return
|
|
}
|
|
s.alreadyStopped = true
|
|
if s.stopped == nil {
|
|
s.stopped = make(chan struct{})
|
|
}
|
|
close(s.stopped)
|
|
}
|
|
|
|
func (s *stopFlag) Done() <-chan struct{} {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
if s.stopped == nil {
|
|
s.stopped = make(chan struct{})
|
|
}
|
|
return s.stopped
|
|
}
|
|
|
|
func (s *stopFlag) Wait() {
|
|
<-s.Done()
|
|
}
|