Skip to content

Commit 7734ead

Browse files
authored
Merge pull request bnb-chain#3605 from fjl/event-feed
event: add new Subscription type and related utilities
2 parents 8b57c49 + 1bed9b3 commit 7734ead

15 files changed

Lines changed: 1256 additions & 49 deletions

‎core/tx_pool.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ type TxPool struct {
8989
gasLimit func() *big.Int // The current gas limit function callback
9090
minGasPrice *big.Int
9191
eventMux *event.TypeMux
92-
events event.Subscription
92+
events *event.TypeMuxSubscription
9393
localTx *txSet
9494
signer types.Signer
9595
mu sync.RWMutex

‎eth/filters/filter_system.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ type subscription struct {
7474
// subscription which match the subscription criteria.
7575
type EventSystem struct {
7676
mux *event.TypeMux
77-
sub event.Subscription
77+
sub *event.TypeMuxSubscription
7878
backend Backend
7979
lightMode bool
8080
lastHead *types.Header
@@ -277,7 +277,7 @@ func (es *EventSystem) SubscribePendingTxEvents(hashes chan common.Hash) *Subscr
277277
type filterIndex map[Type]map[rpc.ID]*subscription
278278

279279
// broadcast event to filters that match criteria.
280-
func (es *EventSystem) broadcast(filters filterIndex, ev *event.Event) {
280+
func (es *EventSystem) broadcast(filters filterIndex, ev *event.TypeMuxEvent) {
281281
if ev == nil {
282282
return
283283
}

‎eth/handler.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ type ProtocolManager struct {
7878
SubProtocols []p2p.Protocol
7979

8080
eventMux *event.TypeMux
81-
txSub event.Subscription
82-
minedBlockSub event.Subscription
81+
txSub *event.TypeMuxSubscription
82+
minedBlockSub *event.TypeMuxSubscription
8383

8484
// channels for fetcher, syncer, txsyncLoop
8585
newPeerCh chan *peer

‎event/event.go‎

Lines changed: 25 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
// You should have received a copy of the GNU Lesser General Public License
1515
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
1616

17-
// Package event implements an event multiplexer.
17+
// Package event deals with subscriptions to real-time events.
1818
package event
1919

2020
import (
@@ -25,33 +25,22 @@ import (
2525
"time"
2626
)
2727

28-
// Event is a time-tagged notification pushed to subscribers.
29-
type Event struct {
28+
// TypeMuxEvent is a time-tagged notification pushed to subscribers.
29+
type TypeMuxEvent struct {
3030
Time time.Time
3131
Data interface{}
3232
}
3333

34-
// Subscription is implemented by event subscriptions.
35-
type Subscription interface {
36-
// Chan returns a channel that carries events.
37-
// Implementations should return the same channel
38-
// for any subsequent calls to Chan.
39-
Chan() <-chan *Event
40-
41-
// Unsubscribe stops delivery of events to a subscription.
42-
// The event channel is closed.
43-
// Unsubscribe can be called more than once.
44-
Unsubscribe()
45-
}
46-
4734
// A TypeMux dispatches events to registered receivers. Receivers can be
4835
// registered to handle events of certain type. Any operation
4936
// called after mux is stopped will return ErrMuxClosed.
5037
//
5138
// The zero value is ready to use.
39+
//
40+
// Deprecated: use Feed
5241
type TypeMux struct {
5342
mutex sync.RWMutex
54-
subm map[reflect.Type][]*muxsub
43+
subm map[reflect.Type][]*TypeMuxSubscription
5544
stopped bool
5645
}
5746

@@ -61,7 +50,7 @@ var ErrMuxClosed = errors.New("event: mux closed")
6150
// Subscribe creates a subscription for events of the given types. The
6251
// subscription's channel is closed when it is unsubscribed
6352
// or the mux is closed.
64-
func (mux *TypeMux) Subscribe(types ...interface{}) Subscription {
53+
func (mux *TypeMux) Subscribe(types ...interface{}) *TypeMuxSubscription {
6554
sub := newsub(mux)
6655
mux.mutex.Lock()
6756
defer mux.mutex.Unlock()
@@ -72,15 +61,15 @@ func (mux *TypeMux) Subscribe(types ...interface{}) Subscription {
7261
close(sub.postC)
7362
} else {
7463
if mux.subm == nil {
75-
mux.subm = make(map[reflect.Type][]*muxsub)
64+
mux.subm = make(map[reflect.Type][]*TypeMuxSubscription)
7665
}
7766
for _, t := range types {
7867
rtyp := reflect.TypeOf(t)
7968
oldsubs := mux.subm[rtyp]
8069
if find(oldsubs, sub) != -1 {
8170
panic(fmt.Sprintf("event: duplicate type %s in Subscribe", rtyp))
8271
}
83-
subs := make([]*muxsub, len(oldsubs)+1)
72+
subs := make([]*TypeMuxSubscription, len(oldsubs)+1)
8473
copy(subs, oldsubs)
8574
subs[len(oldsubs)] = sub
8675
mux.subm[rtyp] = subs
@@ -92,7 +81,7 @@ func (mux *TypeMux) Subscribe(types ...interface{}) Subscription {
9281
// Post sends an event to all receivers registered for the given type.
9382
// It returns ErrMuxClosed if the mux has been stopped.
9483
func (mux *TypeMux) Post(ev interface{}) error {
95-
event := &Event{
84+
event := &TypeMuxEvent{
9685
Time: time.Now(),
9786
Data: ev,
9887
}
@@ -125,7 +114,7 @@ func (mux *TypeMux) Stop() {
125114
mux.mutex.Unlock()
126115
}
127116

128-
func (mux *TypeMux) del(s *muxsub) {
117+
func (mux *TypeMux) del(s *TypeMuxSubscription) {
129118
mux.mutex.Lock()
130119
for typ, subs := range mux.subm {
131120
if pos := find(subs, s); pos >= 0 {
@@ -139,7 +128,7 @@ func (mux *TypeMux) del(s *muxsub) {
139128
s.mux.mutex.Unlock()
140129
}
141130

142-
func find(slice []*muxsub, item *muxsub) int {
131+
func find(slice []*TypeMuxSubscription, item *TypeMuxSubscription) int {
143132
for i, v := range slice {
144133
if v == item {
145134
return i
@@ -148,14 +137,15 @@ func find(slice []*muxsub, item *muxsub) int {
148137
return -1
149138
}
150139

151-
func posdelete(slice []*muxsub, pos int) []*muxsub {
152-
news := make([]*muxsub, len(slice)-1)
140+
func posdelete(slice []*TypeMuxSubscription, pos int) []*TypeMuxSubscription {
141+
news := make([]*TypeMuxSubscription, len(slice)-1)
153142
copy(news[:pos], slice[:pos])
154143
copy(news[pos:], slice[pos+1:])
155144
return news
156145
}
157146

158-
type muxsub struct {
147+
// TypeMuxSubscription is a subscription established through TypeMux.
148+
type TypeMuxSubscription struct {
159149
mux *TypeMux
160150
created time.Time
161151
closeMu sync.Mutex
@@ -166,13 +156,13 @@ type muxsub struct {
166156
// postC can be set to nil without affecting the return value of
167157
// Chan.
168158
postMu sync.RWMutex
169-
readC <-chan *Event
170-
postC chan<- *Event
159+
readC <-chan *TypeMuxEvent
160+
postC chan<- *TypeMuxEvent
171161
}
172162

173-
func newsub(mux *TypeMux) *muxsub {
174-
c := make(chan *Event)
175-
return &muxsub{
163+
func newsub(mux *TypeMux) *TypeMuxSubscription {
164+
c := make(chan *TypeMuxEvent)
165+
return &TypeMuxSubscription{
176166
mux: mux,
177167
created: time.Now(),
178168
readC: c,
@@ -181,16 +171,16 @@ func newsub(mux *TypeMux) *muxsub {
181171
}
182172
}
183173

184-
func (s *muxsub) Chan() <-chan *Event {
174+
func (s *TypeMuxSubscription) Chan() <-chan *TypeMuxEvent {
185175
return s.readC
186176
}
187177

188-
func (s *muxsub) Unsubscribe() {
178+
func (s *TypeMuxSubscription) Unsubscribe() {
189179
s.mux.del(s)
190180
s.closewait()
191181
}
192182

193-
func (s *muxsub) closewait() {
183+
func (s *TypeMuxSubscription) closewait() {
194184
s.closeMu.Lock()
195185
defer s.closeMu.Unlock()
196186
if s.closed {
@@ -205,7 +195,7 @@ func (s *muxsub) closewait() {
205195
s.postMu.Unlock()
206196
}
207197

208-
func (s *muxsub) deliver(event *Event) {
198+
func (s *TypeMuxSubscription) deliver(event *TypeMuxEvent) {
209199
// Short circuit delivery if stale event
210200
if s.created.After(event.Time) {
211201
return

‎event/event_test.go‎

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -149,16 +149,34 @@ func emptySubscriber(mux *TypeMux, types ...interface{}) {
149149
}()
150150
}
151151

152-
func BenchmarkPost3(b *testing.B) {
153-
var mux = new(TypeMux)
154-
defer mux.Stop()
155-
emptySubscriber(mux, testEvent(0))
156-
emptySubscriber(mux, testEvent(0))
157-
emptySubscriber(mux, testEvent(0))
152+
func BenchmarkPost1000(b *testing.B) {
153+
var (
154+
mux = new(TypeMux)
155+
subscribed, done sync.WaitGroup
156+
nsubs = 1000
157+
)
158+
subscribed.Add(nsubs)
159+
done.Add(nsubs)
160+
for i := 0; i < nsubs; i++ {
161+
go func() {
162+
s := mux.Subscribe(testEvent(0))
163+
subscribed.Done()
164+
for range s.Chan() {
165+
}
166+
done.Done()
167+
}()
168+
}
169+
subscribed.Wait()
158170

171+
// The actual benchmark.
172+
b.ResetTimer()
159173
for i := 0; i < b.N; i++ {
160174
mux.Post(testEvent(0))
161175
}
176+
177+
b.StopTimer()
178+
mux.Stop()
179+
done.Wait()
162180
}
163181

164182
func BenchmarkPostConcurrent(b *testing.B) {

‎event/example_feed_test.go‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Copyright 2017 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
package event_test
18+
19+
import (
20+
"fmt"
21+
22+
"github.com/ethereum/go-ethereum/event"
23+
)
24+
25+
func ExampleFeed_acknowledgedEvents() {
26+
// This example shows how the return value of Send can be used for request/reply
27+
// interaction between event consumers and producers.
28+
var feed event.Feed
29+
type ackedEvent struct {
30+
i int
31+
ack chan<- struct{}
32+
}
33+
34+
// Consumers wait for events on the feed and acknowledge processing.
35+
done := make(chan struct{})
36+
defer close(done)
37+
for i := 0; i < 3; i++ {
38+
ch := make(chan ackedEvent, 100)
39+
sub := feed.Subscribe(ch)
40+
go func() {
41+
defer sub.Unsubscribe()
42+
for {
43+
select {
44+
case ev := <-ch:
45+
fmt.Println(ev.i) // "process" the event
46+
ev.ack <- struct{}{}
47+
case <-done:
48+
return
49+
}
50+
}
51+
}()
52+
}
53+
54+
// The producer sends values of type ackedEvent with increasing values of i.
55+
// It waits for all consumers to acknowledge before sending the next event.
56+
for i := 0; i < 3; i++ {
57+
acksignal := make(chan struct{})
58+
n := feed.Send(ackedEvent{i, acksignal})
59+
for ack := 0; ack < n; ack++ {
60+
<-acksignal
61+
}
62+
}
63+
// Output:
64+
// 0
65+
// 0
66+
// 0
67+
// 1
68+
// 1
69+
// 1
70+
// 2
71+
// 2
72+
// 2
73+
}

0 commit comments

Comments
 (0)