grafana/pkg/bus/bus.go

168 lines
3.8 KiB
Go
Raw Normal View History

2014-11-28 15:16:49 -06:00
package bus
import (
"context"
2015-06-26 14:32:24 -05:00
"fmt"
2014-11-28 15:16:49 -06:00
"reflect"
)
2014-12-16 09:45:07 -06:00
type HandlerFunc interface{}
2016-12-13 21:15:35 -06:00
type CtxHandlerFunc func()
2014-12-16 09:45:07 -06:00
type Msg interface{}
2014-11-28 15:16:49 -06:00
2014-12-01 10:56:03 -06:00
type Bus interface {
2014-12-16 09:45:07 -06:00
Dispatch(msg Msg) error
DispatchCtx(ctx context.Context, msg Msg) error
2015-01-09 09:36:23 -06:00
Publish(msg Msg) error
2014-12-16 09:45:07 -06:00
AddHandler(handler HandlerFunc)
AddCtxHandler(handler HandlerFunc)
2015-01-09 09:36:23 -06:00
AddEventListener(handler HandlerFunc)
AddWildcardListener(handler HandlerFunc)
2014-12-01 10:56:03 -06:00
}
type InProcBus struct {
handlers map[string]HandlerFunc
listeners map[string][]HandlerFunc
wildcardListeners []HandlerFunc
2014-12-01 10:56:03 -06:00
}
2014-11-28 15:16:49 -06:00
2014-12-16 05:04:08 -06:00
// temp stuff, not sure how to handle bus instance, and init yet
var globalBus = New()
2014-12-01 10:56:03 -06:00
func New() Bus {
bus := &InProcBus{}
2014-12-16 09:45:07 -06:00
bus.handlers = make(map[string]HandlerFunc)
2015-01-09 09:36:23 -06:00
bus.listeners = make(map[string][]HandlerFunc)
bus.wildcardListeners = make([]HandlerFunc, 0)
2014-12-01 10:56:03 -06:00
return bus
2014-11-28 15:16:49 -06:00
}
func (b *InProcBus) DispatchCtx(ctx context.Context, msg Msg) error {
var msgName = reflect.TypeOf(msg).Elem().Name()
var handler = b.handlers[msgName]
if handler == nil {
return fmt.Errorf("handler not found for %s", msgName)
}
var params = make([]reflect.Value, 2)
params[0] = reflect.ValueOf(ctx)
params[1] = reflect.ValueOf(msg)
ret := reflect.ValueOf(handler).Call(params)
err := ret[0].Interface()
if err == nil {
return nil
} else {
return err.(error)
}
}
2014-12-16 09:45:07 -06:00
func (b *InProcBus) Dispatch(msg Msg) error {
var msgName = reflect.TypeOf(msg).Elem().Name()
2014-11-28 15:16:49 -06:00
2014-12-16 09:45:07 -06:00
var handler = b.handlers[msgName]
2014-11-28 15:16:49 -06:00
if handler == nil {
2015-06-26 14:32:24 -05:00
return fmt.Errorf("handler not found for %s", msgName)
2014-11-28 15:16:49 -06:00
}
2014-12-16 09:45:07 -06:00
2014-11-28 15:16:49 -06:00
var params = make([]reflect.Value, 1)
2014-12-16 09:45:07 -06:00
params[0] = reflect.ValueOf(msg)
2014-11-28 15:16:49 -06:00
ret := reflect.ValueOf(handler).Call(params)
err := ret[0].Interface()
if err == nil {
return nil
} else {
return err.(error)
}
}
2015-01-09 09:36:23 -06:00
func (b *InProcBus) Publish(msg Msg) error {
var msgName = reflect.TypeOf(msg).Elem().Name()
var listeners = b.listeners[msgName]
var params = make([]reflect.Value, 1)
params[0] = reflect.ValueOf(msg)
for _, listenerHandler := range listeners {
2015-01-09 09:36:23 -06:00
ret := reflect.ValueOf(listenerHandler).Call(params)
err := ret[0].Interface()
if err != nil {
return err.(error)
}
}
for _, listenerHandler := range b.wildcardListeners {
ret := reflect.ValueOf(listenerHandler).Call(params)
err := ret[0].Interface()
if err != nil {
return err.(error)
}
}
2015-01-09 09:36:23 -06:00
return nil
}
func (b *InProcBus) AddWildcardListener(handler HandlerFunc) {
b.wildcardListeners = append(b.wildcardListeners, handler)
}
2014-12-16 09:45:07 -06:00
func (b *InProcBus) AddHandler(handler HandlerFunc) {
2014-11-28 15:16:49 -06:00
handlerType := reflect.TypeOf(handler)
queryTypeName := handlerType.In(0).Elem().Name()
2014-12-16 09:45:07 -06:00
b.handlers[queryTypeName] = handler
2014-11-28 15:16:49 -06:00
}
2014-12-16 05:04:08 -06:00
func (b *InProcBus) AddCtxHandler(handler HandlerFunc) {
handlerType := reflect.TypeOf(handler)
queryTypeName := handlerType.In(1).Elem().Name()
b.handlers[queryTypeName] = handler
}
2015-01-09 09:36:23 -06:00
func (b *InProcBus) AddEventListener(handler HandlerFunc) {
handlerType := reflect.TypeOf(handler)
eventName := handlerType.In(0).Elem().Name()
_, exists := b.listeners[eventName]
2015-01-09 09:36:23 -06:00
if !exists {
b.listeners[eventName] = make([]HandlerFunc, 0)
2015-01-09 09:36:23 -06:00
}
b.listeners[eventName] = append(b.listeners[eventName], handler)
2015-01-09 09:36:23 -06:00
}
2014-12-16 05:04:08 -06:00
// Package level functions
2014-12-16 09:45:07 -06:00
func AddHandler(implName string, handler HandlerFunc) {
globalBus.AddHandler(handler)
2014-12-16 05:04:08 -06:00
}
// Package level functions
func AddCtxHandler(implName string, handler HandlerFunc) {
globalBus.AddCtxHandler(handler)
}
2015-01-09 09:36:23 -06:00
// Package level functions
func AddEventListener(handler HandlerFunc) {
globalBus.AddEventListener(handler)
}
func AddWildcardListener(handler HandlerFunc) {
globalBus.AddWildcardListener(handler)
}
2014-12-16 09:45:07 -06:00
func Dispatch(msg Msg) error {
return globalBus.Dispatch(msg)
2014-12-16 05:04:08 -06:00
}
func DispatchCtx(ctx context.Context, msg Msg) error {
return globalBus.DispatchCtx(ctx, msg)
}
func Publish(msg Msg) error {
return globalBus.Publish(msg)
}
func ClearBusHandlers() {
globalBus = New()
}