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 //NewBlockProposeEvent block propose event which needs to broadcast.
37 type NewBlockProposeEvent struct{ Block types.Block }
39 // TypeMuxEvent is a time-tagged notification pushed to subscribers.
40 type TypeMuxEvent struct {
45 // A Dispatcher dispatches events to registered receivers. Receivers can be
46 // registered to handle events of certain type. Any operation
47 // called after mux is stopped will return ErrMuxClosed.
49 // The zero value is ready to use.
50 type Dispatcher struct {
52 subm map[reflect.Type][]*Subscription
56 func NewDispatcher() *Dispatcher {
58 subm: make(map[reflect.Type][]*Subscription),
62 // Subscribe creates a subscription for events of the given types. The
63 // subscription's channel is closed when it is unsubscribed
64 // or the mux is closed.
65 func (d *Dispatcher) Subscribe(types ...interface{}) (*Subscription, error) {
66 sub := newSubscription(d)
68 defer d.mutex.Unlock()
70 // set the status to closed so that calling Unsubscribe after this
71 // call will short circuit.
77 for _, t := range types {
78 rtyp := reflect.TypeOf(t)
79 oldsubs := d.subm[rtyp]
80 if find(oldsubs, sub) != -1 {
81 log.WithFields(log.Fields{"module": logModule}).Errorf("duplicate type %s in Subscribe", rtyp)
82 return nil, ErrDuplicateSubscribe
85 subs := make([]*Subscription, len(oldsubs)+1)
87 subs[len(oldsubs)] = sub
93 // Post sends an event to all receivers registered for the given type.
94 // It returns ErrMuxClosed if the mux has been stopped.
95 func (d *Dispatcher) Post(ev interface{}) error {
96 event := &TypeMuxEvent{
100 rtyp := reflect.TypeOf(ev)
109 for _, sub := range subs {
115 // Stop closes a mux. The mux can no longer be used.
116 // Future Post calls will fail with ErrMuxClosed.
117 // Stop blocks until all current deliveries have finished.
118 func (d *Dispatcher) Stop() {
120 for _, subs := range d.subm {
121 for _, sub := range subs {
130 func (d *Dispatcher) del(s *Subscription) {
132 for typ, subs := range d.subm {
133 if pos := find(subs, s); pos >= 0 {
137 d.subm[typ] = posdelete(subs, pos)
144 func find(slice []*Subscription, item *Subscription) int {
145 for i, v := range slice {
153 func posdelete(slice []*Subscription, pos int) []*Subscription {
154 news := make([]*Subscription, len(slice)-1)
155 copy(news[:pos], slice[:pos])
156 copy(news[pos:], slice[pos+1:])
160 // Subscription is a subscription established through TypeMux.
161 type Subscription struct {
162 dispatcher *Dispatcher
165 closing chan struct{}
168 // these two are the same channel. they are stored separately so
169 // postC can be set to nil without affecting the return value of
172 readC <-chan *TypeMuxEvent
173 postC chan<- *TypeMuxEvent
176 func newSubscription(dispatcher *Dispatcher) *Subscription {
177 c := make(chan *TypeMuxEvent, maxEventChSize)
178 return &Subscription{
179 dispatcher: dispatcher,
183 closing: make(chan struct{}),
187 func (s *Subscription) Chan() <-chan *TypeMuxEvent {
191 func (s *Subscription) Unsubscribe() {
196 func (s *Subscription) Closed() bool {
198 defer s.closeMu.Unlock()
202 func (s *Subscription) closewait() {
204 defer s.closeMu.Unlock()
217 func (s *Subscription) deliver(event *TypeMuxEvent) {
218 // Short circuit delivery if stale event
219 if s.created.After(event.Time) {
222 // Otherwise deliver the event
224 defer s.postMu.RUnlock()
227 case s.postC <- event:
230 log.WithFields(log.Fields{"module": logModule}).Errorf("deliver event err unread event size %d", len(s.postC))