Code Monkey home page Code Monkey logo

Comments (4)

fivezjd avatar fivezjd commented on June 10, 2024 2

冰哥的拦截器设计类似web框架的中间件,拦截器有两个关键点:

  1. 拦截管理器 RealInterceptorChain
  2. 拦截器 interceptor

拦截管理器 RealInterceptorChain 主要作用是维护一个切片interceptors,用来管理所有要执行的拦截器,func (this *RealInterceptorChain) Proceed(request ziface.Request) ziface.Response 这个是拦截管理器的一个方法,用来执行拦截管理器中的拦截器,你可以看到,他的入参是一个 ziface.Request,而 ziface.Request 是一个空接口,在每个拦截器interceptor执行Intercept() 的时候,会递归的调用Proceed(),这样会依次执行完拦截管理器中的所有拦截器,类似web框架中的next()

拦截管理器执行

func (this *RealInterceptorChain) Proceed(request ziface.Request) ziface.Response { if this.position < len(this.interceptors) { chain := NewRealInterceptorChain(this.interceptors, this.position+1, request) interceptor := this.interceptors[this.position] response := interceptor.Intercept(chain) // 执行拦截器的拦截方法 return response } return request }

递归调用
// 拦截器的拦截方法 func (this *MsgHandle) Intercept(chain ziface.Chain) ziface.Response { request := chain.Request() if request != nil { switch request.(type) { case ziface.IRequest: iRequest := request.(ziface.IRequest) if utils.GlobalObject.WorkerPoolSize > 0 { //已经启动工作池机制,将消息交给Worker处理 this.SendMsgToTaskQueue(iRequest) } else { //从绑定好的消息和对应的处理方法中执行对应的Handle方法 go this.DoMsgHandler(iRequest) } } } // 递归调用拦截器的执行方法,进行下一个拦截器的执行 return chain.Proceed(chain.Request()) }
image

from zinx.

Xuzan9396 avatar Xuzan9396 commented on June 10, 2024

明白了,谢谢,设计模式值得学习

from zinx.

xxl6097 avatar xxl6097 commented on June 10, 2024

这个是仿的 okhttp 里面的责任链设计模式,想了解全部,可以去查看 okhttp 的拦截器

from zinx.

aceld avatar aceld commented on June 10, 2024

https://www.yuque.com/aceld/tsgooa/zn0y6xpynrzfd1z5
这部分zinx文档里有有说明。

from zinx.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.