bus.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. package bus
  2. import (
  3. "context"
  4. "errors"
  5. "reflect"
  6. )
  7. type HandlerFunc interface{}
  8. type CtxHandlerFunc func()
  9. type Msg interface{}
  10. var ErrHandlerNotFound = errors.New("handler not found")
  11. type TransactionManager interface {
  12. InTransaction(ctx context.Context, fn func(ctx context.Context) error) error
  13. }
  14. type Bus interface {
  15. Dispatch(msg Msg) error
  16. DispatchCtx(ctx context.Context, msg Msg) error
  17. Publish(msg Msg) error
  18. // InTransaction starts a transaction and store it in the context.
  19. // The caller can then pass a function with multiple DispatchCtx calls that
  20. // all will be executed in the same transaction. InTransaction will rollback if the
  21. // callback returns an error.
  22. InTransaction(ctx context.Context, fn func(ctx context.Context) error) error
  23. AddHandler(handler HandlerFunc)
  24. AddHandlerCtx(handler HandlerFunc)
  25. AddEventListener(handler HandlerFunc)
  26. AddWildcardListener(handler HandlerFunc)
  27. // SetTransactionManager allows the user to replace the internal
  28. // noop TransactionManager that is responsible for manageing
  29. // transactions in `InTransaction`
  30. SetTransactionManager(tm TransactionManager)
  31. }
  32. func (b *InProcBus) InTransaction(ctx context.Context, fn func(ctx context.Context) error) error {
  33. return b.txMng.InTransaction(ctx, fn)
  34. }
  35. type InProcBus struct {
  36. handlers map[string]HandlerFunc
  37. handlersWithCtx map[string]HandlerFunc
  38. listeners map[string][]HandlerFunc
  39. wildcardListeners []HandlerFunc
  40. txMng TransactionManager
  41. }
  42. // temp stuff, not sure how to handle bus instance, and init yet
  43. var globalBus = New()
  44. func New() Bus {
  45. bus := &InProcBus{}
  46. bus.handlers = make(map[string]HandlerFunc)
  47. bus.handlersWithCtx = make(map[string]HandlerFunc)
  48. bus.listeners = make(map[string][]HandlerFunc)
  49. bus.wildcardListeners = make([]HandlerFunc, 0)
  50. bus.txMng = &noopTransactionManager{}
  51. return bus
  52. }
  53. // Want to get rid of global bus
  54. func GetBus() Bus {
  55. return globalBus
  56. }
  57. func (b *InProcBus) SetTransactionManager(tm TransactionManager) {
  58. b.txMng = tm
  59. }
  60. func (b *InProcBus) DispatchCtx(ctx context.Context, msg Msg) error {
  61. var msgName = reflect.TypeOf(msg).Elem().Name()
  62. var handler = b.handlersWithCtx[msgName]
  63. if handler == nil {
  64. return ErrHandlerNotFound
  65. }
  66. var params = []reflect.Value{}
  67. params = append(params, reflect.ValueOf(ctx))
  68. params = append(params, reflect.ValueOf(msg))
  69. ret := reflect.ValueOf(handler).Call(params)
  70. err := ret[0].Interface()
  71. if err == nil {
  72. return nil
  73. }
  74. return err.(error)
  75. }
  76. func (b *InProcBus) Dispatch(msg Msg) error {
  77. var msgName = reflect.TypeOf(msg).Elem().Name()
  78. var handler = b.handlersWithCtx[msgName]
  79. withCtx := true
  80. if handler == nil {
  81. withCtx = false
  82. handler = b.handlers[msgName]
  83. }
  84. if handler == nil {
  85. return ErrHandlerNotFound
  86. }
  87. var params = []reflect.Value{}
  88. if withCtx {
  89. params = append(params, reflect.ValueOf(context.Background()))
  90. }
  91. params = append(params, reflect.ValueOf(msg))
  92. ret := reflect.ValueOf(handler).Call(params)
  93. err := ret[0].Interface()
  94. if err == nil {
  95. return nil
  96. }
  97. return err.(error)
  98. }
  99. func (b *InProcBus) Publish(msg Msg) error {
  100. var msgName = reflect.TypeOf(msg).Elem().Name()
  101. var listeners = b.listeners[msgName]
  102. var params = make([]reflect.Value, 1)
  103. params[0] = reflect.ValueOf(msg)
  104. for _, listenerHandler := range listeners {
  105. ret := reflect.ValueOf(listenerHandler).Call(params)
  106. err := ret[0].Interface()
  107. if err != nil {
  108. return err.(error)
  109. }
  110. }
  111. for _, listenerHandler := range b.wildcardListeners {
  112. ret := reflect.ValueOf(listenerHandler).Call(params)
  113. err := ret[0].Interface()
  114. if err != nil {
  115. return err.(error)
  116. }
  117. }
  118. return nil
  119. }
  120. func (b *InProcBus) AddWildcardListener(handler HandlerFunc) {
  121. b.wildcardListeners = append(b.wildcardListeners, handler)
  122. }
  123. func (b *InProcBus) AddHandler(handler HandlerFunc) {
  124. handlerType := reflect.TypeOf(handler)
  125. queryTypeName := handlerType.In(0).Elem().Name()
  126. b.handlers[queryTypeName] = handler
  127. }
  128. func (b *InProcBus) AddHandlerCtx(handler HandlerFunc) {
  129. handlerType := reflect.TypeOf(handler)
  130. queryTypeName := handlerType.In(1).Elem().Name()
  131. b.handlersWithCtx[queryTypeName] = handler
  132. }
  133. func (b *InProcBus) AddEventListener(handler HandlerFunc) {
  134. handlerType := reflect.TypeOf(handler)
  135. eventName := handlerType.In(0).Elem().Name()
  136. _, exists := b.listeners[eventName]
  137. if !exists {
  138. b.listeners[eventName] = make([]HandlerFunc, 0)
  139. }
  140. b.listeners[eventName] = append(b.listeners[eventName], handler)
  141. }
  142. // Package level functions
  143. func AddHandler(implName string, handler HandlerFunc) {
  144. globalBus.AddHandler(handler)
  145. }
  146. // Package level functions
  147. func AddHandlerCtx(implName string, handler HandlerFunc) {
  148. globalBus.AddHandlerCtx(handler)
  149. }
  150. // Package level functions
  151. func AddEventListener(handler HandlerFunc) {
  152. globalBus.AddEventListener(handler)
  153. }
  154. func AddWildcardListener(handler HandlerFunc) {
  155. globalBus.AddWildcardListener(handler)
  156. }
  157. func Dispatch(msg Msg) error {
  158. return globalBus.Dispatch(msg)
  159. }
  160. func DispatchCtx(ctx context.Context, msg Msg) error {
  161. return globalBus.DispatchCtx(ctx, msg)
  162. }
  163. func Publish(msg Msg) error {
  164. return globalBus.Publish(msg)
  165. }
  166. // InTransaction starts a transaction and store it in the context.
  167. // The caller can then pass a function with multiple DispatchCtx calls that
  168. // all will be executed in the same transaction. InTransaction will rollback if the
  169. // callback returns an error.
  170. func InTransaction(ctx context.Context, fn func(ctx context.Context) error) error {
  171. return globalBus.InTransaction(ctx, fn)
  172. }
  173. func ClearBusHandlers() {
  174. globalBus = New()
  175. }
  176. type noopTransactionManager struct{}
  177. func (*noopTransactionManager) InTransaction(ctx context.Context, fn func(ctx context.Context) error) error {
  178. return fn(ctx)
  179. }