如何在Golang中使用职责链模式_顺序处理请求并支持过滤器链

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

在 Go 语言中实现职责链模式(Chain of Responsibility),核心是让多个处理器(Handler)按顺序接收请求,每个处理器可选择处理或转发给下一个。结合过滤器链(Filter Chain)思想,还能在请求进入/离开时插入前置/后置逻辑(如日志、鉴权、超时等)。Go 原生的 http.Handler 就是典型职责链:通过 http.HandlerFunc 和中间件组合,天然支持链式调用。

定义通用 Handler 接口与链式构造

职责链的关键是统一接口和链式组装能力。Go 中推荐使用函数类型封装,简洁且高效:

type HandlerFunc func(http.ResponseWriter, *http.Request, HandlerFunc)

type Handler interface { ServeHTTP(http.ResponseWriter, *http.Request) }

// 链式中间件:返回包装后的 HandlerFunc type Middleware func(HandlerFunc) HandlerFunc

每个中间件接收“下一个处理器”,返回新处理器;最终 handler 调用 next 即触发链式传递。不调用 next 表示终止链(如鉴权失败直接返回 401)。

编写可复用的过滤器(Middleware)

每个过滤器专注单一职责,符合开闭原则。例如:

  • 日志中间件:记录请求路径、耗时
  • 鉴权中间件:检查 token 或 session,无权限则中断链
  • 限流中间件:基于 IP 或用户 ID 控制 QPS
  • 恢复 panic 中间件:避免崩溃导致服务不可用

示例(鉴权):

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
    }
}

// 使用示例 handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("Hello, secured world!")) })

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 语义。