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'
pull/3605/head
Felix Lange 8 years ago
parent f1069a30b9
commit 9b62facdd4
  1. 2
      core/tx_pool.go
  2. 4
      eth/filters/filter_system.go
  3. 4
      eth/handler.go
  4. 58
      event/event.go
  5. 2
      light/txpool.go
  6. 2
      miner/worker.go

@ -89,7 +89,7 @@ type TxPool struct {
gasLimit func() *big.Int // The current gas limit function callback gasLimit func() *big.Int // The current gas limit function callback
minGasPrice *big.Int minGasPrice *big.Int
eventMux *event.TypeMux eventMux *event.TypeMux
events event.Subscription events *event.TypeMuxSubscription
localTx *txSet localTx *txSet
signer types.Signer signer types.Signer
mu sync.RWMutex mu sync.RWMutex

@ -74,7 +74,7 @@ type subscription struct {
// subscription which match the subscription criteria. // subscription which match the subscription criteria.
type EventSystem struct { type EventSystem struct {
mux *event.TypeMux mux *event.TypeMux
sub event.Subscription sub *event.TypeMuxSubscription
backend Backend backend Backend
lightMode bool lightMode bool
lastHead *types.Header lastHead *types.Header
@ -277,7 +277,7 @@ func (es *EventSystem) SubscribePendingTxEvents(hashes chan common.Hash) *Subscr
type filterIndex map[Type]map[rpc.ID]*subscription type filterIndex map[Type]map[rpc.ID]*subscription
// broadcast event to filters that match criteria. // broadcast event to filters that match criteria.
func (es *EventSystem) broadcast(filters filterIndex, ev *event.Event) { func (es *EventSystem) broadcast(filters filterIndex, ev *event.TypeMuxEvent) {
if ev == nil { if ev == nil {
return return
} }

@ -78,8 +78,8 @@ type ProtocolManager struct {
SubProtocols []p2p.Protocol SubProtocols []p2p.Protocol
eventMux *event.TypeMux eventMux *event.TypeMux
txSub event.Subscription txSub *event.TypeMuxSubscription
minedBlockSub event.Subscription minedBlockSub *event.TypeMuxSubscription
// channels for fetcher, syncer, txsyncLoop // channels for fetcher, syncer, txsyncLoop
newPeerCh chan *peer newPeerCh chan *peer

@ -25,33 +25,22 @@ import (
"time" "time"
) )
// Event is a time-tagged notification pushed to subscribers. // TypeMuxEvent is a time-tagged notification pushed to subscribers.
type Event struct { type TypeMuxEvent struct {
Time time.Time Time time.Time
Data interface{} Data interface{}
} }
// Subscription is implemented by event subscriptions.
type Subscription interface {
// Chan returns a channel that carries events.
// Implementations should return the same channel
// for any subsequent calls to Chan.
Chan() <-chan *Event
// Unsubscribe stops delivery of events to a subscription.
// The event channel is closed.
// Unsubscribe can be called more than once.
Unsubscribe()
}
// A TypeMux dispatches events to registered receivers. Receivers can be // A TypeMux dispatches events to registered receivers. Receivers can be
// registered to handle events of certain type. Any operation // registered to handle events of certain type. Any operation
// called after mux is stopped will return ErrMuxClosed. // called after mux is stopped will return ErrMuxClosed.
// //
// The zero value is ready to use. // The zero value is ready to use.
//
// Deprecated: use Feed
type TypeMux struct { type TypeMux struct {
mutex sync.RWMutex mutex sync.RWMutex
subm map[reflect.Type][]*muxsub subm map[reflect.Type][]*TypeMuxSubscription
stopped bool stopped bool
} }
@ -61,7 +50,7 @@ var ErrMuxClosed = errors.New("event: mux closed")
// Subscribe creates a subscription for events of the given types. The // Subscribe creates a subscription for events of the given types. The
// subscription's channel is closed when it is unsubscribed // subscription's channel is closed when it is unsubscribed
// or the mux is closed. // or the mux is closed.
func (mux *TypeMux) Subscribe(types ...interface{}) Subscription { func (mux *TypeMux) Subscribe(types ...interface{}) *TypeMuxSubscription {
sub := newsub(mux) sub := newsub(mux)
mux.mutex.Lock() mux.mutex.Lock()
defer mux.mutex.Unlock() defer mux.mutex.Unlock()
@ -72,7 +61,7 @@ func (mux *TypeMux) Subscribe(types ...interface{}) Subscription {
close(sub.postC) close(sub.postC)
} else { } else {
if mux.subm == nil { if mux.subm == nil {
mux.subm = make(map[reflect.Type][]*muxsub) mux.subm = make(map[reflect.Type][]*TypeMuxSubscription)
} }
for _, t := range types { for _, t := range types {
rtyp := reflect.TypeOf(t) rtyp := reflect.TypeOf(t)
@ -80,7 +69,7 @@ func (mux *TypeMux) Subscribe(types ...interface{}) Subscription {
if find(oldsubs, sub) != -1 { if find(oldsubs, sub) != -1 {
panic(fmt.Sprintf("event: duplicate type %s in Subscribe", rtyp)) panic(fmt.Sprintf("event: duplicate type %s in Subscribe", rtyp))
} }
subs := make([]*muxsub, len(oldsubs)+1) subs := make([]*TypeMuxSubscription, len(oldsubs)+1)
copy(subs, oldsubs) copy(subs, oldsubs)
subs[len(oldsubs)] = sub subs[len(oldsubs)] = sub
mux.subm[rtyp] = subs mux.subm[rtyp] = subs
@ -92,7 +81,7 @@ func (mux *TypeMux) Subscribe(types ...interface{}) Subscription {
// Post sends an event to all receivers registered for the given type. // Post sends an event to all receivers registered for the given type.
// It returns ErrMuxClosed if the mux has been stopped. // It returns ErrMuxClosed if the mux has been stopped.
func (mux *TypeMux) Post(ev interface{}) error { func (mux *TypeMux) Post(ev interface{}) error {
event := &Event{ event := &TypeMuxEvent{
Time: time.Now(), Time: time.Now(),
Data: ev, Data: ev,
} }
@ -125,7 +114,7 @@ func (mux *TypeMux) Stop() {
mux.mutex.Unlock() mux.mutex.Unlock()
} }
func (mux *TypeMux) del(s *muxsub) { func (mux *TypeMux) del(s *TypeMuxSubscription) {
mux.mutex.Lock() mux.mutex.Lock()
for typ, subs := range mux.subm { for typ, subs := range mux.subm {
if pos := find(subs, s); pos >= 0 { if pos := find(subs, s); pos >= 0 {
@ -139,7 +128,7 @@ func (mux *TypeMux) del(s *muxsub) {
s.mux.mutex.Unlock() s.mux.mutex.Unlock()
} }
func find(slice []*muxsub, item *muxsub) int { func find(slice []*TypeMuxSubscription, item *TypeMuxSubscription) int {
for i, v := range slice { for i, v := range slice {
if v == item { if v == item {
return i return i
@ -148,14 +137,15 @@ func find(slice []*muxsub, item *muxsub) int {
return -1 return -1
} }
func posdelete(slice []*muxsub, pos int) []*muxsub { func posdelete(slice []*TypeMuxSubscription, pos int) []*TypeMuxSubscription {
news := make([]*muxsub, len(slice)-1) news := make([]*TypeMuxSubscription, len(slice)-1)
copy(news[:pos], slice[:pos]) copy(news[:pos], slice[:pos])
copy(news[pos:], slice[pos+1:]) copy(news[pos:], slice[pos+1:])
return news return news
} }
type muxsub struct { // TypeMuxSubscription is a subscription established through TypeMux.
type TypeMuxSubscription struct {
mux *TypeMux mux *TypeMux
created time.Time created time.Time
closeMu sync.Mutex closeMu sync.Mutex
@ -166,13 +156,13 @@ type muxsub struct {
// postC can be set to nil without affecting the return value of // postC can be set to nil without affecting the return value of
// Chan. // Chan.
postMu sync.RWMutex postMu sync.RWMutex
readC <-chan *Event readC <-chan *TypeMuxEvent
postC chan<- *Event postC chan<- *TypeMuxEvent
} }
func newsub(mux *TypeMux) *muxsub { func newsub(mux *TypeMux) *TypeMuxSubscription {
c := make(chan *Event) c := make(chan *TypeMuxEvent)
return &muxsub{ return &TypeMuxSubscription{
mux: mux, mux: mux,
created: time.Now(), created: time.Now(),
readC: c, readC: c,
@ -181,16 +171,16 @@ func newsub(mux *TypeMux) *muxsub {
} }
} }
func (s *muxsub) Chan() <-chan *Event { func (s *TypeMuxSubscription) Chan() <-chan *TypeMuxEvent {
return s.readC return s.readC
} }
func (s *muxsub) Unsubscribe() { func (s *TypeMuxSubscription) Unsubscribe() {
s.mux.del(s) s.mux.del(s)
s.closewait() s.closewait()
} }
func (s *muxsub) closewait() { func (s *TypeMuxSubscription) closewait() {
s.closeMu.Lock() s.closeMu.Lock()
defer s.closeMu.Unlock() defer s.closeMu.Unlock()
if s.closed { if s.closed {
@ -205,7 +195,7 @@ func (s *muxsub) closewait() {
s.postMu.Unlock() s.postMu.Unlock()
} }
func (s *muxsub) deliver(event *Event) { func (s *TypeMuxSubscription) deliver(event *TypeMuxEvent) {
// Short circuit delivery if stale event // Short circuit delivery if stale event
if s.created.After(event.Time) { if s.created.After(event.Time) {
return return

@ -47,7 +47,7 @@ type TxPool struct {
signer types.Signer signer types.Signer
quit chan bool quit chan bool
eventMux *event.TypeMux eventMux *event.TypeMux
events event.Subscription events *event.TypeMuxSubscription
mu sync.RWMutex mu sync.RWMutex
chain *LightChain chain *LightChain
odr OdrBackend odr OdrBackend

@ -90,7 +90,7 @@ type worker struct {
// update loop // update loop
mux *event.TypeMux mux *event.TypeMux
events event.Subscription events *event.TypeMuxSubscription
wg sync.WaitGroup wg sync.WaitGroup
agents map[Agent]struct{} agents map[Agent]struct{}

Loading…
Cancel
Save