1 // Package event deals with subscriptions to real-time events.
10 log "github.com/sirupsen/logrus"
12 "github.com/vapor/protocol/bc/types"
17 maxEventChSize = 65536
21 // ErrMuxClosed is returned when Posting on a closed TypeMux.
22 ErrMuxClosed = errors.New("event: mux closed")
23 //ErrDuplicateSubscribe is returned when subscribe duplicate type
24 ErrDuplicateSubscribe = errors.New("event: subscribe duplicate type")
27 type NewMinedBlockEvent struct{ Block types.Block }
29 // TypeMuxEvent is a time-tagged notification pushed to subscribers.
30 type TypeMuxEvent struct {
35 // A Dispatcher dispatches events to registered receivers. Receivers can be
36 // registered to handle events of certain type. Any operation
37 // called after mux is stopped will return ErrMuxClosed.
39 // The zero value is ready to use.
40 type Dispatcher struct {
42 subm map[reflect.Type][]*Subscription
46 func NewDispatcher() *Dispatcher {
48 subm: make(map[reflect.Type][]*Subscription),
52 // Subscribe creates a subscription for events of the given types. The
53 // subscription's channel is closed when it is unsubscribed
54 // or the mux is closed.
55 func (d *Dispatcher) Subscribe(types ...interface{}) (*Subscription, error) {
56 sub := newSubscription(d)
58 defer d.mutex.Unlock()
60 // set the status to closed so that calling Unsubscribe after this
61 // call will short circuit.
67 for _, t := range types {
68 rtyp := reflect.TypeOf(t)
69 oldsubs := d.subm[rtyp]
70 if find(oldsubs, sub) != -1 {
71 log.WithFields(log.Fields{"module": logModule}).Errorf("duplicate type %s in Subscribe", rtyp)
72 return nil, ErrDuplicateSubscribe
75 subs := make([]*Subscription, len(oldsubs)+1)
77 subs[len(oldsubs)] = sub
83 // Post sends an event to all receivers registered for the given type.
84 // It returns ErrMuxClosed if the mux has been stopped.
85 func (d *Dispatcher) Post(ev interface{}) error {
86 event := &TypeMuxEvent{
90 rtyp := reflect.TypeOf(ev)
99 for _, sub := range subs {
105 // Stop closes a mux. The mux can no longer be used.
106 // Future Post calls will fail with ErrMuxClosed.
107 // Stop blocks until all current deliveries have finished.
108 func (d *Dispatcher) Stop() {
110 for _, subs := range d.subm {
111 for _, sub := range subs {
120 func (d *Dispatcher) del(s *Subscription) {
122 for typ, subs := range d.subm {
123 if pos := find(subs, s); pos >= 0 {
127 d.subm[typ] = posdelete(subs, pos)
134 func find(slice []*Subscription, item *Subscription) int {
135 for i, v := range slice {
143 func posdelete(slice []*Subscription, pos int) []*Subscription {
144 news := make([]*Subscription, len(slice)-1)
145 copy(news[:pos], slice[:pos])
146 copy(news[pos:], slice[pos+1:])
150 // Subscription is a subscription established through TypeMux.
151 type Subscription struct {
152 dispatcher *Dispatcher
155 closing chan struct{}
158 // these two are the same channel. they are stored separately so
159 // postC can be set to nil without affecting the return value of
162 readC <-chan *TypeMuxEvent
163 postC chan<- *TypeMuxEvent
166 func newSubscription(dispatcher *Dispatcher) *Subscription {
167 c := make(chan *TypeMuxEvent, maxEventChSize)
168 return &Subscription{
169 dispatcher: dispatcher,
173 closing: make(chan struct{}),
177 func (s *Subscription) Chan() <-chan *TypeMuxEvent {
181 func (s *Subscription) Unsubscribe() {
186 func (s *Subscription) Closed() bool {
188 defer s.closeMu.Unlock()
192 func (s *Subscription) closewait() {
194 defer s.closeMu.Unlock()
207 func (s *Subscription) deliver(event *TypeMuxEvent) {
208 // Short circuit delivery if stale event
209 if s.created.After(event.Time) {
212 // Otherwise deliver the event
214 defer s.postMu.RUnlock()
217 case s.postC <- event:
220 log.WithFields(log.Fields{"module": logModule}).Errorf("deliver event err unread event size %d", len(s.postC))