聊聊dapr的Pipeline


本文摘自网络,作者,侵删。

本文主要研究一下dapr的Pipeline

Pipeline

dapr/pkg/middleware/http/http_pipeline.go

import (
    "github.com/dapr/dapr/pkg/config"
    "github.com/valyala/fasthttp"
)

type Middleware func(h fasthttp.RequestHandler) fasthttp.RequestHandler

// HTTPPipeline defines the middleware pipeline to be plugged into Dapr sidecar
type Pipeline struct {
    Handlers []Middleware
}

func BuildHTTPPipeline(spec config.PipelineSpec) (Pipeline, error) {
    return Pipeline{}, nil
}

func (p Pipeline) Apply(handler fasthttp.RequestHandler) fasthttp.RequestHandler {
    for i := len(p.Handlers) - 1; i >= 0; i-- {
        handler = p.Handlers[i](handler)
    }
    return handler
}
Pipeline定义了Handlers属性,是一个Middleware数组;Pipeline定义了Apply方法,它会从后往前挨个执行Middleware函数;Middleware函数接收fasthttp.RequestHandler,返回fasthttp.RequestHandler

实例

dapr/pkg/http/server.go

type server struct {
    config      ServerConfig
    tracingSpec config.TracingSpec
    metricSpec  config.MetricSpec
    pipeline    http_middleware.Pipeline
    api         API
}

func (s *server) useComponents(next fasthttp.RequestHandler) fasthttp.RequestHandler {
    return s.pipeline.Apply(next)
}
server定义了pipeline属性,其useComponents方法接收fasthttp.RequestHandler),对其执行pipeline.Apply

小结

dapr的Pipeline定义了Handlers属性,是一个Middleware数组;Pipeline定义了Apply方法,它会从后往前挨个执行Middleware函数;Middleware函数接收fasthttp.RequestHandler,返回fasthttp.RequestHandler。

doc

  • dapr

本文来自:Segmentfault

感谢作者:codecraft

查看原文:聊聊dapr的Pipeline

相关阅读 >>

模块一 Go语言基础知识-库源码文件

Golang-mysql自定义数据类型

聊聊promtail的client

又掉进slice切片的坑里面了

详解 Golang 通道 chan

Golang和erlang区别

小孩数数出列问题

【发布了Go-carbon1.1.1版本】完善对主流orm的支持,新增公共方法

Go-gin源码(一)

Golang怎么判断数组是否为空

更多相关阅读请进入《Go》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...