mirror of
https://github.com/tailscale/tailscale.git
synced 2025-06-21 15:48:39 +00:00
util/eventbus: remove redundant code from eventbus.Publish
eventbus.Publish() calls newPublisher(), which in turn invokes (*Client).addPublisher(). That method adds the new publisher to c.pub, so we don’t need to add it again in eventbus.Publish. Updates #cleanup Signed-off-by: Nick Khyl <nickk@tailscale.com>
This commit is contained in:
parent
5b7cf7fc36
commit
866614202c
@ -113,15 +113,16 @@ func (c *Client) shouldPublish(t reflect.Type) bool {
|
|||||||
// Subscribe requests delivery of events of type T through the given
|
// Subscribe requests delivery of events of type T through the given
|
||||||
// Queue. Panics if the queue already has a subscriber for T.
|
// Queue. Panics if the queue already has a subscriber for T.
|
||||||
func Subscribe[T any](c *Client) *Subscriber[T] {
|
func Subscribe[T any](c *Client) *Subscriber[T] {
|
||||||
return newSubscriber[T](c.subscribeState())
|
r := c.subscribeState()
|
||||||
|
s := newSubscriber[T](r)
|
||||||
|
r.addSubscriber(s)
|
||||||
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
// Publisher returns a publisher for event type T using the given
|
// Publisher returns a publisher for event type T using the given
|
||||||
// client.
|
// client.
|
||||||
func Publish[T any](c *Client) *Publisher[T] {
|
func Publish[T any](c *Client) *Publisher[T] {
|
||||||
ret := newPublisher[T](c)
|
p := newPublisher[T](c)
|
||||||
c.mu.Lock()
|
c.addPublisher(p)
|
||||||
defer c.mu.Unlock()
|
return p
|
||||||
c.pub.Add(ret)
|
|
||||||
return ret
|
|
||||||
}
|
}
|
||||||
|
@ -21,11 +21,7 @@ type Publisher[T any] struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func newPublisher[T any](c *Client) *Publisher[T] {
|
func newPublisher[T any](c *Client) *Publisher[T] {
|
||||||
ret := &Publisher[T]{
|
return &Publisher[T]{client: c}
|
||||||
client: c,
|
|
||||||
}
|
|
||||||
c.addPublisher(ret)
|
|
||||||
return ret
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close closes the publisher.
|
// Close closes the publisher.
|
||||||
|
@ -91,7 +91,7 @@ func (q *subscribeState) pump(ctx context.Context) {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Keep the cases in this select in sync with
|
// Keep the cases in this select in sync with
|
||||||
// Subscriber.dispatch below. The only different should be
|
// Subscriber.dispatch below. The only difference should be
|
||||||
// that this select doesn't deliver queued values to
|
// that this select doesn't deliver queued values to
|
||||||
// anyone, and unconditionally accepts new values.
|
// anyone, and unconditionally accepts new values.
|
||||||
select {
|
select {
|
||||||
@ -134,9 +134,10 @@ func (s *subscribeState) subscribeTypes() []reflect.Type {
|
|||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *subscribeState) addSubscriber(t reflect.Type, sub subscriber) {
|
func (s *subscribeState) addSubscriber(sub subscriber) {
|
||||||
s.outputsMu.Lock()
|
s.outputsMu.Lock()
|
||||||
defer s.outputsMu.Unlock()
|
defer s.outputsMu.Unlock()
|
||||||
|
t := sub.subscribeType()
|
||||||
if s.outputs[t] != nil {
|
if s.outputs[t] != nil {
|
||||||
panic(fmt.Errorf("double subscription for event %s", t))
|
panic(fmt.Errorf("double subscription for event %s", t))
|
||||||
}
|
}
|
||||||
@ -183,15 +184,10 @@ type Subscriber[T any] struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func newSubscriber[T any](r *subscribeState) *Subscriber[T] {
|
func newSubscriber[T any](r *subscribeState) *Subscriber[T] {
|
||||||
t := reflect.TypeFor[T]()
|
return &Subscriber[T]{
|
||||||
|
|
||||||
ret := &Subscriber[T]{
|
|
||||||
read: make(chan T),
|
read: make(chan T),
|
||||||
unregister: func() { r.deleteSubscriber(t) },
|
unregister: func() { r.deleteSubscriber(reflect.TypeFor[T]()) },
|
||||||
}
|
}
|
||||||
r.addSubscriber(t, ret)
|
|
||||||
|
|
||||||
return ret
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func newMonitor[T any](attach func(fn func(T)) (cancel func())) *Subscriber[T] {
|
func newMonitor[T any](attach func(fn func(T)) (cancel func())) *Subscriber[T] {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user