You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
tailscale/util
M. J. Fromberger ad6cf2f8f3
util/eventbus: add a function-based subscriber type (#17432)
Originally proposed by @bradfitz in #17413.

In practice, a lot of subscribers have only one event type of interest, or a
small number of mostly independent ones. In that case, the overhead of running
and maintaining a goroutine to select on multiple channels winds up being more
noisy than we'd like for the user of the API.

For this common case, add a new SubscriberFunc[T] type that delivers events to
a callback owned by the subscriber, directly on the goroutine belonging to the
client itself. This frees the consumer from the need to maintain their own
goroutine to pull events from the channel, and to watch for closure of the
subscriber.

Before:

     s := eventbus.Subscribe[T](eventClient)
     go func() {
       for {
          select {
          case <-s.Done():
            return
          case e := <-s.Events():
            doSomethingWith(e)
          }
       }
     }()
     // ...
     s.Close()

After:

     func doSomethingWithT(e T) { ... }
     s := eventbus.SubscribeFunc(eventClient, doSomethingWithT)
     // ...
     s.Close()

Moreover, unless the caller wants to explicitly stop the subscriber separately
from its governing client, it need not capture the SubscriberFunc value at all.

One downside of this approach is that a slow or deadlocked callback could block
client's service routine and thus stall all other subscriptions on that client,
However, this can already happen more broadly if a subscriber fails to service
its delivery channel in a timely manner, it just feeds back more immediately.

Updates #17487

Change-Id: I64592d786005177aa9fd445c263178ed415784d5
Signed-off-by: M. J. Fromberger <fromberger@tailscale.com>
2 months ago
..
backoff util/backoff: rename logtail/backoff package to util/backoff 2 months ago
cache util/cache: fix missing interface methods (#11275) 2 years ago
checkchange util/checkchange: stop using deephash everywhere 2 months ago
cibuild all: update copyright and license headers 3 years ago
clientmetric feature/featuretags: make clientmetrics optional 2 months ago
cloudenv feature/featuretags, all: add build features, use existing ones in more places 2 months ago
cmpver util/cmpver: add Less/LessEq helper funcs 2 years ago
codegen cmd/viewer, types/views: implement support for json/v2 (#16852) 4 months ago
cstruct all: use Go 1.21's binary.NativeEndian 11 months ago
ctxkey all: use reflect.TypeFor now available in Go 1.22 (#11078) 2 years ago
deephash util/deephash: move tests that depend on other tailscale packages to deephash_test 7 months ago
dirwalk all: use tstest.Replace more 3 years ago
dnsname tailcfg: adjust ServiceName.Validate to use vizerror 10 months ago
eventbus util/eventbus: add a function-based subscriber type (#17432) 2 months ago
execqueue control/controlclient,util/execqueue: extract execqueue into a package 2 years ago
expvarx util/expvarx: deflake TestSafeFuncHappyPath with synctest 3 months ago
goroutines ipn/ipnlocal, util/goroutines: track goroutines for tests, shutdown 11 months ago
groupmember util/groupmember: fail earlier if group doesn't exist, use slices.Contains 2 years ago
hashx all: use Go 1.22 range-over-int 2 years ago
httphdr util/httphdr: add new package for parsing HTTP headers (#9797) 2 years ago
httpm util/httpm: don't run test if .git doesn't exist 2 years ago
limiter all: add test for package comments, fix, add comments as needed 1 year ago
lineiter types/result, util/lineiter: add package for a result type, use it 1 year ago
lineread all: update copyright and license headers 3 years ago
linuxfw all: use Go 1.20's errors.Join instead of our multierr package 2 months ago
lru util/slicesx: add MapKeys and MapValues from golang.org/x/exp/maps 11 months ago
mak util/mak: delete long-deprecated, unused, pre-generics NonNil func 7 months ago
multierr all: use Go 1.22 range-over-int 2 years ago
must util/must: add Get2 for functions that return two values 6 months ago
nocasemaps all: use Go 1.22 range-over-int 2 years ago
osdiag all: add test for package comments, fix, add comments as needed 1 year ago
osshare all: add test for package comments, fix, add comments as needed 1 year ago
osuser ssh/tailssh: add Plan 9 support for Tailscale SSH 8 months ago
pidowner types/result, util/lineiter: add package for a result type, use it 1 year ago
pool util/pool: add package for storing and using a pool of items 2 years ago
precompress all: update copyright and license headers 3 years ago
progresstracking ipn/localapi: add support for multipart POST to file-put 2 years ago
prompt util/prompt: add a default and take default in non-interactive cases 2 months ago
quarantine all: update copyright and license headers 3 years ago
race all: use Go 1.22 range-over-int 2 years ago
racebuild all: update copyright and license headers 3 years ago
rands wgengine/magicsock: use math/rands/v2 2 years ago
reload all: use math/rand/v2 more 2 years ago
ringlog util/ringbuffer: rename to ringlog 3 months ago
set control/controlclient: restore aggressive Direct.Close teardown 2 months ago
singleflight util/singleflight: add DoChanContext 2 years ago
slicesx util/slicesx: add AppendNonzero 11 months ago
stringsx util/stringsx: add package for extra string functions, like CompareFold 12 months ago
syspolicy cmd/tailscaled: default --encrypt-state to true if TPM is available (#17376) 2 months ago
sysresources util/sysresources, magicsock: scale DERP buffer based on system memory 3 years ago
testenv nettest, *: add option to run HTTP tests with in-memory network 8 months ago
topk all: use Go 1.22 range-over-int 2 years ago
truncate util/truncate: support []byte as well (#11614) 2 years ago
usermetric feature/featuretags: make usermetrics modular 2 months ago
vizerror util/vizerror: add WrapWithMessage 1 year ago
winutil all: use Go 1.20's errors.Join instead of our multierr package 2 months ago
zstdframe all: use Go 1.22 range-over-int 2 years ago