MCPcopy Create free account
hub / github.com/PostHog/duckgres / runChildWorker

Function runChildWorker

server/worker.go:194–423  ·  view source on GitHub ↗

runChildWorker handles a single client connection in a child process. Returns the appropriate exit code.

(tcpConn *net.TCPConn, cfg *ChildConfig)

Source from the content-addressed store, hash-verified

192
193// runChildWorker handles a single client connection in a child process.
194// Returns the appropriate exit code.
195func runChildWorker(tcpConn *net.TCPConn, cfg *ChildConfig) int {
196 // Set up signal handling for graceful shutdown and query cancellation
197 sigChan := make(chan os.Signal, 1)
198 signal.Notify(sigChan, syscall.SIGTERM, syscall.SIGINT, syscall.SIGUSR1)
199
200 // Create a context for shutdown signals (SIGTERM/SIGINT)
201 shutdownCtx, shutdownCancel := context.WithCancel(context.Background())
202 defer shutdownCancel()
203
204 // Create a channel to signal query cancellation (SIGUSR1).
205 // This is separate from shutdown so we can cancel queries without closing
206 // the connection. Each SIGUSR1 delivers ONE cancel token (coalescing
207 // bursts) that the in-flight query's context goroutine consumes — the
208 // channel must never be closed: a closed channel is permanently readable,
209 // so a single cancel would instantly cancel every subsequent query on the
210 // connection (the session could never run another statement).
211 queryCancelCh := make(chan struct{}, 1)
212
213 // Handle signals in a goroutine
214 go func() {
215 for sig := range sigChan {
216 switch sig {
217 case syscall.SIGTERM, syscall.SIGINT:
218 slog.Info("Received shutdown signal", "signal", sig)
219 shutdownCancel()
220 case syscall.SIGUSR1:
221 slog.Info("Received query cancel signal")
222 notifyQueryCancel(queryCancelCh)
223 }
224 }
225 }()
226
227 // Load TLS certificate
228 cert, err := tls.LoadX509KeyPair(cfg.TLSCertFile, cfg.TLSKeyFile)
229 if err != nil {
230 slog.Error("Failed to load TLS certificates", "error", err)
231 return ExitError
232 }
233
234 tlsConfig := &tls.Config{
235 Certificates: []tls.Certificate{cert},
236 }
237
238 // Complete TLS handshake with timeout to prevent slow clients from holding resources
239 // Parent has already sent 'S' response to SSL request, we just need to do the handshake
240 tlsConn := tls.Server(tcpConn, tlsConfig)
241
242 // Set deadline for TLS handshake (30 seconds should be plenty)
243 if err := tlsConn.SetDeadline(time.Now().Add(30 * time.Second)); err != nil {
244 slog.Error("Failed to set TLS handshake deadline", "error", err)
245 return ExitError
246 }
247
248 if err := tlsConn.Handshake(); err != nil {
249 slog.Error("TLS handshake failed", "error", err, "remote_addr", cfg.RemoteAddr)
250 return ExitError
251 }

Callers 1

RunChildModeFunction · 0.85

Calls 15

initConnsMapMethod · 0.95
registerConnMethod · 0.95
safeCleanupDBMethod · 0.95
sendInitialParamsMethod · 0.95
messageLoopMethod · 0.95
ReadStartupMessageFunction · 0.92
WriteErrorResponseFunction · 0.92
WriteReadyForQueryFunction · 0.92
notifyQueryCancelFunction · 0.85
authenticateChildClientFunction · 0.85

Tested by

no test coverage detected