(batch []*core.InternalRequest, timeout time.Duration)
| 177 | } |
| 178 | |
| 179 | func (p *batchProcessor) Process(batch []*core.InternalRequest, timeout time.Duration) error { |
| 180 | p.results = make([]*BatchRequestResult, 0, len(batch)) |
| 181 | |
| 182 | if p.stopCh != nil { |
| 183 | close(p.stopCh) |
| 184 | } |
| 185 | p.stopCh = make(chan struct{}, 1) |
| 186 | |
| 187 | if p.errCh != nil { |
| 188 | close(p.errCh) |
| 189 | } |
| 190 | p.errCh = make(chan error, 1) |
| 191 | |
| 192 | return p.app.RunInTransaction(func(txApp core.App) error { |
| 193 | // used to interupts the recursive processing calls in case of a timeout or connection close |
| 194 | defer func() { |
| 195 | p.stopCh <- struct{}{} |
| 196 | }() |
| 197 | |
| 198 | go func() { |
| 199 | err := p.process(txApp, batch, 0) |
| 200 | |
| 201 | if err != nil { |
| 202 | err = validation.Errors{ |
| 203 | "requests": validation.Errors{ |
| 204 | strconv.Itoa(p.failedIndex): &BatchResponseError{ |
| 205 | code: "batch_request_failed", |
| 206 | message: "Batch request failed.", |
| 207 | err: router.ToApiError(err), |
| 208 | }, |
| 209 | }, |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | // note: to avoid copying and due to the process recursion the final results order is reversed |
| 214 | if err == nil { |
| 215 | slices.Reverse(p.results) |
| 216 | } |
| 217 | |
| 218 | p.errCh <- err |
| 219 | }() |
| 220 | |
| 221 | select { |
| 222 | case responseErr := <-p.errCh: |
| 223 | return responseErr |
| 224 | case <-time.After(timeout): |
| 225 | // note: we don't return 408 Reques Timeout error because |
| 226 | // some browsers perform automatic retry behind the scenes |
| 227 | // which are hard to debug and unnecessary |
| 228 | return errors.New("batch transaction timeout") |
| 229 | case <-p.baseEvent.Request.Context().Done(): |
| 230 | return errors.New("batch request interrupted") |
| 231 | } |
| 232 | }) |
| 233 | } |
| 234 | |
| 235 | func (p *batchProcessor) process(activeApp core.App, batch []*core.InternalRequest, i int) error { |
| 236 | select { |
no test coverage detected