1 // Package event deals with subscriptions to real-time events.
10 log "github.com/sirupsen/logrus"
12 "github.com/vapor/protocol/bc"
13 "github.com/vapor/protocol/bc/types"
18 maxEventChSize = 65536
22 // ErrMuxClosed is returned when Posting on a closed TypeMux.
23 ErrMuxClosed = errors.New("event: mux closed")
24 //ErrDuplicateSubscribe is returned when subscribe duplicate type
25 ErrDuplicateSubscribe = errors.New("event: subscribe duplicate type")
28 type NewProposedBlockEvent struct{ Block types.Block }
30 type BlockSignatureEvent struct {
36 // TypeMuxEvent is a time-tagged notification pushed to subscribers.
37 type TypeMuxEvent struct {
42 // A Dispatcher dispatches events to registered receivers. Receivers can be
43 // registered to handle events of certain type. Any operation
44 // called after mux is stopped will return ErrMuxClosed.
46 // The zero value is ready to use.
47 type Dispatcher struct {
49 subm map[reflect.Type][]*Subscription
53 func NewDispatcher() *Dispatcher {
55 subm: make(map[reflect.Type][]*Subscription),
59 // Subscribe creates a subscription for events of the given types. The
60 // subscription's channel is closed when it is unsubscribed
61 // or the mux is closed.
62 func (d *Dispatcher) Subscribe(types ...interface{}) (*Subscription, error) {
63 sub := newSubscription(d)
65 defer d.mutex.Unlock()
67 // set the status to closed so that calling Unsubscribe after this
68 // call will short circuit.
74 for _, t := range types {
75 rtyp := reflect.TypeOf(t)
76 oldsubs := d.subm[rtyp]
77 if find(oldsubs, sub) != -1 {
78 log.WithFields(log.Fields{"module": logModule}).Errorf("duplicate type %s in Subscribe", rtyp)
79 return nil, ErrDuplicateSubscribe
82 subs := make([]*Subscription, len(oldsubs)+1)
84 subs[len(oldsubs)] = sub
90 // Post sends an event to all receivers registered for the given type.
91 // It returns ErrMuxClosed if the mux has been stopped.
92 func (d *Dispatcher) Post(ev interface{}) error {
93 event := &TypeMuxEvent{
97 rtyp := reflect.TypeOf(ev)
106 for _, sub := range subs {
112 // Stop closes a mux. The mux can no longer be used.
113 // Future Post calls will fail with ErrMuxClosed.
114 // Stop blocks until all current deliveries have finished.
115 func (d *Dispatcher) Stop() {
117 for _, subs := range d.subm {
118 for _, sub := range subs {
127 func (d *Dispatcher) del(s *Subscription) {
129 for typ, subs := range d.subm {
130 if pos := find(subs, s); pos >= 0 {
134 d.subm[typ] = posdelete(subs, pos)
141 func find(slice []*Subscription, item *Subscription) int {
142 for i, v := range slice {
150 func posdelete(slice []*Subscription, pos int) []*Subscription {
151 news := make([]*Subscription, len(slice)-1)
152 copy(news[:pos], slice[:pos])
153 copy(news[pos:], slice[pos+1:])
157 // Subscription is a subscription established through TypeMux.
158 type Subscription struct {
159 dispatcher *Dispatcher
162 closing chan struct{}
165 // these two are the same channel. they are stored separately so
166 // postC can be set to nil without affecting the return value of
169 readC <-chan *TypeMuxEvent
170 postC chan<- *TypeMuxEvent
173 func newSubscription(dispatcher *Dispatcher) *Subscription {
174 c := make(chan *TypeMuxEvent, maxEventChSize)
175 return &Subscription{
176 dispatcher: dispatcher,
180 closing: make(chan struct{}),
184 func (s *Subscription) Chan() <-chan *TypeMuxEvent {
188 func (s *Subscription) Unsubscribe() {
193 func (s *Subscription) Closed() bool {
195 defer s.closeMu.Unlock()
199 func (s *Subscription) closewait() {
201 defer s.closeMu.Unlock()
214 func (s *Subscription) deliver(event *TypeMuxEvent) {
215 // Short circuit delivery if stale event
216 if s.created.After(event.Time) {
219 // Otherwise deliver the event
221 defer s.postMu.RUnlock()
224 case s.postC <- event:
227 log.WithFields(log.Fields{"module": logModule}).Errorf("deliver event err unread event size %d", len(s.postC))