request.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package tsdb
  2. func HandleRequest(req *Request) (*Response, error) {
  3. context := NewQueryContext(req.Queries, req.TimeRange)
  4. batches, err := getBatches(req)
  5. if err != nil {
  6. return nil, err
  7. }
  8. currentlyExecuting := 0
  9. for _, batch := range batches {
  10. if len(batch.Depends) == 0 {
  11. currentlyExecuting += 1
  12. batch.Started = true
  13. go batch.process(context)
  14. }
  15. }
  16. response := &Response{}
  17. for currentlyExecuting != 0 {
  18. select {
  19. case batchResult := <-context.ResultsChan:
  20. currentlyExecuting -= 1
  21. response.BatchTimings = append(response.BatchTimings, batchResult.Timings)
  22. for refId, result := range batchResult.QueryResults {
  23. context.Results[refId] = result
  24. }
  25. for _, batch := range batches {
  26. // not interested in started batches
  27. if batch.Started {
  28. continue
  29. }
  30. if batch.allDependenciesAreIn(context) {
  31. currentlyExecuting += 1
  32. batch.Started = true
  33. go batch.process(context)
  34. }
  35. }
  36. }
  37. }
  38. response.Results = context.Results
  39. return response, nil
  40. }