event.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. // Copyright 2014 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. // Package event deals with subscriptions to real-time events.
  17. package event
  18. import (
  19. "errors"
  20. "fmt"
  21. "reflect"
  22. "sync"
  23. "time"
  24. )
  25. // TypeMuxEvent is a time-tagged notification pushed to subscribers.
  26. type TypeMuxEvent struct {
  27. Time time.Time
  28. Data interface{}
  29. }
  30. // A TypeMux dispatches events to registered receivers. Receivers can be
  31. // registered to handle events of certain type. Any operation
  32. // called after mux is stopped will return ErrMuxClosed.
  33. //
  34. // The zero value is ready to use.
  35. //
  36. // Deprecated: use Feed
  37. type TypeMux struct {
  38. mutex sync.RWMutex
  39. subm map[reflect.Type][]*TypeMuxSubscription
  40. stopped bool
  41. }
  42. // ErrMuxClosed is returned when Posting on a closed TypeMux.
  43. var ErrMuxClosed = errors.New("event: mux closed")
  44. // Subscribe creates a subscription for events of the given types. The
  45. // subscription's channel is closed when it is unsubscribed
  46. // or the mux is closed.
  47. func (mux *TypeMux) Subscribe(types ...interface{}) *TypeMuxSubscription {
  48. sub := newsub(mux)
  49. mux.mutex.Lock()
  50. defer mux.mutex.Unlock()
  51. if mux.stopped {
  52. // set the status to closed so that calling Unsubscribe after this
  53. // call will short circuit.
  54. sub.closed = true
  55. close(sub.postC)
  56. } else {
  57. if mux.subm == nil {
  58. mux.subm = make(map[reflect.Type][]*TypeMuxSubscription)
  59. }
  60. for _, t := range types {
  61. rtyp := reflect.TypeOf(t)
  62. oldsubs := mux.subm[rtyp]
  63. if find(oldsubs, sub) != -1 {
  64. panic(fmt.Sprintf("event: duplicate type %s in Subscribe", rtyp))
  65. }
  66. subs := make([]*TypeMuxSubscription, len(oldsubs)+1)
  67. copy(subs, oldsubs)
  68. subs[len(oldsubs)] = sub
  69. mux.subm[rtyp] = subs
  70. }
  71. }
  72. return sub
  73. }
  74. // Post sends an event to all receivers registered for the given type.
  75. // It returns ErrMuxClosed if the mux has been stopped.
  76. func (mux *TypeMux) Post(ev interface{}) error {
  77. event := &TypeMuxEvent{
  78. Time: time.Now(),
  79. Data: ev,
  80. }
  81. rtyp := reflect.TypeOf(ev)
  82. mux.mutex.RLock()
  83. if mux.stopped {
  84. mux.mutex.RUnlock()
  85. return ErrMuxClosed
  86. }
  87. subs := mux.subm[rtyp]
  88. mux.mutex.RUnlock()
  89. for _, sub := range subs {
  90. sub.deliver(event)
  91. }
  92. return nil
  93. }
  94. // Stop closes a mux. The mux can no longer be used.
  95. // Future Post calls will fail with ErrMuxClosed.
  96. // Stop blocks until all current deliveries have finished.
  97. func (mux *TypeMux) Stop() {
  98. mux.mutex.Lock()
  99. for _, subs := range mux.subm {
  100. for _, sub := range subs {
  101. sub.closewait()
  102. }
  103. }
  104. mux.subm = nil
  105. mux.stopped = true
  106. mux.mutex.Unlock()
  107. }
  108. func (mux *TypeMux) del(s *TypeMuxSubscription) {
  109. mux.mutex.Lock()
  110. for typ, subs := range mux.subm {
  111. if pos := find(subs, s); pos >= 0 {
  112. if len(subs) == 1 {
  113. delete(mux.subm, typ)
  114. } else {
  115. mux.subm[typ] = posdelete(subs, pos)
  116. }
  117. }
  118. }
  119. s.mux.mutex.Unlock()
  120. }
  121. func find(slice []*TypeMuxSubscription, item *TypeMuxSubscription) int {
  122. for i, v := range slice {
  123. if v == item {
  124. return i
  125. }
  126. }
  127. return -1
  128. }
  129. func posdelete(slice []*TypeMuxSubscription, pos int) []*TypeMuxSubscription {
  130. news := make([]*TypeMuxSubscription, len(slice)-1)
  131. copy(news[:pos], slice[:pos])
  132. copy(news[pos:], slice[pos+1:])
  133. return news
  134. }
  135. // TypeMuxSubscription is a subscription established through TypeMux.
  136. type TypeMuxSubscription struct {
  137. mux *TypeMux
  138. created time.Time
  139. closeMu sync.Mutex
  140. closing chan struct{}
  141. closed bool
  142. // these two are the same channel. they are stored separately so
  143. // postC can be set to nil without affecting the return value of
  144. // Chan.
  145. postMu sync.RWMutex
  146. readC <-chan *TypeMuxEvent
  147. postC chan<- *TypeMuxEvent
  148. }
  149. func newsub(mux *TypeMux) *TypeMuxSubscription {
  150. c := make(chan *TypeMuxEvent)
  151. return &TypeMuxSubscription{
  152. mux: mux,
  153. created: time.Now(),
  154. readC: c,
  155. postC: c,
  156. closing: make(chan struct{}),
  157. }
  158. }
  159. func (s *TypeMuxSubscription) Chan() <-chan *TypeMuxEvent {
  160. return s.readC
  161. }
  162. func (s *TypeMuxSubscription) Unsubscribe() {
  163. s.mux.del(s)
  164. s.closewait()
  165. }
  166. func (s *TypeMuxSubscription) Closed() bool {
  167. s.closeMu.Lock()
  168. defer s.closeMu.Unlock()
  169. return s.closed
  170. }
  171. func (s *TypeMuxSubscription) closewait() {
  172. s.closeMu.Lock()
  173. defer s.closeMu.Unlock()
  174. if s.closed {
  175. return
  176. }
  177. close(s.closing)
  178. s.closed = true
  179. s.postMu.Lock()
  180. close(s.postC)
  181. s.postC = nil
  182. s.postMu.Unlock()
  183. }
  184. func (s *TypeMuxSubscription) deliver(event *TypeMuxEvent) {
  185. // Short circuit delivery if stale event
  186. if s.created.After(event.Time) {
  187. return
  188. }
  189. // Otherwise deliver the event
  190. s.postMu.RLock()
  191. defer s.postMu.RUnlock()
  192. select {
  193. case s.postC <- event:
  194. case <-s.closing:
  195. }
  196. }