定制 flyokai/amp-data-pipeline 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

flyokai/amp-data-pipeline

Composer 安装命令:

composer require flyokai/amp-data-pipeline

包简介

Async data pipeline using amphp

README 文档

README

User docs → README.md · Agent quick-ref → CLAUDE.md · Agent deep dive → AGENTS.md

Composable async data pipelines on AMPHP 3.x — sources, processors, batching, multicast, with explicit concurrency and backpressure controls.

A small toolkit for building pull-based concurrent processing pipelines. You wire together a DataSource and one or more Processors; each processor controls its own fiber count and output queue size. Batch and multicast operators give you the rest.

Features

  • DataItem wrapper — data + meta arrays, immutable-ish
  • SourcesArraySource, IteratorSource, QueueSource
  • ProcessorsProcessorAbstract (override processDataItem()), SkipProcessor
  • CompositionProcessorComposition chains stages sequentially
  • BatchingBatch\BatchProcessor groups items and creates a per-batch processor
  • MulticastDataCast\MultiCastProcessor fans out each item to parallel cast processors
  • Per-stage concurrency — fiber count, buffer size, optional ordering
  • Cancellation propagated through the chain

Installation

composer require flyokai/amp-data-pipeline

Quick start

use Flyokai\AmpDataPipeline\{ArraySource, DataItem, ProcessorAbstract, ProcessorComposition};

final class Upper extends ProcessorAbstract
{
    protected function processDataItem(DataItem $item): void
    {
        $item->setData('value', strtoupper($item->getData('value')));
        $this->releaseDataItem($item);
    }
}

$source = new ArraySource([
    DataItem::fromArray(['value' => 'alice'], []),
    DataItem::fromArray(['value' => 'bob'],   []),
]);

$pipeline = new ProcessorComposition([new Upper()]);
$pipeline->setSource($source);

$pipeline->run(function (DataItem $item) {
    echo $item->getData('value'), "\n";   // ALICE, BOB
});

Concepts

DataItem

$item->getData('key');          // payload access
$item->setData('key', 'value'); // returns mutated
$item->getMeta();               // metadata bag

Sources

Class Use case
ArraySource wraps a PHP array (ConcurrentArrayIterator)
IteratorSource wraps any iterable
QueueSource wraps an AMPHP Queue for push-based input

Processors

ProcessorAbstract gives you:

  • setConcurrency(int) — fiber count inside the stage
  • setBufferSize(int) — output queue depth (0 = same as concurrency)
  • setCancellation(Cancellation) — graceful shutdown
  • releaseDataItem(DataItem) — push to output
new MyProcessor()
    ->setConcurrency(8)
    ->setBufferSize(16);

Linear pipeline

$pipeline = new ProcessorComposition([
    new PrepareProcessor(),
    new ValidateProcessor(),
    new SaveProcessor(),
]);
$pipeline->setSource(new ArraySource($rows));
$pipeline->run(/* optional itemCallback */);

Batching

use Flyokai\AmpDataPipeline\Batch\BatchProcessor;

$batcher = new BatchProcessor(
    batchProcessorFactory: fn() => new SaveBatchProcessor(),
    resultHandlerFactory:  fn() => new ResultRouter(),
    batchSize: 100,
    ordered: false,        // true → preserve order across batches
    groupResults: false,   // true → merge batch results into one DataItem
    throwIfUnhandled: true,
);

Items accumulate up to batchSize, a fresh processor is built for each batch, and results are routed through a DataItemHandler strategy.

Multicast

use Flyokai\AmpDataPipeline\DataCast\MultiCastProcessor;

$cast = new MultiCastProcessor(
    castProcessorFactories: [
        fn() => new IndexInOpensearch(),
        fn() => new WriteToCache(),
    ],
    groupResults: true,
    groupBufferSize: 10,
);

Each input item is delivered to every cast processor in parallel; outputs are aggregated by MultiCastConsumer.

Handler strategies

DataItemHandler is the strategy for handling specific items:

interface DataItemHandler {
    public function canHandle(DataItem $item): bool;
    public function handle(DataItem $item): Future;
}

HandlerComposition enforces mutual exclusion — exactly one handler per item; multiple matches throw. Pass $ordered = true to preserve item order via Mutex / Sequence.

Concurrency model

  • Inter-stage: a ProcessorComposition chains iterators (pull-based, demand-driven).
  • Intra-stage: concurrency controls the number of fibers servicing the queue inside a processor.
  • Buffer: bufferSize decouples producer/consumer (set to 0 to mirror concurrency).
  • Multicast: every cast processor fires simultaneously per item.
  • Backpressure: queue + groupBufferSize cap memory growth.

Gotchas

  • Order is not preserved by default. Use $ordered = true on BatchProcessor or HandlerComposition if you need it.
  • reset() requires queue completion. Resetting an incomplete queue throws RuntimeException.
  • Handler exclusivity — a HandlerComposition enforces one handler per item. Two matching handlers throw.
  • CastProcessorProcessor. Cast processors receive a raw ConcurrentIterator and own their queues.
  • groupBufferSize = 0 is unlimited. Multicast with grouping can grow memory unboundedly.
  • Cancellation must propagate. ProcessorComposition propagates to children automatically, but custom compositions need to do this explicitly.
  • Reflection in error handlingerrorDisposeQueue() reaches into Queue internals via reflection. AMPHP version updates may break it.

See also

  • flyokai/indexer — full reindex uses pipelines
  • Bulk data-import services typically use this as their processing core (DataSource → BatchProcessor → Stage Pipeline).

License

MIT

flyokai/amp-data-pipeline 适用场景与选型建议

flyokai/amp-data-pipeline 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 37 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 04 月 24 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 flyokai/amp-data-pipeline 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 flyokai/amp-data-pipeline 我们能提供哪些服务?
定制开发 / 二次开发

基于 flyokai/amp-data-pipeline 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 37
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 25
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-04-24