Repository: etcd-io/etcd
File: server/embed/etcd.go (Go)

Code before:
		zap.String("discovery-cacert", sc.DiscoveryCfg.Secure.Cacert),
		zap.String("discovery-user", sc.DiscoveryCfg.Auth.Username),

		zap.String("downgrade-check-interval", sc.DowngradeCheckTime.String()),
		zap.Int("max-learners", sc.ExperimentalMaxLearners),

		zap.String("v2-deprecation", string(ec.V2Deprecation)),
	)
}

// Config returns the current configuration.
func (e *Etcd) Config() Config {
	return e.cfg
}

// Close gracefully shuts down all servers/listeners.
// Client requests will be terminated with request timeout.
// After timeout, enforce remaning requests be closed immediately.
//
// The rough workflow to shut down etcd:
//  1. close the `stopc` channel, so that all error handlers (child
//     goroutines) won't send back any errors anymore;
//  2. close all client and metrics listeners, so that etcd server
//     stops receiving any new connection immediately;
//  3. stop the http and grpc servers gracefully, within request timeout;
//  4. call the cancel function to close the gateway context, so that
//     all gateway connections are closed.
//  5. stop etcd server gracefully, and ensure the main raft loop
//     goroutine is stopped;
//  6. stop all peer listeners, so that it stops receiving peer connections
//     and messages (wait up to 1-second);
//  7. wait for all child goroutines (i.e. client handlers, peer handlers
//     and metrics handlers) to exit;
//  8. close the `errc` channel to release the resource. Note that it's only
//     safe to close the `errc` after step 7 above is done, otherwise the
//     child goroutines may send errors back to already closed `errc` channel.
func (e *Etcd) Close() {
	fields := []zap.Field{
		zap.String("name", e.cfg.Name),
		zap.String("data-dir", e.cfg.Dir),
		zap.Strings("advertise-peer-urls", e.cfg.getAdvertisePeerURLs()),
		zap.Strings("advertise-client-urls", e.cfg.getAdvertiseClientURLs()),
	}
	lg := e.GetLogger()
	lg.Info("closing etcd server", fields...)
	defer func() {
		lg.Info("closed etcd server", fields...)
		verify.MustVerifyIfEnabled(verify.Config{
			Logger:     lg,
			DataDir:    e.cfg.Dir,
			ExactIndex: false,

Review comment (bug):
Should we close gateway before we close grpc servers? Correct me if I'm wrong, gateway exposes HTTP API and translates HTTP requests to grpc API by connecting to grpc server.

Code after:
		zap.String("discovery-cacert", sc.DiscoveryCfg.Secure.Cacert),
		zap.String("discovery-user", sc.DiscoveryCfg.Auth.Username),

		zap.String("downgrade-check-interval", sc.DowngradeCheckTime.String()),
		zap.Int("max-learners", sc.ExperimentalMaxLearners),

		zap.String("v2-deprecation", string(ec.V2Deprecation)),
	)
}

// Config returns the current configuration.
func (e *Etcd) Config() Config {
	return e.cfg
}

// Close gracefully shuts down all servers/listeners.
// Client requests will be terminated with request timeout.
// After timeout, enforce remaning requests be closed immediately.
//
// The rough workflow to shut down etcd:
//  1. close the `stopc` channel, so that all error handlers (child
//     goroutines) won't send back any errors anymore;
//  2. stop the http and grpc servers gracefully, within request timeout;
//  3. close all client and metrics listeners, so that etcd server
//     stops receiving any new connection;
//  4. call the cancel function to close the gateway context, so that
//     all gateway connections are closed.
//  5. stop etcd server gracefully, and ensure the main raft loop
//     goroutine is stopped;
//  6. stop all peer listeners, so that it stops receiving peer connections
//     and messages (wait up to 1-second);
//  7. wait for all child goroutines (i.e. client handlers, peer handlers
//     and metrics handlers) to exit;
//  8. close the `errc` channel to release the resource. Note that it's only
//     safe to close the `errc` after step 7 above is done, otherwise the
//     child goroutines may send errors back to already closed `errc` channel.
func (e *Etcd) Close() {
	fields := []zap.Field{
		zap.String("name", e.cfg.Name),
		zap.String("data-dir", e.cfg.Dir),
		zap.Strings("advertise-peer-urls", e.cfg.getAdvertisePeerURLs()),
		zap.Strings("advertise-client-urls", e.cfg.getAdvertiseClientURLs()),
	}
	lg := e.GetLogger()
	lg.Info("closing etcd server", fields...)
	defer func() {
		lg.Info("closed etcd server", fields...)
		verify.MustVerifyIfEnabled(verify.Config{
			Logger:     lg,
			DataDir:    e.cfg.Dir,
			ExactIndex: false,