聊聊dubbogo的gracefulShutdownFilter

43次阅读

共计 3129 个字符,预计需要花费 8 分钟才能阅读完成。

本文次要钻研一下 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

正文完
 0