Comments (4)
冰哥的拦截器设计类似web框架的中间件,拦截器有两个关键点:
- 拦截管理器 RealInterceptorChain
- 拦截器 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()) }
from zinx.
明白了,谢谢,设计模式值得学习
from zinx.
这个是仿的 okhttp 里面的责任链设计模式,想了解全部,可以去查看 okhttp 的拦截器
from zinx.
https://www.yuque.com/aceld/tsgooa/zn0y6xpynrzfd1z5
这部分zinx文档里有有说明。
from zinx.
Related Issues (20)
- 异步问题 HOT 3
- 建议增加设置golang net的默认keepalive的方法 HOT 1
- 请教zinx必须通过msgid处理消息吗 HOT 3
- 如果在同一台服务器初始化了两个server,初始化服务器顺序不同,导致routerslice问题 HOT 1
- 请问怎么构建一个websocket的client? HOT 1
- 关闭服务时,书写重复代码 HOT 1
- RouterSlices中Handlers的意义还存在逻辑错误 HOT 1
- connection.go SendBuffMsg函数代码重复 HOT 3
- clone函数切片应该使用深拷贝 HOT 5
- Request stepLock and Chain.Proceed
- 在zinx中网关节点做转发协议该如何配置路由? HOT 3
- How to send mesaage from server to cleint? HOT 2
- 支持TLS吗? HOT 1
- windows上通信,路由的触发会存在卡顿的情况 HOT 3
- [Warnning]: Request 对象池机制问题 HOT 2
- 如果我用其他语言写一个客户端,怎么才能触发路由 HOT 3
- 怎么能设置 拨号的超时时间 HOT 1
- [Panic]: send on closed channel
- decoder、pack、framedecoder HOT 1
- znet/request.go 文件中Request对象index成员的初始值问题
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from zinx.