bus.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. AddCtxHandler(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. listeners map[string][]HandlerFunc
  38. wildcardListeners []HandlerFunc
  39. txMng TransactionManager
  40. }
  41. // temp stuff, not sure how to handle bus instance, and init yet
  42. var globalBus = New()
  43. func New() Bus {
  44. bus := &InProcBus{}
  45. bus.handlers = make(map[string]HandlerFunc)
  46. bus.listeners = make(map[string][]HandlerFunc)
  47. bus.wildcardListeners = make([]HandlerFunc, 0)
  48. bus.txMng = &noopTransactionManager{}
  49. return bus
  50. }
  51. // Want to get rid of global bus
  52. func GetBus() Bus {
  53. return globalBus
  54. }
  55. func (b *InProcBus) SetTransactionManager(tm TransactionManager) {
  56. b.txMng = tm
  57. }
  58. func (b *InProcBus) DispatchCtx(ctx context.Context, msg Msg) error {
  59. var msgName = reflect.TypeOf(msg).Elem().Name()
  60. var handler = b.handlers[msgName]
  61. if handler == nil {
  62. return ErrHandlerNotFound
  63. }
  64. var params = make([]reflect.Value, 2)
  65. params[0] = reflect.ValueOf(ctx)
  66. params[1] = reflect.ValueOf(msg)
  67. ret := reflect.ValueOf(handler).Call(params)
  68. err := ret[0].Interface()
  69. if err == nil {
  70. return nil
  71. }
  72. return err.(error)
  73. }
  74. func (b *InProcBus) Dispatch(msg Msg) error {
  75. var msgName = reflect.TypeOf(msg).Elem().Name()
  76. var handler = b.handlers[msgName]
  77. if handler == nil {
  78. return ErrHandlerNotFound
  79. }
  80. var params = make([]reflect.Value, 1)
  81. params[0] = reflect.ValueOf(msg)
  82. ret := reflect.ValueOf(handler).Call(params)
  83. err := ret[0].Interface()
  84. if err == nil {
  85. return nil
  86. }
  87. return err.(error)
  88. }
  89. func (b *InProcBus) Publish(msg Msg) error {
  90. var msgName = reflect.TypeOf(msg).Elem().Name()
  91. var listeners = b.listeners[msgName]
  92. var params = make([]reflect.Value, 1)
  93. params[0] = reflect.ValueOf(msg)
  94. for _, listenerHandler := range listeners {
  95. ret := reflect.ValueOf(listenerHandler).Call(params)
  96. err := ret[0].Interface()
  97. if err != nil {
  98. return err.(error)
  99. }
  100. }
  101. for _, listenerHandler := range b.wildcardListeners {
  102. ret := reflect.ValueOf(listenerHandler).Call(params)
  103. err := ret[0].Interface()
  104. if err != nil {
  105. return err.(error)
  106. }
  107. }
  108. return nil
  109. }
  110. func (b *InProcBus) AddWildcardListener(handler HandlerFunc) {
  111. b.wildcardListeners = append(b.wildcardListeners, handler)
  112. }
  113. func (b *InProcBus) AddHandler(handler HandlerFunc) {
  114. handlerType := reflect.TypeOf(handler)
  115. queryTypeName := handlerType.In(0).Elem().Name()
  116. b.handlers[queryTypeName] = handler
  117. }
  118. func (b *InProcBus) AddCtxHandler(handler HandlerFunc) {
  119. handlerType := reflect.TypeOf(handler)
  120. queryTypeName := handlerType.In(1).Elem().Name()
  121. b.handlers[queryTypeName] = handler
  122. }
  123. func (b *InProcBus) AddEventListener(handler HandlerFunc) {
  124. handlerType := reflect.TypeOf(handler)
  125. eventName := handlerType.In(0).Elem().Name()
  126. _, exists := b.listeners[eventName]
  127. if !exists {
  128. b.listeners[eventName] = make([]HandlerFunc, 0)
  129. }
  130. b.listeners[eventName] = append(b.listeners[eventName], handler)
  131. }
  132. // Package level functions
  133. func AddHandler(implName string, handler HandlerFunc) {
  134. globalBus.AddHandler(handler)
  135. }
  136. // Package level functions
  137. func AddCtxHandler(implName string, handler HandlerFunc) {
  138. globalBus.AddCtxHandler(handler)
  139. }
  140. // Package level functions
  141. func AddEventListener(handler HandlerFunc) {
  142. globalBus.AddEventListener(handler)
  143. }
  144. func AddWildcardListener(handler HandlerFunc) {
  145. globalBus.AddWildcardListener(handler)
  146. }
  147. func Dispatch(msg Msg) error {
  148. return globalBus.Dispatch(msg)
  149. }
  150. func DispatchCtx(ctx context.Context, msg Msg) error {
  151. return globalBus.DispatchCtx(ctx, msg)
  152. }
  153. func Publish(msg Msg) error {
  154. return globalBus.Publish(msg)
  155. }
  156. // InTransaction starts a transaction and store it in the context.
  157. // The caller can then pass a function with multiple DispatchCtx calls that
  158. // all will be executed in the same transaction. InTransaction will rollback if the
  159. // callback returns an error.
  160. func InTransaction(ctx context.Context, fn func(ctx context.Context) error) error {
  161. return globalBus.InTransaction(ctx, fn)
  162. }
  163. func ClearBusHandlers() {
  164. globalBus = New()
  165. }
  166. type noopTransactionManager struct{}
  167. func (*noopTransactionManager) InTransaction(ctx context.Context, fn func(ctx context.Context) error) error {
  168. return nil
  169. }