2014-11-28 15:16:49 -06:00
|
|
|
package bus
|
|
|
|
|
|
|
|
import (
|
2016-10-03 02:38:03 -05:00
|
|
|
"context"
|
2018-04-27 06:41:58 -05:00
|
|
|
"errors"
|
2020-11-17 04:27:45 -06:00
|
|
|
"fmt"
|
2014-11-28 15:16:49 -06:00
|
|
|
"reflect"
|
2020-11-27 07:58:45 -06:00
|
|
|
|
2022-01-20 04:10:12 -06:00
|
|
|
"go.opentelemetry.io/otel/attribute"
|
2022-12-15 05:58:48 -06:00
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/infra/tracing"
|
2014-11-28 15:16:49 -06:00
|
|
|
)
|
|
|
|
|
2019-06-04 15:00:05 -05:00
|
|
|
// HandlerFunc defines a handler function interface.
|
2014-12-16 09:45:07 -06:00
|
|
|
type HandlerFunc interface{}
|
2019-06-04 15:00:05 -05:00
|
|
|
|
|
|
|
// Msg defines a message interface.
|
2014-12-16 09:45:07 -06:00
|
|
|
type Msg interface{}
|
2014-11-28 15:16:49 -06:00
|
|
|
|
2022-12-15 05:58:48 -06:00
|
|
|
// ErrHandlerNotFound defines an error if a handler is not found.
|
2018-04-27 06:41:58 -05:00
|
|
|
var ErrHandlerNotFound = errors.New("handler not found")
|
|
|
|
|
2022-12-15 05:58:48 -06:00
|
|
|
// Bus type defines the bus interface structure.
|
2014-12-01 10:56:03 -06:00
|
|
|
type Bus interface {
|
2022-01-04 02:36:01 -06:00
|
|
|
Publish(ctx context.Context, msg Msg) error
|
|
|
|
AddEventListener(handler HandlerFunc)
|
2018-06-05 14:13:53 -05:00
|
|
|
}
|
|
|
|
|
2022-12-15 05:58:48 -06:00
|
|
|
// InProcBus defines the bus structure.
|
2014-12-01 10:56:03 -06:00
|
|
|
type InProcBus struct {
|
2022-06-14 09:07:41 -05:00
|
|
|
listeners map[string][]HandlerFunc
|
|
|
|
tracer tracing.Tracer
|
2014-12-01 10:56:03 -06:00
|
|
|
}
|
2014-11-28 15:16:49 -06:00
|
|
|
|
2022-01-20 04:10:12 -06:00
|
|
|
func ProvideBus(tracer tracing.Tracer) *InProcBus {
|
2022-06-14 09:07:41 -05:00
|
|
|
return &InProcBus{
|
|
|
|
listeners: make(map[string][]HandlerFunc),
|
|
|
|
tracer: tracer,
|
2021-07-15 07:33:38 -05:00
|
|
|
}
|
2014-11-28 15:16:49 -06:00
|
|
|
}
|
|
|
|
|
2022-12-15 05:58:48 -06:00
|
|
|
// Publish function publish a message to the bus listener.
|
2022-01-04 02:36:01 -06:00
|
|
|
func (b *InProcBus) Publish(ctx context.Context, msg Msg) error {
|
2021-11-19 06:56:42 -06:00
|
|
|
var msgName = reflect.TypeOf(msg).Elem().Name()
|
|
|
|
|
|
|
|
var params = []reflect.Value{}
|
2022-06-14 09:07:41 -05:00
|
|
|
if listeners, exists := b.listeners[msgName]; exists {
|
2021-11-19 06:56:42 -06:00
|
|
|
params = append(params, reflect.ValueOf(ctx))
|
|
|
|
params = append(params, reflect.ValueOf(msg))
|
2021-11-19 07:32:14 -06:00
|
|
|
if err := callListeners(listeners, params); err != nil {
|
2021-11-19 06:56:42 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-20 04:10:12 -06:00
|
|
|
_, span := b.tracer.Start(ctx, "bus - "+msgName)
|
|
|
|
defer span.End()
|
2021-11-19 06:56:42 -06:00
|
|
|
|
2022-01-20 04:10:12 -06:00
|
|
|
span.SetAttributes("msg", msgName, attribute.Key("msg").String(msgName))
|
2021-11-19 06:56:42 -06:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-19 07:32:14 -06:00
|
|
|
func callListeners(listeners []HandlerFunc, params []reflect.Value) error {
|
2015-02-03 09:57:42 -06:00
|
|
|
for _, listenerHandler := range listeners {
|
2015-01-09 09:36:23 -06:00
|
|
|
ret := reflect.ValueOf(listenerHandler).Call(params)
|
2020-11-17 04:27:45 -06:00
|
|
|
e := ret[0].Interface()
|
|
|
|
if e != nil {
|
|
|
|
err, ok := e.(error)
|
|
|
|
if ok {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return fmt.Errorf("expected listener to return an error, got '%T'", e)
|
2015-01-09 09:36:23 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-04 02:36:01 -06:00
|
|
|
func (b *InProcBus) AddEventListener(handler HandlerFunc) {
|
2021-11-19 06:56:42 -06:00
|
|
|
handlerType := reflect.TypeOf(handler)
|
|
|
|
eventName := handlerType.In(1).Elem().Name()
|
2022-06-14 09:07:41 -05:00
|
|
|
_, exists := b.listeners[eventName]
|
2021-11-19 06:56:42 -06:00
|
|
|
if !exists {
|
2022-06-14 09:07:41 -05:00
|
|
|
b.listeners[eventName] = make([]HandlerFunc, 0)
|
2021-11-19 06:56:42 -06:00
|
|
|
}
|
2022-06-14 09:07:41 -05:00
|
|
|
b.listeners[eventName] = append(b.listeners[eventName], handler)
|
2021-11-19 06:56:42 -06:00
|
|
|
}
|