MCPcopy Create free account
hub / github.com/TheThingsNetwork/lorawan-stack / workerBody

Method workerBody

pkg/workerpool/workerpool.go:95–149  ·  view source on GitHub ↗
(initialWork *contextualItem[T])

Source from the content-addressed store, hash-verified

93}
94
95func (wp *workerPool[T]) workerBody(initialWork *contextualItem[T]) func(context.Context) error {
96 worker := func(ctx context.Context) error {
97 var timeout bool
98 defer func() {
99 if timeout {
100 return
101 }
102 atomic.AddInt32(&wp.workers, -1)
103 select {
104 case <-ctx.Done():
105 case <-wp.Done():
106 default:
107 // Since the task did not finish due to a context cancellation
108 // or a timeout, the worker body must have panicked. As such
109 // we attempt to spawn a replacement worker in order to avoid
110 // stalling the queue indefinitely.
111 wp.spawnWorker(nil)
112 }
113 }()
114
115 defer wp.wg.Done()
116 defer registerWorkerStopped(wp.Name)
117
118 registerWorkerIdle(wp.Name)
119 defer registerWorkerBusy(wp.Name)
120
121 if initialWork != nil {
122 wp.handle(initialWork)
123 }
124
125 for {
126 select {
127 case <-wp.Done():
128 return wp.Err()
129
130 case <-ctx.Done():
131 return ctx.Err()
132
133 case <-time.After(wp.WorkerIdleTimeout):
134 if decrementIfGreaterThan(&wp.workers, int32(wp.MinWorkers)) {
135 timeout = true
136 return nil
137 }
138
139 case item := <-wp.fastQueue:
140 wp.handle(item)
141
142 case item := <-wp.mainQueue:
143 registerWorkDequeued(wp.Name, item.queuedAt)
144 wp.handle(item)
145 }
146 }
147 }
148 return worker
149}
150
151// spawnWorker spawns a worker task, if a worker slot is available.
152func (wp *workerPool[T]) spawnWorker(initialWork *contextualItem[T]) bool {

Callers 1

spawnWorkerMethod · 0.95

Calls 10

spawnWorkerMethod · 0.95
handleMethod · 0.95
registerWorkerStoppedFunction · 0.85
registerWorkerIdleFunction · 0.85
registerWorkerBusyFunction · 0.85
decrementIfGreaterThanFunction · 0.85
registerWorkDequeuedFunction · 0.85
DoneMethod · 0.80
AfterMethod · 0.80
ErrMethod · 0.45

Tested by

no test coverage detected