package main
import (
"fmt"
abxbus "github.com/ArchiveBox/abxbus/abxbus-go"
)
type AuthService struct {
bus *abxbus.EventBus
}
type RelayService struct {
bus *abxbus.EventBus
}
type BillingService struct {
bus *abxbus.EventBus
}
func main() {
maxHistory := 100
zeroHistory := 0
auth := &AuthService{bus: abxbus.NewEventBus("AuthBus", &abxbus.EventBusOptions{
EventConcurrency: abxbus.EventConcurrencyBusSerial,
EventHandlerConcurrency: abxbus.EventHandlerConcurrencySerial,
MaxHistorySize: &maxHistory,
})}
relay := &RelayService{bus: abxbus.NewEventBus("RelayBus", &abxbus.EventBusOptions{
EventConcurrency: abxbus.EventConcurrencyParallel,
MaxHistorySize: &zeroHistory,
})}
billing := &BillingService{bus: abxbus.NewEventBus("BillingBus", &abxbus.EventBusOptions{
EventConcurrency: abxbus.EventConcurrencyBusSerial,
EventHandlerConcurrency: abxbus.EventHandlerConcurrencySerial,
MaxHistorySize: &maxHistory,
})}
auth.bus.On("UserCreatedEvent", "on_user_created", func(event *abxbus.BaseEvent) (any, error) {
return fmt.Sprintf("auth-ok:%s", event.Payload["user_id"]), nil
}, nil)
billing.bus.On("UserCreatedEvent", "on_user_created", func(event *abxbus.BaseEvent) (any, error) {
return fmt.Sprintf("billing-ok:%s", event.Payload["user_id"]), nil
}, nil)
auth.bus.OnEventName("*", "forward_to_relay", func(event *abxbus.BaseEvent) (any, error) {
return relay.bus.Emit(event), nil
}, nil)
relay.bus.OnEventName("*", "forward_to_billing", func(event *abxbus.BaseEvent) (any, error) {
return billing.bus.Emit(event), nil
}, nil)
event := auth.bus.Emit(abxbus.NewBaseEvent("UserCreatedEvent", map[string]any{"user_id": "u-a8d1"}))
result, err := event.EventResult()
if err != nil {
panic(err)
}
fmt.Println(result)
// auth-ok:u-a8d1
fmt.Println(event.EventPath)
// [AuthBus#a8d1 RelayBus#3f2c BillingBus#b91e]
}