Skip to content

Commit 9b62fac

Browse files
committed
event: deprecate TypeMux and related types
The Subscription type is gone, all uses are replaced by *TypeMuxSubscription. This change is prep-work for the introduction of the new Subscription type in a later commit. gorename -from '"github.com/ethereum/go-ethereum/event"::Event' -to TypeMuxEvent gorename -from '"github.com/ethereum/go-ethereum/event"::muxsub' -to TypeMuxSubscription gofmt -w -r 'Subscription -> *TypeMuxSubscription' ./event/*.go find . -name '*.go' -and -not -regex '\./vendor/.*' \| xargs gofmt -w -r 'event.Subscription -> *event.TypeMuxSubscription'
1 parent f1069a3 commit 9b62fac

6 files changed

Lines changed: 31 additions & 41 deletions

File tree

‎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: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -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

‎light/txpool.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ type TxPool struct {
4747
signer types.Signer
4848
quit chan bool
4949
eventMux *event.TypeMux
50-
events event.Subscription
50+
events *event.TypeMuxSubscription
5151
mu sync.RWMutex
5252
chain *LightChain
5353
odr OdrBackend

‎miner/worker.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ type worker struct {
9090

9191
// update loop
9292
mux *event.TypeMux
93-
events event.Subscription
93+
events *event.TypeMuxSubscription
9494
wg sync.WaitGroup
9595

9696
agents map[Agent]struct{}

0 commit comments

Comments
 (0)