nodes_dispatch

package
v0.12.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 2, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package nodes_dispatch provides the `dispatch` action, which selects one action from a named pool (or explicit member list) and invokes it with the supplied payload. On failure it falls through the remaining members.

Typical use:

{ members: "log.info,noop", payload: { value: "x" } } -> dispatch
{ pool: "workers", chosen: "log.info", payload: .data } -> dispatch

Index

Constants

View Source
const ID = "nodes_dispatch"

Variables

View Source
var Dispatch = action.New("dispatch", func(ctx context.Context, req DispatchReq) (any, error) {
	resolver := contracts.ActionResolverFromContext(ctx)
	if resolver == nil {
		return nil, xerr.Internal("dispatch: no action resolver in context")
	}

	members := req.Members
	if members == "" && req.Pool != "" {
		pools := contracts.PoolsFromContext(ctx)
		if pools == nil {
			return nil, xerr.Internal("dispatch: pool table not available in context")
		}
		listed, ok := pools[req.Pool]
		if !ok {
			return nil, xerr.NotFound("dispatch: pool not declared: " + req.Pool)
		}
		members = strings.Join(listed, ",")
	}

	names := splitMembers(members)
	if len(names) == 0 {
		return nil, xerr.BadRequest("dispatch: members is empty")
	}

	if req.Chosen != "" {
		if target, ok := resolver.Action(req.Chosen); ok {
			if output, err := action.InvokeAny(ctx, target, req.Payload); err == nil {
				return output, nil
			}
		}
	}

	if req.Fallback != "" {
		if target, ok := resolver.Action(req.Fallback); ok {
			if output, err := action.InvokeAny(ctx, target, req.Payload); err == nil {
				return output, nil
			}
		}
	}

	var lastErr error
	for _, name := range names {
		target, ok := resolver.Action(name)
		if !ok {
			lastErr = xerr.NotFound("dispatch: member not registered: " + name)
			continue
		}
		output, err := action.InvokeAny(ctx, target, req.Payload)
		if err == nil {
			return output, nil
		}
		lastErr = err
	}
	if lastErr == nil {
		lastErr = xerr.NotFound("dispatch: no member succeeded")
	}
	return nil, lastErr
}).Description("Selects one action from a pool or list and invokes it").
	Tag("dispatch", "pool", "routing").
	Build()

Functions

func Bundle

func Bundle(_ map[string]string) core.Bundle

Types

type DispatchReq

type DispatchReq struct {
	Pool     string `json:"pool,omitempty"`
	Members  string `json:"members,omitempty"`
	Chosen   string `json:"chosen,omitempty"`
	Fallback string `json:"fallback,omitempty"`
	Payload  any    `json:"payload,omitempty"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL