Go中职责链模式通过函数型中间件实现,每个Middleware接收并返回HandlerFunc,链式调用支持前置/后置逻辑;组合时右结合,执行顺序与注册相反;典型应用包括鉴权、日志、限流等单一职责过滤器。

在 Go 语言中实现职责链模式(Chain of Responsibility),核心是让多个处理器(Handler)按顺序接收请求,每个处理器可选择处理或转发给下一个。结合过滤器链(Filter Chain)思想,还能在请求进入/离开时插入前置/后置逻辑(如日志、鉴权、超时等)。Go 原生的 http.Handler 就是典型职责链:通过 http.HandlerFunc 和中间件组合,天然支持链式调用。
职责链的关键是统一接口和链式组装能力。Go 中推荐使用函数类型封装,简洁且高效:
type HandlerFunc func(http.ResponseWriter, *http.Request, HandlerFunc)
<p>type Handler interface {
ServeHTTP(http.ResponseWriter, *http.Request)
}</p><p>// 链式中间件:返回包装后的 HandlerFunc
type Middleware func(HandlerFunc) HandlerFunc
每个中间件接收“下一个处理器”,返回新处理器;最终 handler 调用 next 即触发链式传递。不调用 next 表示终止链(如鉴权失败直接返回 401)。
每个过滤器专注单一职责,符合开闭原则。例如:
立即学习“go语言免费学习笔记(深入)”;
示例(鉴权):
func AuthMiddleware(next HandlerFunc) HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
token := r.Header.Get("Authorization")
if token == "" || !isValidToken(token) {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return // 不调用 next,链在此终止
}
next(w, r) // 继续向下传递
}
}
使用函数式组合(从左到右或右到左)构建完整链。推荐右结合(最外层中间件最先执行):
func Chain(handlers ...Middleware) Middleware {
return func(next HandlerFunc) HandlerFunc {
for i := len(handlers) - 1; i >= 0; i-- {
next = handlers[i](next)
}
return next
}
}
<p>// 使用示例
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello, secured world!"))
})</p><p>final := Chain(AuthMiddleware, LoggingMiddleware, RateLimitMiddleware)(handler)
http.Handle("/api/data", final)
这样,每次请求会依次经过限流 → 日志 → 鉴权 → 实际业务 handler(注意执行顺序与组合顺序相反,因是“包装”关系)。
标准 http.Request 支持 context.WithValue,可在链中安全透传数据(如用户 ID、请求 ID):
func UserContextMiddleware(next HandlerFunc) HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
userID := extractUserID(r)
ctx := context.WithValue(r.Context(), "user_id", userID)
r = r.WithContext(ctx)
next(w, r)
}
}
若需更细粒度控制中断(如返回特定错误码或响应),可自定义错误类型或扩展 HandlerFunc 签名(例如返回 error),但通常用 http.Error + 提前 return 更符合 HTTP 语义。
以上就是如何在Golang中使用职责链模式_顺序处理请求并支持过滤器链的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号