mirror of
https://github.com/jwetzell/showbridge-go.git
synced 2026-04-26 21:05:30 +00:00
process routes concurrently
This commit is contained in:
61
router.go
61
router.go
@@ -131,42 +131,49 @@ func (r *Router) Stop() {
|
|||||||
func (r *Router) HandleInput(ctx context.Context, sourceId string, payload any) (bool, []route.RouteIOError) {
|
func (r *Router) HandleInput(ctx context.Context, sourceId string, payload any) (bool, []route.RouteIOError) {
|
||||||
var routeIOErrors []route.RouteIOError
|
var routeIOErrors []route.RouteIOError
|
||||||
routeFound := false
|
routeFound := false
|
||||||
|
|
||||||
|
var routeWaitGroup sync.WaitGroup
|
||||||
|
|
||||||
for routeIndex, routeInstance := range r.RouteInstances {
|
for routeIndex, routeInstance := range r.RouteInstances {
|
||||||
if routeInstance.Input() == sourceId {
|
if routeInstance.Input() == sourceId {
|
||||||
routeFound = true
|
routeWaitGroup.Go(func() {
|
||||||
routeContext := context.WithValue(ctx, route.SourceContextKey, sourceId)
|
|
||||||
|
|
||||||
payload, err := routeInstance.ProcessPayload(routeContext, payload)
|
routeFound = true
|
||||||
if err != nil {
|
routeContext := context.WithValue(ctx, route.SourceContextKey, sourceId)
|
||||||
if routeIOErrors == nil {
|
|
||||||
routeIOErrors = []route.RouteIOError{}
|
payload, err := routeInstance.ProcessPayload(routeContext, payload)
|
||||||
|
if err != nil {
|
||||||
|
if routeIOErrors == nil {
|
||||||
|
routeIOErrors = []route.RouteIOError{}
|
||||||
|
}
|
||||||
|
r.logger.Error("unable to process input", "route", routeIndex, "source", sourceId, "error", err)
|
||||||
|
routeIOErrors = append(routeIOErrors, route.RouteIOError{
|
||||||
|
Index: routeIndex,
|
||||||
|
ProcessError: err,
|
||||||
|
})
|
||||||
|
return
|
||||||
}
|
}
|
||||||
r.logger.Error("unable to process input", "route", routeIndex, "source", sourceId, "error", err)
|
|
||||||
routeIOErrors = append(routeIOErrors, route.RouteIOError{
|
|
||||||
Index: routeIndex,
|
|
||||||
ProcessError: err,
|
|
||||||
})
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if payload == nil {
|
if payload == nil {
|
||||||
r.logger.Error("no input after processing", "route", routeIndex, "source", sourceId)
|
r.logger.Error("no input after processing", "route", routeIndex, "source", sourceId)
|
||||||
continue
|
return
|
||||||
}
|
|
||||||
|
|
||||||
outputErrors := r.HandleOutput(routeContext, routeInstance.Output(), payload)
|
|
||||||
if outputErrors != nil {
|
|
||||||
if routeIOErrors == nil {
|
|
||||||
routeIOErrors = []route.RouteIOError{}
|
|
||||||
}
|
}
|
||||||
routeIOErrors = append(routeIOErrors, route.RouteIOError{
|
|
||||||
Index: routeIndex,
|
outputErrors := r.HandleOutput(routeContext, routeInstance.Output(), payload)
|
||||||
OutputErrors: outputErrors,
|
if outputErrors != nil {
|
||||||
})
|
if routeIOErrors == nil {
|
||||||
}
|
routeIOErrors = []route.RouteIOError{}
|
||||||
|
}
|
||||||
|
routeIOErrors = append(routeIOErrors, route.RouteIOError{
|
||||||
|
Index: routeIndex,
|
||||||
|
OutputErrors: outputErrors,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
routeWaitGroup.Wait()
|
||||||
return routeFound, routeIOErrors
|
return routeFound, routeIOErrors
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user