MCPcopy
hub / github.com/redis/go-redis / onDemandWorker

Method onDemandWorker

maintnotifications/handoff_worker.go:123–177  ·  view source on GitHub ↗

onDemandWorker processes handoff requests and exits when idle

()

Source from the content-addressed store, hash-verified

121
122// onDemandWorker processes handoff requests and exits when idle
123func (hwm *handoffWorkerManager) onDemandWorker() {
124 defer func() {
125 // Handle panics to ensure proper cleanup
126 if r := recover(); r != nil {
127 internal.Logger.Printf(context.Background(), logs.WorkerPanicRecovered(r))
128 }
129
130 // Decrement active worker count when exiting
131 hwm.activeWorkers.Add(-1)
132 hwm.workerWg.Done()
133 }()
134
135 // Create reusable timer to prevent timer leaks
136 timer := time.NewTimer(hwm.workerTimeout)
137 defer timer.Stop()
138
139 for {
140 // Reset timer for next iteration
141 if !timer.Stop() {
142 select {
143 case <-timer.C:
144 default:
145 }
146 }
147 timer.Reset(hwm.workerTimeout)
148
149 select {
150 case <-hwm.shutdown:
151 if internal.LogLevel.InfoOrAbove() {
152 internal.Logger.Printf(context.Background(), logs.WorkerExitingDueToShutdown())
153 }
154 return
155 case <-timer.C:
156 // Worker has been idle for too long, exit to save resources
157 if internal.LogLevel.InfoOrAbove() {
158 internal.Logger.Printf(context.Background(), logs.WorkerExitingDueToInactivityTimeout(hwm.workerTimeout))
159 }
160 return
161 case request := <-hwm.handoffQueue:
162 // Check for shutdown before processing
163 select {
164 case <-hwm.shutdown:
165 if internal.LogLevel.InfoOrAbove() {
166 internal.Logger.Printf(context.Background(), logs.WorkerExitingDueToShutdownWhileProcessing())
167 }
168 // Clean up the request before exiting
169 hwm.pending.Delete(request.ConnID)
170 return
171 default:
172 // Process the request
173 hwm.processHandoffRequest(request)
174 }
175 }
176 }
177}
178
179// processHandoffRequest processes a single handoff request
180func (hwm *handoffWorkerManager) processHandoffRequest(request HandoffRequest) {

Callers 1

ensureWorkerAvailableMethod · 0.95

Calls 10

processHandoffRequestMethod · 0.95
WorkerPanicRecoveredFunction · 0.92
InfoOrAboveMethod · 0.80
PrintfMethod · 0.65
AddMethod · 0.65
StopMethod · 0.65
ResetMethod · 0.45

Tested by

no test coverage detected