machine

machine

高效灵活的Go语言数据工作流库

Machine是Go语言开发的数据工作流库,支持构建简洁和复杂的工作流。该库提供转换、过滤、条件分支和分发等功能,可灵活处理各类数据。Machine集成OpenTelemetry,便于收集指标和跟踪。它适用于多种数据处理场景,支持循环、重试和自我修复机制。

Machine数据工作流Go语言函数式编程OpenTelemetryGithub开源项目

Go CodeQL PkgGoDev GoDoc Go Report Card Codacy Badge Codacy Badge Version Badge

<p align="center"> <img alt="Machine" height="125" src="https://raw.githubusercontent.com/whitaker-io/machine/master/docs/static/Black-No-BG.png"> </p>

Machine is a library for creating data workflows. These workflows can be either very concise or quite complex, even allowing for cycles for flows that need retry or self healing mechanisms.


Installation

Add the primary library to your project

go get github.com/whitaker-io/machine/v3

The two function types are:

// Monad is a function that is applied to payload and used for transformations type Monad[T any] func(d T) T // Filter is a function that can be used to filter the payload. type Filter[T any] func(d T) bool

These are used in the Machine for functional operations

// New is a function for creating a new Machine. // // name string // input chan T // option ...Option // // Call the startFn returned by New to start the Machine once built. func New[T any](name string, input chan T, options ...Option) (startFn func(context.Context), x Machine[T]) // Transform is a function for converting the type of the Machine. Cannot be used inside a loop // until I figure out how to do it without some kind of run time error or overly complex // tracking method that isn't type safe. I really wish method level generics were a thing. func Transform[T, U any](m Machine[T], fn func(d T) U) (Machine[U], error) // Machine is the interface provided for creating a data processing stream. type Machine[T any] interface { // Name returns the name of the Machine path. Useful for debugging or reasoning about the path. Name() string // Then apply a mutation to each individual element of the payload. Then(a Monad[T]) Machine[T] // Recurse applies a recursive function to the payload through a Y Combinator. // f is a function used by the Y Combinator to perform a recursion // on the payload. // Example: // // func(f Monad[int]) Monad[int] { // return func(x int) int { // if x <= 0 { // return 1 // } else { // return x * f(x-1) // } // } // } Recurse(x Monad[Monad[T]]) Machine[T] // Memoize applies a recursive function to the payload through a Y Combinator // and memoizes the results based on the index func. // f is a function used by the Y Combinator to perform a recursion // on the payload. // Example: // // func(f Monad[int]) Monad[int] { // return func(x int) int { // if x <= 0 { // return 1 // } else { // return x * f(x-1) // } // } // } Memoize(x Monad[Monad[T]], index func(T) string) Machine[T] // Or runs all of the functions until one succeeds or sends the payload to the right branch Or(x ...Filter[T]) (Machine[T], Machine[T]) // And runs all of the functions and if one doesnt succeed sends the payload to the right branch And(x ...Filter[T]) (Machine[T], Machine[T]) // Filter splits the data into multiple stream branches If(f Filter[T]) (Machine[T], Machine[T]) // Select applies a series of Filters to the payload and returns a list of Builders // the last one being for any unmatched payloads. Select(fns ...Filter[T]) []Machine[T] // Tee duplicates the data into multiple stream branches. Tee(func(T) (a, b T)) (Machine[T], Machine[T]) // While creates a loop in the stream based on the filter While(x Filter[T]) (loop, out Machine[T]) // Drop terminates the data from further processing without passing it on Drop() // Distribute is a function used for fanout Distribute(Edge[T]) Machine[T] // Output provided channel Output() chan T }

Distribute is a special method used for fan-out operations. It takes an instance of Edge[T] and can be used most typically to distribute work via a Pub/Sub or it can be used in a commandline utility to handle user input or a similiar blocking process.

The Edge[T] interface is as follows:

// Edge is an interface that is used for transferring data between vertices type Edge[T any] interface { Output() chan T Send(payload T) }

The Send method is used for data leaving the associated vertex and the Output method is used by the following vertex to receive data from the channel.


Confirguration is done using the Option helper

// Option is used to configure the machine type Option interface // OptionFIF0 controls the processing order of the payloads // If set to true the system will wait for one payload // to be processed before starting the next. var OptionFIF0 Option // OptionBufferSize sets the buffer size on the edge channels between the // vertices, this setting can be useful when processing large amounts // of data with FIFO turned on. func OptionBufferSize(size int) Option // OptionAttributes apply the slog.Attr's to the machine metrics and spans // Do not override the "name", "type", "duration", "error", or "value" attributes func OptionAttributes(attributes ...slog.Attr) Option // OptionFlush attempts to send all data to the flushFN before exiting after the gracePeriod has expired // Im looking for a good way to make this type specific, but want to avoid having to add separate option // settings for the Transform function. func OptionFlush(gracePeriod time.Duration, flushFN func(vertexName string, payload any)) Option

Machine supports collecting metrics and traces through a log/slog wrapper that sends the telemetry to the provided OpenTelemetry Meter and Tracer

// import "github.com/whitaker-io/machine/telemetry" // Make your slog handler however you please yourSlogHandler := slog.Default().Handler() // wrap your handler and provide your tracer and meter telemetryHandler := telemetry.New( yourSlogHandler, meterProvider.Meter("your_meter"), // Your otel metric.Meter tracerProvider.Tracer("your_tracer"), // Your otel trace.Tracer false, // Log Metrics and Traces to logs as well (useful for debugging) ) slog.SetDefault(slog.New(telemetryHandler))

Examples of Edge implentations can be found in the edge directory and can be used as follows

// import "github.com/whitaker-io/machine/edge/pubsub" func New[T any]( ctx context.Context, subscription *pubsub.Subscription, publisher *pubsub.Topic, to func(T) *pubsub.Message, from func(context.Context, *pubsub.Message) T, ) machine.Edge[T] // import "github.com/whitaker-io/machine/edge/http" func New[T any](c http.Client, fn func(context.Context, T) *http.Request) machine.Edge[T]


🤝 Contributing

Contributions, issues and feature requests are welcome.<br /> Feel free to check issues page if you want to contribute.<br /> Check the contributing guide.<br />

Author

👤 Jonathan Whitaker

Show your support

Please ⭐️ this repository if this project helped you!


License

Machine is provided under the MIT License.

The MIT License (MIT) Copyright (c) 2020 Jonathan Whitaker

编辑推荐精选

扣子-AI办公

扣子-AI办公

职场AI,就用扣子

AI办公助手,复杂任务高效处理。办公效率低?扣子空间AI助手支持播客生成、PPT制作、网页开发及报告写作,覆盖科研、商业、舆情等领域的专家Agent 7x24小时响应,生活工作无缝切换,提升50%效率!

堆友

堆友

多风格AI绘画神器

堆友平台由阿里巴巴设计团队创建,作为一款AI驱动的设计工具,专为设计师提供一站式增长服务。功能覆盖海量3D素材、AI绘画、实时渲染以及专业抠图,显著提升设计品质和效率。平台不仅提供工具,还是一个促进创意交流和个人发展的空间,界面友好,适合所有级别的设计师和创意工作者。

图像生成AI工具AI反应堆AI工具箱AI绘画GOAI艺术字堆友相机AI图像热门
码上飞

码上飞

零代码AI应用开发平台

零代码AI应用开发平台,用户只需一句话简单描述需求,AI能自动生成小程序、APP或H5网页应用,无需编写代码。

Vora

Vora

免费创建高清无水印Sora视频

Vora是一个免费创建高清无水印Sora视频的AI工具

Refly.AI

Refly.AI

最适合小白的AI自动化工作流平台

无需编码,轻松生成可复用、可变现的AI自动化工作流

酷表ChatExcel

酷表ChatExcel

大模型驱动的Excel数据处理工具

基于大模型交互的表格处理系统,允许用户通过对话方式完成数据整理和可视化分析。系统采用机器学习算法解析用户指令,自动执行排序、公式计算和数据透视等操作,支持多种文件格式导入导出。数据处理响应速度保持在0.8秒以内,支持超过100万行数据的即时分析。

AI工具酷表ChatExcelAI智能客服AI营销产品使用教程
TRAE编程

TRAE编程

AI辅助编程,代码自动修复

Trae是一种自适应的集成开发环境(IDE),通过自动化和多元协作改变开发流程。利用Trae,团队能够更快速、精确地编写和部署代码,从而提高编程效率和项目交付速度。Trae具备上下文感知和代码自动完成功能,是提升开发效率的理想工具。

AI工具TraeAI IDE协作生产力转型热门
AIWritePaper论文写作

AIWritePaper论文写作

AI论文写作指导平台

AIWritePaper论文写作是一站式AI论文写作辅助工具,简化了选题、文献检索至论文撰写的整个过程。通过简单设定,平台可快速生成高质量论文大纲和全文,配合图表、参考文献等一应俱全,同时提供开题报告和答辩PPT等增值服务,保障数据安全,有效提升写作效率和论文质量。

AI辅助写作AI工具AI论文工具论文写作智能生成大纲数据安全AI助手热门
博思AIPPT

博思AIPPT

AI一键生成PPT,就用博思AIPPT!

博思AIPPT,新一代的AI生成PPT平台,支持智能生成PPT、AI美化PPT、文本&链接生成PPT、导入Word/PDF/Markdown文档生成PPT等,内置海量精美PPT模板,涵盖商务、教育、科技等不同风格,同时针对每个页面提供多种版式,一键自适应切换,完美适配各种办公场景。

AI办公办公工具AI工具博思AIPPTAI生成PPT智能排版海量精品模板AI创作热门
潮际好麦

潮际好麦

AI赋能电商视觉革命,一站式智能商拍平台

潮际好麦深耕服装行业,是国内AI试衣效果最好的软件。使用先进AIGC能力为电商卖家批量提供优质的、低成本的商拍图。合作品牌有Shein、Lazada、安踏、百丽等65个国内外头部品牌,以及国内10万+淘宝、天猫、京东等主流平台的品牌商家,为卖家节省将近85%的出图成本,提升约3倍出图效率,让品牌能够快速上架。

下拉加载更多