序
本文主要研究一下dubbo-go的gracefulShutdownFilter
gracefulShutdownFilter
dubbo-go-v1.4.2/filter/filter_impl/graceful_shutdown_filter.go
type gracefulShutdownFilter struct {
activeCount int32
shutdownConfig *config.ShutdownConfig
}
- gracefulShutdownFilter定义了activeCount、shutdownConfig属性
init
dubbo-go-v1.4.2/filter/filter_impl/graceful_shutdown_filter.go
func init() {
var consumerFiler = &gracefulShutdownFilter{
shutdownConfig: config.GetConsumerConfig().ShutdownConfig,
}
var providerFilter = &gracefulShutdownFilter{
shutdownConfig: config.GetProviderConfig().ShutdownConfig,
}
extension.SetFilter(constant.CONSUMER_SHUTDOWN_FILTER, func() filter.Filter {
return consumerFiler
})
extension.SetFilter(constant.PROVIDER_SHUTDOWN_FILTER, func() filter.Filter {
return providerFilter
})
}
- init方法分别创建并设置了CONSUMER_SHUTDOWN_FILTER、PROVIDER_SHUTDOWN_FILTER
Invoke
dubbo-go-v1.4.2/filter/filter_impl/graceful_shutdown_filter.go
func (gf *gracefulShutdownFilter) Invoke(ctx context.Context, invoker protocol.Invoker, invocation protocol.Invocation) protocol.Result {
if gf.rejectNewRequest() {
logger.Info("The application is closing, new request will be rejected.")
return gf.getRejectHandler().RejectedExecution(invoker.GetUrl(), invocation)
}
atomic.AddInt32(&gf.activeCount, 1)
return invoker.Invoke(ctx, invocation)
}
- Invoke方法先判断gracefulShutdownFilter的rejectNewRequest是否为true,若为true则执行getRejectHandler().RejectedExecution,否则执行atomic.AddInt32及invoker.Invoke(ctx, invocation)
OnResponse
dubbo-go-v1.4.2/filter/filter_impl/graceful_shutdown_filter.go
func (gf *gracefulShutdownFilter) OnResponse(ctx context.Context, result protocol.Result, invoker protocol.Invoker, invocation protocol.Invocation) protocol.Result {
atomic.AddInt32(&gf.activeCount, -1)
// although this isn't thread safe, it won't be a problem if the gf.rejectNewRequest() is true.
if gf.shutdownConfig != nil && gf.activeCount <= 0 {
gf.shutdownConfig.RequestsFinished = true
}
return result
}
- OnResponse方法先执行atomic.AddInt32(&gf.activeCount, -1),之后判断gf.activeCount是否小于等于0,是的话则标记gf.shutdownConfig.RequestsFinished为true
rejectNewRequest
dubbo-go-v1.4.2/filter/filter_impl/graceful_shutdown_filter.go
func (gf *gracefulShutdownFilter) rejectNewRequest() bool {
if gf.shutdownConfig == nil {
return false
}
return gf.shutdownConfig.RejectRequest
}
- rejectNewRequest方法在shutdownConfig为nil是返回false,否则返回gf.shutdownConfig.RejectRequest的值
getRejectHandler
dubbo-go-v1.4.2/filter/filter_impl/graceful_shutdown_filter.go
func (gf *gracefulShutdownFilter) getRejectHandler() filter.RejectedExecutionHandler {
handler := constant.DEFAULT_KEY
if gf.shutdownConfig != nil && len(gf.shutdownConfig.RejectRequestHandler) > 0 {
handler = gf.shutdownConfig.RejectRequestHandler
}
return extension.GetRejectedExecutionHandler(handler)
}
- getRejectHandler方法在gf.shutdownConfig不为nil且gf.shutdownConfig.RejectRequestHandler长度大于0时设置handler值为gf.shutdownConfig.RejectRequestHandler,之后根据该值执行extension.GetRejectedExecutionHandler
小结
gracefulShutdownFilter定义了activeCount、shutdownConfig属性;其Invoke方法先判断gracefulShutdownFilter的rejectNewRequest是否为true,若为true则执行getRejectHandler().RejectedExecution,否则执行atomic.AddInt32及invoker.Invoke(ctx, invocation);其OnResponse方法先执行atomic.AddInt32(&gf.activeCount, -1),之后判断gf.activeCount是否小于等于0,是的话则标记gf.shutdownConfig.RequestsFinished为true
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。