Skip to content

event: proposal for improving the API #1867

@fjl

Description

@fjl

Package event provides a mechanism for pushing change notifications to an unknown number of
interested parties. This issue is a collection of ideas for improving package event. We've lived with the current API for almost a year now and while it has held up well, there are a few things that can be improved.

Posting events should not be synchronous by default.

We've learned this the hard way, with several hard-to-see deadlocks being caused by a blocking call to Post while a mutex is held.

Posting should return the number of recipients.

This allows for response gathering, e.g.

resp := make(chan Reply)
n := mux.Post(SomeEvent{resp})
for i := 0; i < n; i++ {
    <-resp
}

Implementation improvements

The current implementation of TypeMux assumes that event subscriptions are mostly static (set up once, then used). Adding and removing subscriptions has non-trivial overhead, halting all event processing while the internal state is updated.

Delivery of each event of a certain type is sequential, waiting for each subscriber to receive the event before attempting to send to the next. This can delay reception if subscribers are blocked.

Provide helpers for shutdown coordination

Package event might also be the right place to contain a generic solution for shutdown signaling. Many components in go-ethereum have boilerplate that coordinates shutdown of one or more 'worker loop'
goroutines when the Stop method is called. Since quite a lot of boilerplate is involved to get this right, components usually cheat and implement a simple solution that doesn't wait for workers to terminate and
panics with "close of closed channel" if Stop is called multiple times.

I propose that we add a generic solution for shutdown coordination to package event.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions