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 //NewBlockProposeEvent block propose event which needs to broadcast.
36 type NewBlockProposeEvent struct{ Block types.Block }
38 // TypeMuxEvent is a time-tagged notification pushed to subscribers.
39 type TypeMuxEvent struct {
44 // A Dispatcher dispatches events to registered receivers. Receivers can be
45 // registered to handle events of certain type. Any operation
46 // called after mux is stopped will return ErrMuxClosed.
48 // The zero value is ready to use.
49 type Dispatcher struct {
51 subm map[reflect.Type][]*Subscription
55 func NewDispatcher() *Dispatcher {
57 subm: make(map[reflect.Type][]*Subscription),
61 // Subscribe creates a subscription for events of the given types. The
62 // subscription's channel is closed when it is unsubscribed
63 // or the mux is closed.
64 func (d *Dispatcher) Subscribe(types ...interface{}) (*Subscription, error) {
65 sub := newSubscription(d)
67 defer d.mutex.Unlock()
69 // set the status to closed so that calling Unsubscribe after this
70 // call will short circuit.
76 for _, t := range types {
77 rtyp := reflect.TypeOf(t)
78 oldsubs := d.subm[rtyp]
79 if find(oldsubs, sub) != -1 {
80 log.WithFields(log.Fields{"module": logModule}).Errorf("duplicate type %s in Subscribe", rtyp)
81 return nil, ErrDuplicateSubscribe
84 subs := make([]*Subscription, len(oldsubs)+1)
86 subs[len(oldsubs)] = sub
92 // Post sends an event to all receivers registered for the given type.
93 // It returns ErrMuxClosed if the mux has been stopped.
94 func (d *Dispatcher) Post(ev interface{}) error {
95 event := &TypeMuxEvent{
99 rtyp := reflect.TypeOf(ev)
108 for _, sub := range subs {
114 // Stop closes a mux. The mux can no longer be used.
115 // Future Post calls will fail with ErrMuxClosed.
116 // Stop blocks until all current deliveries have finished.
117 func (d *Dispatcher) Stop() {
119 for _, subs := range d.subm {
120 for _, sub := range subs {
129 func (d *Dispatcher) del(s *Subscription) {
131 for typ, subs := range d.subm {
132 if pos := find(subs, s); pos >= 0 {
136 d.subm[typ] = posdelete(subs, pos)
143 func find(slice []*Subscription, item *Subscription) int {
144 for i, v := range slice {
152 func posdelete(slice []*Subscription, pos int) []*Subscription {
153 news := make([]*Subscription, len(slice)-1)
154 copy(news[:pos], slice[:pos])
155 copy(news[pos:], slice[pos+1:])
159 // Subscription is a subscription established through TypeMux.
160 type Subscription struct {
161 dispatcher *Dispatcher
164 closing chan struct{}
167 // these two are the same channel. they are stored separately so
168 // postC can be set to nil without affecting the return value of
171 readC <-chan *TypeMuxEvent
172 postC chan<- *TypeMuxEvent
175 func newSubscription(dispatcher *Dispatcher) *Subscription {
176 c := make(chan *TypeMuxEvent, maxEventChSize)
177 return &Subscription{
178 dispatcher: dispatcher,
182 closing: make(chan struct{}),
186 func (s *Subscription) Chan() <-chan *TypeMuxEvent {
190 func (s *Subscription) Unsubscribe() {
195 func (s *Subscription) Closed() bool {
197 defer s.closeMu.Unlock()
201 func (s *Subscription) closewait() {
203 defer s.closeMu.Unlock()
216 func (s *Subscription) deliver(event *TypeMuxEvent) {
217 // Short circuit delivery if stale event
218 if s.created.After(event.Time) {
221 // Otherwise deliver the event
223 defer s.postMu.RUnlock()
226 case s.postC <- event:
229 log.WithFields(log.Fields{"module": logModule}).Errorf("deliver event err unread event size %d", len(s.postC))