sequence

This paper mainly studies dubo-Go’s gracefulShutdownFilter

gracefulShutdownFilter

Dubbo – go – v1.4.2 / filter/filter_impl graceful_shutdown_filter. Go

type gracefulShutdownFilter struct {
	activeCount    int32
	shutdownConfig *config.ShutdownConfig
}
Copy the code
  • GracefulShutdownFilter defines activeCount and shutdownConfig attributes

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
	})
}
Copy the code
  • The init method creates and sets the CONSUMER_SHUTDOWN_FILTER and PROVIDER_SHUTDOWN_FILTER, respectively

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)
}
Copy the code
  • The Invoke method first evaluates whether rejectNewRequest of gracefulShutdownFilter is true. If true, getRejectHandler().rejectedexecution is executed. Otherwise Invoke Atomic.AddInt32 and 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.
	ifgf.shutdownConfig ! = nil && gf.activeCount <= 0 { gf.shutdownConfig.RequestsFinished =true
	}
	return result
}
Copy the code
  • OnResponse method: first perform atomic.AddInt32(&gf.activeCount, -1) and then check whether gf.activeCount is less than or equal to 0. So the tag gf. ShutdownConfig. RequestsFinished to 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
}
Copy the code
  • For nil in shutdownConfig rejectNewRequest method is returns false, otherwise returns gf. ShutdownConfig. RejectRequest value

getRejectHandler

Dubbo – go – v1.4.2 / filter/filter_impl graceful_shutdown_filter. Go

func (gf *gracefulShutdownFilter) getRejectHandler() filter.RejectedExecutionHandler {
	handler := constant.DEFAULT_KEY
	ifgf.shutdownConfig ! = nil && len(gf.shutdownConfig.RejectRequestHandler) > 0 { handler = gf.shutdownConfig.RejectRequestHandler }return extension.GetRejectedExecutionHandler(handler)
}
Copy the code
  • GetRejectHandler method in the gf. ShutdownConfig not is nil and gf. ShutdownConfig. RejectRequestHandler handler is set when value is greater than 0 length gf. ShutdownConfig. Rejec TRequestHandler, according to the value after execution of the extension. GetRejectedExecutionHandler

summary

GracefulShutdownFilter defines activeCount and shutdownConfig attributes; The Invoke method determines whether rejectNewRequest of gracefulShutdownFilter is true, if true, it executes getRejectHandler().rejectedexecution. Otherwise, Invoke atomic.AddInt32 and Invoker.Invoke(CTX, Invocation); Its OnResponse method first executes atomic.AddInt32(& Gf. activeCount, -1), and then determines whether gf.activeCount is less than or equal to 0. So the tag gf. ShutdownConfig. RequestsFinished to true

doc

  • graceful_shutdown_filter