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/eventbus
M. J. Fromberger ca9d795006
util/eventbus: add a Monitor type to manage subscriber goroutines (#17127)
A common pattern in event bus usage is to run a goroutine to service a
collection of subscribers on a single bus client. To have an orderly shutdown,
however, we need a way to wait for such a goroutine to be finished.

This commit adds a Monitor type that makes this pattern easier to wire up:
rather than having to track all the subscribers and an extra channel, the
component need only track the client and the monitor.  For example:

   cli := bus.Client("example")
   m := cli.Monitor(func(c *eventbus.Client) {
     s1 := eventbus.Subscribe[T](cli)
     s2 := eventbus.Subscribe[U](cli)
     for {
       select {
       case <-c.Done():
         return
       case t := <-s1.Events():
          processT(t)
       case u := <-s2.Events():
          processU(u)
       }
     }
   })

To shut down the client and wait for the goroutine, the caller can write:

   m.Close()

which closes cli and waits for the goroutine to finish. Or, separately:

   cli.Close()
   // do other stuff
   m.Wait()

While the goroutine management is not explicitly tied to subscriptions, it is a
common enough pattern that this seems like a useful simplification in use.

Updates #15160

Change-Id: I657afda1cfaf03465a9dce1336e9fd518a968bca
Signed-off-by: M. J. Fromberger <fromberger@tailscale.com>
3 months ago
..
assets util/eventbus: add a debug HTTP handler for the bus 9 months ago
debug-demo util/eventbus: add a helper program for bus development 9 months ago
eventbustest health: remove direct callback and replace with eventbus (#17199) 3 months ago
bench_test.go util/eventbus: add basic throughput benchmarks (#15284) 9 months ago
bus.go util/eventbus: fix docstrings (#16401) 5 months ago
bus_test.go util/eventbus: add a Monitor type to manage subscriber goroutines (#17127) 3 months ago
client.go util/eventbus: add a Done channel to the Client (#17118) 3 months ago
debug.go client/local,cmd/tailscale/cli,ipn/localapi: expose eventbus graph (#16597) 5 months ago
debughttp.go util/eventbus: add ts_omit_debugeventbus 3 months ago
debughttp_off.go util/eventbus: add ts_omit_debugeventbus 3 months ago
doc.go util/eventbus: add test helpers to simplify testing events (#16294) 5 months ago
fetch-htmx.go util/eventbus: add a debug HTTP handler for the bus 9 months ago
monitor.go util/eventbus: add a Monitor type to manage subscriber goroutines (#17127) 3 months ago
publish.go util/eventbus: remove redundant code from eventbus.Publish 6 months ago
queue.go util/eventbus: make internal queue a generic type 9 months ago
subscribe.go util/eventbus: remove redundant code from eventbus.Publish 6 months ago