nodes_distribute

package
v0.80.2 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2026 License: Apache-2.0 Imports: 10 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package nodes_distribute provides distribute.map (bounded-concurrency fan-out over a slice) and distribute.reduce (fold the map output into a single value).

Typical use:

{ action: "log.info", items: [1, 2, 3], concurrency: 4 } -> distribute.map
{ strategy: "all_pass", items: .results } -> distribute.reduce

Index

Constants

View Source
const ID = "nodes_distribute"

Variables

View Source
var DistributeMap = action.New("distribute.map", func(ctx context.Context, req DistributeMapReq) (DistributeMapRes, error) {
	if req.Action == "" {
		return DistributeMapRes{}, xerr.BadRequest("distribute.map: action is required")
	}
	timeout, err := mapTimeoutDuration(req.TimeoutMs)
	if err != nil {
		return DistributeMapRes{}, err
	}
	if len(req.Items) == 0 {
		return DistributeMapRes{}, nil
	}
	if len(req.Items) > maxItems {
		return DistributeMapRes{}, xerr.BadRequest("distribute.map: items count exceeds limit")
	}

	resolver := contracts.ActionResolverFromContext(ctx)
	if resolver == nil {
		return DistributeMapRes{}, xerr.Internal("distribute.map: no action resolver in context")
	}

	target, ok := resolver.Action(req.Action)
	if !ok {
		return DistributeMapRes{}, xerr.NotFound("distribute.map: action not found: " + req.Action)
	}

	concurrency := req.Concurrency
	if concurrency <= 0 {
		concurrency = defaultConcurrency
	}
	if concurrency > maxConcurrency {
		concurrency = maxConcurrency
	}

	runCtx, cancel := context.WithCancel(ctx)
	defer cancel()
	var firstErr error
	var firstErrOnce sync.Once

	builder := action.Dynamic(target)
	builder = builder.HookBefore(func(itemCtx context.Context, _ any, _ *action.Meta) (context.Context, error) {
		if err := itemCtx.Err(); err != nil {
			return itemCtx, &fanoutNotStartedError{cause: err}
		}
		return itemCtx, nil
	})
	if timeout > 0 {
		builder = builder.Timeout(timeout)
	}
	if req.Retries > 0 {
		builder = builder.Retry(req.Retries, action.ExponentialJitter(10*time.Millisecond, 2*time.Second))
	}
	if req.RateLimit > 0 {
		builder = builder.RateLimit(float64(req.RateLimit), max(req.RateLimit/10, 1))
	}
	if req.FailFast {
		builder = builder.HookAfter(func(_ context.Context, _, _ any, err error, _ *action.Meta) {
			if err != nil {
				if notStarted, ok := errors.AsType[*fanoutNotStartedError](err); ok && notStarted != nil {
					return
				}
				firstErrOnce.Do(func() {
					firstErr = err
					cancel()
				})
			}
		})
	}
	execTarget := builder.Build()

	results := action.FanOut(runCtx, execTarget, req.Items, concurrency)
	if req.FailFast {
		if firstErr != nil {
			return DistributeMapRes{}, firstErr
		}
		for _, result := range results {
			if result.Err != nil {
				return DistributeMapRes{}, normalizeFanoutError(result.Err)
			}
		}
	}

	items := make([]Item, len(results))
	for i, result := range results {
		if result.Err != nil {
			items[i] = Item{Error: normalizeFanoutError(result.Err).Error()}
			continue
		}
		items[i] = Item{OK: true, Result: result.Value}
	}

	result := DistributeMapRes{Items: items}
	for i := range items {
		if items[i].OK {
			result.Succeeded++
		} else {
			result.Failed++
		}
	}
	return result, nil
}).Description("Fan out one action over many items with Kernel rate-limiting, retry, and timeout").
	Tag("distribute", "fanout").
	Build()
View Source
var DistributeReduce = action.New("distribute.reduce", func(_ context.Context, req DistributeReduceReq) (DistributeReduceRes, error) {
	return runReduce(req)
}).Description("Fold distribute.map output into a single value").
	Tag("distribute", "fold").
	Build()

DistributeReduce folds a distribute.map result into a single value.

Functions

func Bundle

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

func Library

func Library() action.Library

Types

type DistributeMapReq

type DistributeMapReq struct {
	Action      string `json:"action"                 validate:"required"`
	Concurrency int    `json:"concurrency,omitempty"`
	Items       []any  `json:"items"                  validate:"required"`
	RateLimit   int    `json:"rate_limit,omitempty"`
	TimeoutMs   int64  `json:"timeout_ms,omitempty"`
	Retries     int    `json:"retries,omitempty"`
	FailFast    bool   `json:"fail_fast,omitempty"`
}

type DistributeMapRes

type DistributeMapRes struct {
	Items     []Item `json:"items"`
	Succeeded int    `json:"succeeded"`
	Failed    int    `json:"failed"`
}

type DistributeReduceReq

type DistributeReduceReq struct {
	Strategy string `json:"strategy" validate:"required,oneof=collect all_pass any_pass first_success count"`
	Items    []Item `json:"items"    validate:"required"`
}

DistributeReduceReq is the reduce configuration. Strategy selects the fold: collect, all_pass, any_pass, first_success, count.

type DistributeReduceRes

type DistributeReduceRes struct {
	Strategy  string `json:"strategy"`
	AllPass   bool   `json:"all_pass,omitempty"`
	AnyPass   bool   `json:"any_pass,omitempty"`
	Succeeded int    `json:"succeeded,omitempty"`
	Failed    int    `json:"failed,omitempty"`
	Result    any    `json:"result,omitempty"`
	Results   []any  `json:"results,omitempty"`
}

DistributeReduceRes carries only the field selected by the strategy.

type Item

type Item struct {
	OK     bool   `json:"ok"`
	Result any    `json:"result,omitempty"`
	Error  string `json:"error,omitempty"`
}

Item is one entry in a distribute.map result. Exactly one of Result or Error is populated per item.

Jump to

Keyboard shortcuts

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