MCPcopy Index your code
hub / github.com/GoogleCloudPlatform/cloud-sql-proxy / runSignalWrapper

Function runSignalWrapper

cmd/root.go:1060–1259  ·  view source on GitHub ↗

runSignalWrapper watches for SIGTERM and SIGINT and interupts execution if necessary.

(cmd *Command)

Source from the content-addressed store, hash-verified

1058
1059// runSignalWrapper watches for SIGTERM and SIGINT and interupts execution if necessary.
1060func runSignalWrapper(cmd *Command) (err error) {
1061 defer func() { _ = cmd.cleanup() }()
1062 ctx, cancel := context.WithCancel(cmd.Context())
1063 defer cancel()
1064
1065 // Configure collectors before the proxy has started to ensure we are
1066 // collecting metrics before *ANY* Cloud SQL Admin API calls are made.
1067 enableMetrics := !cmd.conf.DisableMetrics
1068 enableTraces := !cmd.conf.DisableTraces
1069 if cmd.conf.TelemetryProject != "" && (enableMetrics || enableTraces) {
1070 sd, err := stackdriver.NewExporter(stackdriver.Options{
1071 ProjectID: cmd.conf.TelemetryProject,
1072 MetricPrefix: cmd.conf.TelemetryPrefix,
1073 })
1074 if err != nil {
1075 return err
1076 }
1077 if enableMetrics {
1078 err = sd.StartMetricsExporter()
1079 if err != nil {
1080 return err
1081 }
1082 }
1083 if enableTraces {
1084 s := trace.ProbabilitySampler(1 / float64(cmd.conf.TelemetryTracingSampleRate))
1085 trace.ApplyConfig(trace.Config{DefaultSampler: s})
1086 trace.RegisterExporter(sd)
1087 }
1088 defer func() {
1089 sd.Flush()
1090 sd.StopMetricsExporter()
1091 }()
1092 }
1093
1094 shutdownCh := make(chan error)
1095 // watch for sigterm / sigint signals
1096 signals := make(chan os.Signal, 1)
1097 signal.Notify(signals, syscall.SIGTERM, syscall.SIGINT)
1098 go func() {
1099 var s os.Signal
1100 select {
1101 case s = <-signals:
1102 case <-ctx.Done():
1103 // this should only happen when the context supplied in tests in canceled
1104 s = syscall.SIGINT
1105 }
1106 switch s {
1107 case syscall.SIGINT:
1108 cmd.logger.Debugf("Sending SIGINT signal for proxy to shutdown.")
1109 shutdownCh <- errSigInt
1110 case syscall.SIGTERM:
1111 cmd.logger.Debugf("Sending SIGTERM signal for proxy to shutdown.")
1112 if cmd.conf.ExitZeroOnSigterm {
1113 shutdownCh <- errSigTermZero
1114 } else {
1115 shutdownCh <- errSigTerm
1116 }
1117 }

Callers 1

NewCommandFunction · 0.85

Calls 10

CloseMethod · 0.95
ServeMethod · 0.95
NewClientFunction · 0.92
NewCheckFunction · 0.92
startHTTPServerFunction · 0.85
quitquitquitFunction · 0.85
DoneMethod · 0.80
DebugfMethod · 0.65
ErrorfMethod · 0.65
InfofMethod · 0.65

Tested by

no test coverage detected