共计 909 个字符,预计需要花费 3 分钟才能阅读完成。
序
本文次要钻研一下 dubbo-go-proxy 的 recoveryFilter
recoveryFilter
dubbo-go-proxy/pkg/filter/recovery/recovery.go
func Init() {extension.SetFilterFunc(constant.RecoveryFilter, recoveryFilterFunc())
}
func recoveryFilterFunc() context.FilterFunc {return New().Do()}
// recoveryFilter is a filter for recover.
type recoveryFilter struct {
}
// New create timeout filter.
func New() filter.Filter {return &recoveryFilter{}
}
recoveryFilter 往 extension 注册名为
dgp.filters.recovery
的 recoveryFilterFunc;该 func 执行的是 recoveryFilter 的 Do 办法
Do
dubbo-go-proxy/pkg/filter/recovery/recovery.go
// Recovery execute recoveryFilter filter logic, if recover happen, print log or do other things.
func (f recoveryFilter) Do() context.FilterFunc {return func(c context.Context) {defer func() {if err := recover(); err != nil {logger.Warnf("[dubboproxy go] error:%+v", err)
c.WriteErr(err)
}
}()
c.Next()}
}
Do 办法先注册一个 defer func 进行 recover,而后再执行 c.Next()
小结
dubbo-go-proxy 的 recoveryFilter 注册一个 defer func 进行 recover,而后再执行 c.Next()。
doc
- dubbo-go-proxy
正文完