聊聊dubbo-go的gracefulShutdownFilter

本文主要研究一下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

doc

  • graceful_shutdown_filter

原文 

https://segmentfault.com/a/1190000023263680

本站部分文章源于互联网,本着传播知识、有益学习和研究的目的进行的转载,为网友免费提供。如有著作权人或出版方提出异议,本站将立即删除。如果您对文章转载有任何疑问请告之我们,以便我们及时纠正。

PS:推荐一个微信公众号: askHarries 或者qq群:474807195,里面会分享一些资深架构师录制的视频录像:有Spring,MyBatis,Netty源码分析,高并发、高性能、分布式、微服务架构的原理,JVM性能优化这些成为架构师必备的知识体系。还能领取免费的学习资源,目前受益良多

转载请注明原文出处:Harries Blog™ » 聊聊dubbo-go的gracefulShutdownFilter

赞 (0)
分享到:更多 ()

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址