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 {
35 // TypeMuxEvent is a time-tagged notification pushed to subscribers.
36 type TypeMuxEvent struct {
41 // A Dispatcher dispatches events to registered receivers. Receivers can be
42 // registered to handle events of certain type. Any operation
43 // called after mux is stopped will return ErrMuxClosed.
45 // The zero value is ready to use.
46 type Dispatcher struct {
48 subm map[reflect.Type][]*Subscription
52 func NewDispatcher() *Dispatcher {
54 subm: make(map[reflect.Type][]*Subscription),
58 // Subscribe creates a subscription for events of the given types. The
59 // subscription's channel is closed when it is unsubscribed
60 // or the mux is closed.
61 func (d *Dispatcher) Subscribe(types ...interface{}) (*Subscription, error) {
62 sub := newSubscription(d)
64 defer d.mutex.Unlock()
66 // set the status to closed so that calling Unsubscribe after this
67 // call will short circuit.
73 for _, t := range types {
74 rtyp := reflect.TypeOf(t)
75 oldsubs := d.subm[rtyp]
76 if find(oldsubs, sub) != -1 {
77 log.WithFields(log.Fields{"module": logModule}).Errorf("duplicate type %s in Subscribe", rtyp)
78 return nil, ErrDuplicateSubscribe
81 subs := make([]*Subscription, len(oldsubs)+1)
83 subs[len(oldsubs)] = sub
89 // Post sends an event to all receivers registered for the given type.
90 // It returns ErrMuxClosed if the mux has been stopped.
91 func (d *Dispatcher) Post(ev interface{}) error {
92 event := &TypeMuxEvent{
96 rtyp := reflect.TypeOf(ev)
105 for _, sub := range subs {
111 // Stop closes a mux. The mux can no longer be used.
112 // Future Post calls will fail with ErrMuxClosed.
113 // Stop blocks until all current deliveries have finished.
114 func (d *Dispatcher) Stop() {
116 for _, subs := range d.subm {
117 for _, sub := range subs {
126 func (d *Dispatcher) del(s *Subscription) {
128 for typ, subs := range d.subm {
129 if pos := find(subs, s); pos >= 0 {
133 d.subm[typ] = posdelete(subs, pos)
140 func find(slice []*Subscription, item *Subscription) int {
141 for i, v := range slice {
149 func posdelete(slice []*Subscription, pos int) []*Subscription {
150 news := make([]*Subscription, len(slice)-1)
151 copy(news[:pos], slice[:pos])
152 copy(news[pos:], slice[pos+1:])
156 // Subscription is a subscription established through TypeMux.
157 type Subscription struct {
158 dispatcher *Dispatcher
161 closing chan struct{}
164 // these two are the same channel. they are stored separately so
165 // postC can be set to nil without affecting the return value of
168 readC <-chan *TypeMuxEvent
169 postC chan<- *TypeMuxEvent
172 func newSubscription(dispatcher *Dispatcher) *Subscription {
173 c := make(chan *TypeMuxEvent, maxEventChSize)
174 return &Subscription{
175 dispatcher: dispatcher,
179 closing: make(chan struct{}),
183 func (s *Subscription) Chan() <-chan *TypeMuxEvent {
187 func (s *Subscription) Unsubscribe() {
192 func (s *Subscription) Closed() bool {
194 defer s.closeMu.Unlock()
198 func (s *Subscription) closewait() {
200 defer s.closeMu.Unlock()
213 func (s *Subscription) deliver(event *TypeMuxEvent) {
214 // Short circuit delivery if stale event
215 if s.created.After(event.Time) {
218 // Otherwise deliver the event
220 defer s.postMu.RUnlock()
223 case s.postC <- event:
226 log.WithFields(log.Fields{"module": logModule}).Errorf("deliver event err unread event size %d", len(s.postC))